SNMP: UBUNTU 20.04 instalasi SNMP
Sebagian besar menjadi administrator sistem mengumpulkan informasi yang akurat tentang server dan infrastruktur. Ada sejumlah tool dan opsi untuk mengumpulkan dan memproses informasi jenis ini. Banyak dari mereka dibangun di atas teknologi yang disebut SNMP.
SNMP adalah singkatan dari simple network management protocol. Ini adalah cara server dapat berbagi informasi tentang keadaan server saat ini, dan juga saluran di mana seorang administrator dapat mengubah pre-defined value. Sementara protokol itu sendiri ringan, struktur program yang mengimplementasikan SNMP dapat berkembang menjadi kompleks.
Dalam panduan ini, kita akan mengatur tool untuk berkomunikasi menggunakan SNMP. Kita akan menggunakan dua server Ubuntu 20.04 untuk mendemokan. Satu akan berisi SNMP manager, yang akan berbicara dengan agen untuk mengimplementasikan network device. Komputer tempat SNMP manager akan disebut server manajer. Server yang lain akan memiliki SNMP agent yang akan bertindak atas perintah dari server manajer. Ini akan disebut server agen. Kita dapat memilih untuk menginstal agen pada mesin manajer juga, tetapi memisahkannya membuatnya lebih mudah untuk menunjukkan fungsionalitas apa yang disediakan oleh masing-masing komponen.
Pra-Syarat
Ini tutorial ini kita akan membutuhkan:
- Dua Ubuntu 20.04 server kosong.
- Ada non-root user dengan sudo privileges.
- Firewall yang di konfigurasi dengan ufw.
Step 1 — Installing the SNMP Daemon and Utilities
Masuk ke manager server, dengan perintah,
ssh your_username@manager_server_ip_address
Lakukan,
sudo apt update sudo apt -y install snmp snmp-mibs-downloader
Paket snmp menyediakan koleksi tool CLI untuk mengirim SNMP request ke agen. Paket snmp-mibs-downloader akan membantu untuk menginstal dan mengelola file Management Information Base (MIB), yang melacak network object.
Masuk ke server agent, dengan perintah
ssh your_username@agent_server_ip_address
Lakukan
sudo apt update sudo apt -y install snmpd
Perhatikan bahwa kita tidak memerlukan paket snmp-mibs-downloader, karena agent server tidak akan mengelola file MIB.
Step 2 — Konfigurasi SNMP Manager Server
Seperti disebutkan sebelumnya, sebagian besar pekerjaan terjadi di agent server, sehingga konfigurasi kita pada server manajer akan tidak berat. Kita hanya perlu memodifikasi satu file untuk memastikan bahwa SNMP tool dapat menggunakan data MIB tambahan yang kita instal.
Pada manager server, buka /etc/snmp/snmp.conf
sudo viu /etc/snmp/snmp.conf
Pada file ini ada bebera comments dan single un-commented line. Untuk mengijinkan manager untuk import MIB file, comment out mibs :
# As the snmp packages come without MIB files due to license reasons, loading # of MIBs is disabled by default. If you added the MIBs you can reenable # loading them by commenting out the following line. mibs :
Save dan tutup snmp.conf
Step 3 — Konfigurasi SNMP Agent Server
As a true client-server system, the agent server does not have any of the external tools needed to configure its own SNMP setup. You can modify some configuration files to make some changes, but most of the changes you need to make will be done by connecting to your agent server from your management server.
In this tutorial, you will use version 3 of the SNMP protocol. Unlike SNMPv1 and v2, in SNMPv3 each message contains security parameters that are encoded. In this step you will configure SNMPv3 authentication and access control rules.
To get started, on your agent server, open the daemon’s configuration file with sudo privileges:
sudo nano /etc/snmp/snmpd.conf
Inside, you will have to make a few changes. These will mainly be used to bootstrap your configuration so that you can manage it from your other server.
First, you need to change the agentAddress directive. Currently, it is set to only allow connections originating from the local computer. You’ll need to comment out the current line, and uncomment the line underneath, which allows all connections. /etc/snmp/snmpd.conf
- Listen for connections from the local system only
- agentAddress udp:127.0.0.1:161
- Listen for connections on all interfaces (both IPv4 *and* IPv6)
agentAddress udp:161,udp6:[::1]:161
Note: Since allowing all connections like this is not a security best practice, it is best to make sure to lock this back down soon, after the bootstraping is complete.
Next, you will temporarily insert a createUser line. These directives are not normally kept in this file; you will be removing it again in a moment.
The user you are creating will be called bootstrap and will be used as a template in which to create your first actual user. The SNMP packages do this through a process of cloning the user’s properties.
When defining a new user, you must specify the authentication type (MD5 or SHA) as well as supply a passphrase that must be at least eight characters. If you plan on using encryption for the transfer, like you will in this tutorial, you also must specify the privacy protocol (DES or AES) and optionally a privacy protocol passphrase. If no privacy protocol passphrase is supplied, the authentication passphrase will be used for the privacy protocol as well.
Add this createUser line to the end of the file: /etc/snmp/snmpd.conf
... createUser bootstrap MD5 temp_password DES
Now that you have a new user specified, you can set up the level of access that this user will have. In this tutorial you will set this up for your bootstrap user, and also for the new user you will be creating, called demo. You will allow them read and write access by using the rwuser directive (the alternative is rouser for read-only access).
You will also enforce the use of encryption by specifying priv after your user. If you wanted to restrict the user to a specific part of the MIB, you could specify the highest-level object identifier (OID) that the user should have access to at the end of the line.
For this tutorial’s purposes, both of your lines will be as follows: /etc/snmp/snmpd.conf
... rwuser bootstrap priv rwuser demo priv
When you are finished making these changes, save and close the file.
To implement these changes, restart the snmpd service on your agent server:
sudo systemctl restart snmpd
The SNMP daemon will listen for connections on port :161. Configure UFW to allow connections from the manager server to this port:
sudo ufw allow from manager_server_ip_address to any port 161
You can learn more about UFW in How To Set Up a Firewall with UFW on Ubuntu 18.04.
Now that the agent server is configured, you can connect to your agent server from the manager server to verify the connection.
Step 4 — Verifying Authentication to the Agent Server
In this step, you will test to make sure you can connect with your bootstrap account to the agent server. Before that, however, this tutorial will talk a bit about the general structure of sending an SNMP command.
When using the suite of tools included in the snmp package (the net-snmp software suite), there are a few patterns in the way you must call the commands. The first thing to do is authenticate with the SNMP daemon that you wish to communicate with. This usually involves supplying a few pieces of information. The common ones are as follows:
-v: This flag is used to specify the version of the SNMP protocol that you would like to use. This tutorial will be using v3. -c: This flag is used if you are using SNMP v1 or v2-style community strings for authentication. Since you are using v3-style user-based authentication, you don’t need to do this. -u: This parameter is used to specify the username that you wish to authenticate as. To read or modify anything using SNMP, you must authenticate with a known username. -l: This is used to specify the security level that you are connecting with. The possible values are noAuthNoPriv for no authentication and no encryption, authNoPriv for authentication but no encryption, and authPriv for authentication and encryption. The username that you are using must be configured to operate at the security level you specify, or else the authentication will not succeed. -a: This parameter is used to specify the authentication protocol that is used. The possible values are MD5 or SHA. This must match the information that was specified when the user was created. -x: This parameter is used to specify the encryption protocol that is used. The possible values are DES or AES. This must match the information that was specified when the user was created. This is necessary whenever the user’s privilege specification has priv after it, making encryption mandatory. -A: This is used to give the authentication passphrase that was specified when the user was created. -X: This is the encryption passphrase that was specified when the user was created. If none was specified but an encryption algorithm was given, the authentication passphrase will be used. This is required when the -x parameter is given or whenever a user’s privilege specification has a priv after it, requiring encryption.
Using this information, you can construct your commands. Given how you set up your bootstrap user, the commands you will be using with that account will look like this:
snmp_command -u bootstrap -l authPriv -a MD5 -x DES -A temp_password -X temp_password remote_host snmp_sub_command_or_options
From your manager server, test to make sure your bootstrap account is available. Type the following to display the system information for the agent server:
snmpget -u bootstrap -l authPriv -a MD5 -x DES -A temp_password -X temp_password agent_server_ip_address 1.3.6.1.2.1.1.1.0
The 1.3.6.1.2.1.1.1.0 string is the OID that is responsible for displaying system information. It will return the output of uname -a on the remote system.
This will give the following output:
Output SNMPv2-MIB::sysDescr.0 = STRING: Linux agent 4.15.0-66-generic #75-Ubuntu SMP Tue Oct 1 05:24:09 UTC 2019 x86_64
Now that you have verified that you can authenticate to the server running the SNMP daemon, you can continue on to create your regular user account.
Step 5 — Setting Up the Regular User Account
Although you have specified the privileges for the demo user account in the snmpd.conf file, you haven’t actually created this user yet. In this step, you are going to use the bootstrap user as a template for your new user. You will do this using the snmpusm tool, which is used for user management.
On the manager server, you can create the user from the template using the snmpusm tool and the following general syntax:
snmpusm authentication_info agent_server_ip_address create new_user existing_user
Using what you know about the authentication flags you need to pass, and leveraging the user account you already have (bootstrap), you can make a user that fits the user privileges you have already defined (demo).
The command will look like this:
snmpusm -u bootstrap -l authPriv -a MD5 -x DES -A temp_password -X temp_password agent_server_ip_address create demo bootstrap
You will receive the following message:
Output User successfully created.
You now have a fully functioning user called demo on your agent server. However, it is still using the same authentication information as the bootstrap account. To increase security, you can change the password to something else. This time, you will use the demo account to authenticate. Remember, passwords must be at least eight characters long:
snmpusm -u demo -l authPriv -a MD5 -x DES -A temp_password -X temp_password agent_server_ip_address passwd temp_password new_password
You will receive the following message back:
Output SNMPv3 Key(s) successfully changed.
You can test your new credentials and password by asking the agent server how long the SNMP service has been running. You will use the snmpget command to get a single value from the agent server.
This time, take advantage of the extra MIB definitions you downloaded to ask for the value by name instead of the OID numeric ID.
snmpget -u demo -l authPriv -a MD5 -x DES -A new_password -X new_password agent_server_ip_address sysUpTime.0
You will get back a value that represents the last time that the remote SNMP daemon was restarted:
Output DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (53309) 0:08:53.09
You now have a working user account named demo. In the next step, you will simplify working with SNMP commands by configuring the client.
Step 6 — Creating a Client Configuration File
You have probably noticed by this point that the authentication details for all of your SNMP commands will be fairly static with each request. Rather than typing these in each time, you can create a client-side configuration file that will contain the credentials you are connecting with.
The client configuration file can be placed in two different locations depending on how wide-spread you wish to share it.
If you want to share your login credentials with any valid user on your management machine, you can place your configuration details into the global snmp.conf file on the manager server. You would need to open that file with sudo privileges:
sudo nano /etc/snmp/snmp.conf
If, however, you want to define the authentication credentials for your user alone, you can create a hidden .snmp directory within your user’s home directory on the manager server, and create the file there:
mkdir ~/.snmp nano ~/.snmp/snmp.conf
Regardless of your decision on where to place your configuration, the contents will be the same.
The commands that you will be using to authenticate are in the following table. In the right-hand column, you can see the directive names used to set those configuration details within the snmp.conf file: Command Flag Description Translated snmp.conf directive
-u username The SNMPv3 username to authenticate as. defSecurityName username -l authPriv The security level to authenticate with. defSecurityLevel authPriv -a MD5 The authentication protocol to use. defAuthType MD5 -x DES The privacy (encryption) protocol to use. defPrivType DES -A passphrase The authentication passphrase for the supplied username. defAuthPassphrase passphrase -X passphrase The privacy passphrase from the supplied username. defPrivPassphrase passphrase
Using this information, you can construct an appropriate snmp.conf file. For this guide, it will look like this:
snmp.conf
defSecurityName demo defSecurityLevel authPriv defAuthType MD5 defPrivType DES defAuthPassphrase new_password defPrivPassphrase new_password
When you are finished, save and close the file.
Now, you can issue commands without supplying the authentication details. You will only need the SNMP command, the host, and the command arguments.
Instead of typing:
snmpget -u demo -l authPriv -a MD5 -x DES -A new_password -X new_password agent_server_ip_address sysUpTime.0
You can type:
snmpget agent_server_ip_address sysUpTime.0
As you can see, this significantly reduces the amount of information you need to supply in each request. Next, you will remove the bootstrap account to tighten the network security.
Step 7 — Removing the Bootstrap Account
Now that your regular account is configured correctly, you can remove the insecure bootstrap account.
On your agent server, open the /etc/snmp/snmpd.conf file again with sudo privileges.
sudo nano /etc/snmp/snmpd.conf
Find and comment out (or remove) both of the lines that you previously added that reference the bootstrap user:
/etc/snmp/snmpd.conf
... #createUser bootstrap MD5 temp_password DES #rwuser bootstrap priv ...
Save and close the file.
Now, restart the SNMP daemon:
sudo systemctl restart snmpd
This will fulfill the recommendation of not having createUser directives in the normal snmpd.conf file. It will also remove privileges from that temporary user.
If you want to completely remove the bootstrap user from the usmUserTable, you can do so by issuing this command from the manager server:
snmpusm agent_server_ip_address delete bootstrap
You will receive the following response:
Output User successfully deleted.
Conclusion
At this point, you have a fully configured client-server setup that can communicate securely using the SNMP protocol. You can now add additional daemons on other hosts and configure account access across your entire infrastructure.
For further study, you can use our How To Use the Net-SNMP Tool Suite To Manage and Monitor Servers tutorial to learn about SNMP tools and how to use them to retrieve values one-by-one or by bulk and how to modify data.