Quick Way to Install and Configure SNMP on Ubuntu 20.04

|
Last Updated:
|
|

In this guide, we are going to learn how to install and configure SNMP on Ubuntu 20.04. SNMP is an acronym for Simple Network Management Protocol. It provides an agentless method of managing and monitoring of network devices and servers for health information, system metrics such as CPU load, Physical Memory usage, number of running processes, service state e.t.c that support polling over the SNMP protocol.. With this method, you don’t need to install the usual Nagios NRPE agent on your hosts.

Installing SNMP on Ubuntu 20.04

Before you can monitor Linux hosts via SNMP using monitoring tools like Nagios or Cacti, you first need to install and configure SNMP.

Install SNMP

The SNMP packages are available on default Ubuntu 20.04 repositories.

You first need to update your package cache;

apt update

Once the update is done, install SNMP on Ubuntu 20.04.

apt install snmpd snmp libsnmp-dev

Configure SNMP on Ubuntu 20.04

Once the installation is done, proceed to configure SNMP as follows.

The default configuration file for SNMP is /etc/snmp/snmpd.conf. The file is highly commented and thus, we will only make a few changes. As a result, make a copy of the original file before you can proceed.

cp /etc/snmp/snmpd.conf{,.bak}

Configuring SNMP Authentication on Ubuntu 20.04

SNMP supports three versions of SNMP protocol;

  • SNMP v1
  • SNMP v2c
  • SNMP v3

Both SNMP version 1 and 2c provides authentication using community string, which is a shared secret between the agent and the client that is passed in clear text over the network

SNMP version 3 supports user authentication and message encryption using a variety of protocols and is thus a bit more secure.

Configuring SNMP Version 2c Community on Ubuntu 20.04

As stated above, SNMP v2 provides access using a permission directive, a community string and the source Address. The source address can be IP of the monitoring server such as Nagios Server (SNMP server) or Cacti for example. This directive should be set in the format;

directive community [source [OID]]

Where

  • directive can be rocommunity (provides read-only access) or rwcommunity (provides read-write access)
  • source can be IP of the monitoring server such as Nagios Server (SNMP server) or Cacti for example
  • OID is the optional SNMP tree to provide access to. To configure SNMP v2c

Therefore, edit the snmpd configuration file and configure it to listen on both a localhost and an interface IP. By default, SNMP agent on is set to allow connections originating from the localhost only. 

vim /etc/snmp/snmpd.conf

...
###########################################################################
# SECTION: Agent Operating Mode
#
#   This section defines how the agent will operate when it
#   is running.
...
#agentaddress  127.0.0.1,[::1]
agentAddress udp:127.0.0.1:161,udp:192.168.58.18:161

Also, configure it to allow the monitoring server (Cacti server in my case, with IP, 192.168.58.3) only to connect using the community string (Ex@mPL3).


...
#
SECTION: Access Control Setup
#
This section defines who is allowed to talk to your running
snmp agent.
...
rocommunity Ex@mPL3 192.168.58.3

Save and exit the configuration file.

Note that for any changes to the configuration file to take effect, force the snmpd service to re-read the configuration by running the following command:

systemctl restart snmpd

If you can check, SNMP is now listening on two interfaces;

netstat -nlpu|grep snmp
udp        0      0 192.168.58.18:161       0.0.0.0:*                           36473/snmpd         
udp        0      0 127.0.0.1:161           0.0.0.0:*                           36473/snmpd

If firewall is running, allow connection from the monitoring server.

ufw allow from 192.168.58.3 to any port 161 proto udp

Verify SNMP v2c Connection with Community String

Next, run the command below from the monitoring server to verify access to the host;

snmpwalk -v2c -c Ex@mPL3 192.168.58.18

iso.3.6.1.2.1.1.1.0 = STRING: "Linux ubuntu20.kifarunix-demo.com 5.4.0-51-generic #56-Ubuntu SMP Mon Oct 5 14:28:49 UTC 2020 x86_64"
iso.3.6.1.2.1.1.2.0 = OID: iso.3.6.1.4.1.8072.3.2.10
iso.3.6.1.2.1.1.3.0 = Timeticks: (2891) 0:00:28.91
iso.3.6.1.2.1.1.4.0 = STRING: "Me <[email protected]>"
iso.3.6.1.2.1.1.5.0 = STRING: "ubuntu20.kifarunix-demo.com"
iso.3.6.1.2.1.1.6.0 = STRING: "Sitting on the Dock of the Bay"
iso.3.6.1.2.1.1.7.0 = INTEGER: 72
iso.3.6.1.2.1.1.8.0 = Timeticks: (2) 0:00:00.02
iso.3.6.1.2.1.1.9.1.2.1 = OID: iso.3.6.1.6.3.10.3.1.1
iso.3.6.1.2.1.1.9.1.2.2 = OID: iso.3.6.1.6.3.11.3.1.1
iso.3.6.1.2.1.1.9.1.2.3 = OID: iso.3.6.1.6.3.15.2.1.1
iso.3.6.1.2.1.1.9.1.2.4 = OID: iso.3.6.1.6.3.1
iso.3.6.1.2.1.1.9.1.2.5 = OID: iso.3.6.1.6.3.16.2.2.1
iso.3.6.1.2.1.1.9.1.2.6 = OID: iso.3.6.1.2.1.49
iso.3.6.1.2.1.1.9.1.2.7 = OID: iso.3.6.1.2.1.4
iso.3.6.1.2.1.1.9.1.2.8 = OID: iso.3.6.1.2.1.50
iso.3.6.1.2.1.1.9.1.2.9 = OID: iso.3.6.1.6.3.13.3.1.3
iso.3.6.1.2.1.1.9.1.2.10 = OID: iso.3.6.1.2.1.92
iso.3.6.1.2.1.1.9.1.3.1 = STRING: "The SNMP Management Architecture MIB."
iso.3.6.1.2.1.1.9.1.3.2 = STRING: "The MIB for Message Processing and Dispatching."

