SNMP: UBUNTU 20.04 instalasi SNMP

From OnnoWiki
Jump to navigation Jump to search

Sumber: https://www.digitalocean.com/community/tutorials/how-to-install-and-configure-an-snmp-daemon-and-client-on-ubuntu-18-04


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

Sebagai sistem client-server yang sebenarnya, agent server tidak memiliki tool eksternal / tambahan yang diperlukan untuk mengkonfigurasi SNMP setup-nya. Kita dapat memodifikasi beberapa file konfigurasi untuk membuat beberapa perubahan, tetapi sebagian besar perubahan yang perlu kita lakukan adalah untuk menghubungkan ke agent server ke management server.

Dalam tutorial ini, kita akan menggunakan SNMP versi 3. Tidak seperti SNMPv1 dan v2, di SNMPv3 setiap pesan berisi parameter keamanan yang disandikan. Pada langkah ini kita akan mengonfigurasi aturan SNMPv3 otentikasi dan kontrol akses.

Untuk memulainya, pada agent server, bukan konfigurasi snmpd.conf sebagai berikut

sudo vi /etc/snmp/snmpd.conf

Kita akan melakukan beberapa perubahan kecil. Ini akan digunakan untuk mem-bootstrap konfigurasi agar bisa memanage server dari server lain.

SATU

Ubah agentAddress agar bisa di akses dari komputer lain

/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

Setelah selesai melakukan bootstraping, sebaiknya di set hanya localhost yang bisa akses.

DUA

Mendefinisikan pengguna. Pengguna yang kita buat akan disebut bootstrap dan akan digunakan sebagai template untuk membuat pengguna pertama. Paket SNMP melakukan ini melalui proses kloning properti pengguna.

Saat mendefinisikan pengguna baru, kita harus menentukan jenis otentikasi (MD5 atau SHA) serta memberikan frasa sandi yang paling tidak harus delapan karakter. Jika kita berencana menggunakan enkripsi untuk transfer, kita harus menentukan protokol privasi (DES atau AES) dan secara opsional frasa sandi protokol privasi. Jika tidak ada frasa sandi protokol privasi yang diberikan, frasa sandi otentikasi juga akan digunakan untuk protokol privasi.

Tambahkan createUser line di akhir file:

/etc/snmp/snmpd.conf

...
createUser bootstrap MD5 temp_password DES

Sekarang kita memiliki pengguna baru yang ditentukan, kita dapat mengatur tingkat akses yang akan dimiliki oleh pengguna ini. Dalam tutorial ini kita akan mengatur ini untuk pengguna bootstrap, dan juga untuk pengguna baru yang akan kita buat, yang disebut demo. Kita akan mengijinkan mereka read dan write akses dengan menggunakan petunjuk rwuser (alternatifnya adalah rouser untuk read-only access).

Kita juga akan menegakkan penggunaan enkripsi dengan menentukan priv setelah pengguna. Jika kita ingin membatasi pengguna ke bagian tertentu dari MIB, kita bisa menentukan highest-level object identifier (OID) yang bisa diakses pengguna di akhir baris.

Dalam tutorial ini, ke dua baris yang dibutuhkan adalah sebagai berikut,

/etc/snmp/snmpd.conf
...
rwuser bootstrap priv
rwuser demo priv

Setelah sudah konfigurasi snmpd.conf, save & close file.

TIGA - Restart

sudo systemctl restart snmpd

SNMP daemon akan mendengarkan port 161. Konfigurasi UFW untuk mengijinkan sambungan port 161

sudo ufw allow from manager_server_ip_address to any port 161

Step 4 — Verify Authentication ke Agent Server

Selanjutnya test apakah kita bisa menyambungkan diri ke agent server dengan bootstrap account. Dalam net-snmp software suite kita mengenal beberapa pola (switch / flag) yang bisa digunakan, beberapa yang sering digunakan adalah,

  • -v: men-spesifikasi versi SNMP, disini digunakan v3.
  • -c: men-spesifikasi community string di SNMP v1 atau v2 untuk authentikasi. Karena kita menggunakan v3, maka ini tidak dibutuhkan.
  • -u: username untuk authentikasi. Untuk read & modify, kita harus ter-authentikasi ke username yang sudah di set.
  • -l: set security level. Nilai-nya bisa noAuthNoPriv untuk no authentication & no encryption, authNoPriv untuk authentication tapi no encryption, dan authPriv untuk authentication & encryption. Username yang digunakan harus di konfigurasi untuk level security yang diinginkan tersebut.
  • -a: set authentication protocol yang digunakan. Nilainya bisa MD5 atau SHA. Ini harus match dengan yang di set ke user.
-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.

Referensi

Pranala Menarik