Configure SNMP Version 3 on Ubuntu 20.04

SNMP v3 uses a username, permission, security level, authentication and privacy passphrases to allow access. As a result, you need to create user for authenticating. When created, the user is added to the following configuration files; /etc/snmp/snmpd.conf and /var/lib/net-snmp/snmpd.conf.

Similarly, make a copy of the original configuration file just like we did above.

cp /etc/snmp/snmpd.conf{,.bak}

Stop SNMP daemon

systemctl stop snmpd

Create SNMP v3 Authentication User

Create a read-only authentication user using the net-snmp-create-v3-user command. The command syntax is;

net-snmp-create-v3-user [-ro] [-A authpass] [-a MD5|SHA] [-X privpass][-x DES|AES] [username]

net-snmp-create-v3-user -ro -A STrP@SSWRD -a SHA -X STr0ngP@SSWRD -x AES snmpadmin

adding the following line to /var/lib/snmp/snmpd.conf:
   createUser snmpadmin SHA "STrP@SSWRD" AES "STr0ngP@SSWRD"
adding the following line to /usr/share/snmp/snmpd.conf:
   rouser snmpadmin

Start SNMP daemon and configure inbound Firewall rules to UDP port 161 as we did above.

systemctl start snmpd

Enable SNMP daemon to run on system reboot.

systemctl enable snmpd

Verify SNMP v3 Connection

Test to verify that everything is working as expected.

snmpwalk -v3 -a SHA -A STrP@SSWRD -x AES -X STr0ngP@SSWRD -l authPriv -u snmpadmin localhost | head

iso.3.6.1.2.1.1.1.0 = STRING: "Linux ubuntu20.kifarunix-demo.com 5.4.0-51-generic #56-Ubuntu SMP Mon Oct 5 14:28:49 UTC 2020 x86_64"
iso.3.6.1.2.1.1.2.0 = OID: iso.3.6.1.4.1.8072.3.2.10
iso.3.6.1.2.1.1.3.0 = Timeticks: (6269) 0:01:02.69
iso.3.6.1.2.1.1.4.0 = STRING: "Me [email protected]"
iso.3.6.1.2.1.1.5.0 = STRING: "ubuntu20.kifarunix-demo.com"
iso.3.6.1.2.1.1.6.0 = STRING: "Sitting on the Dock of the Bay"
iso.3.6.1.2.1.1.7.0 = INTEGER: 72
iso.3.6.1.2.1.1.8.0 = Timeticks: (1) 0:00:00.01
iso.3.6.1.2.1.1.9.1.2.1 = OID: iso.3.6.1.6.3.10.3.1.1
iso.3.6.1.2.1.1.9.1.2.2 = OID: iso.3.6.1.6.3.11.3.1.1

From a remote host;

snmpwalk -v3 -a SHA -A STrP@SSWRD -x AES -X STr0ngP@SSWRD -l authPriv -u snmpadmin 192.168.58.18 | head

iso.3.6.1.2.1.1.1.0 = STRING: "Linux ubuntu20.kifarunix-demo.com 5.4.0-51-generic #56-Ubuntu SMP Mon Oct 5 14:28:49 UTC 2020 x86_64"
iso.3.6.1.2.1.1.2.0 = OID: iso.3.6.1.4.1.8072.3.2.10
iso.3.6.1.2.1.1.3.0 = Timeticks: (11693) 0:01:56.93
iso.3.6.1.2.1.1.4.0 = STRING: "Me <[email protected]>"
iso.3.6.1.2.1.1.5.0 = STRING: "ubuntu20.kifarunix-demo.com"
iso.3.6.1.2.1.1.6.0 = STRING: "Sitting on the Dock of the Bay"
iso.3.6.1.2.1.1.7.0 = INTEGER: 72
iso.3.6.1.2.1.1.8.0 = Timeticks: (1) 0:00:00.01
iso.3.6.1.2.1.1.9.1.2.1 = OID: iso.3.6.1.6.3.10.3.1.1
iso.3.6.1.2.1.1.9.1.2.2 = OID: iso.3.6.1.6.3.11.3.1.1

Great!! You have successfully setup SNMP v2c and SNMP v3 on an Ubuntu 20.04 system. You can now configure your server for polling from a remove monitoring server using SNMP strings or with SNMP v3.

Other Tutorials

Install and Configure SNMP on CentOS 8

Install and Configure SNMP on Debian 10 Buster

How to Configure SNMP version 3 on Debian 9

How to Configure SNMP Version 2c on Debian 9

Nagios SNMP Monitoring of Linux Hosts on AlienVault USM/OSSIM

SUPPORT US VIA A VIRTUAL CUP OF COFFEE

We're passionate about sharing our knowledge and experiences with you through our blog. If you appreciate our efforts, consider buying us a virtual coffee. Your support keeps us motivated and enables us to continually improve, ensuring that we can provide you with the best content possible. Thank you for being a coffee-fueled champion of our work!

Photo of author
gen_too
Co-founder of Kifarunix.com, Linux Tips and Tutorials. Linux/Unix admin and author at Kifarunix.com.

Leave a Comment