Install and Configure SNMP on Ubuntu 22.04/Debian 11

1
895

In this guide, we are going to learn how to install and configure SNMP on Ubuntu 22.04/Debian 11. 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 or any other service/process that support polling over the SNMP protocol.

Install and Configure SNMP on Ubuntu 22.04/Debian 11

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

In this setup, we will install and configure SNMP on Ubuntu 22.04/Debian 11.

Install SNMP on Ubuntu 22.04/Debian 11

The SNMP packages are available on default Ubuntu 22.04/Debian 11 repositories.

You first need to update your package cache;

apt update

Once the update is done, install SNMP on Ubuntu 22.04/Debian 11.

apt install snmpd snmp libsnmp-dev

Configure SNMP on Ubuntu 22.04/Debian 11

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 22.04/Debian 11

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 22.04/Debian 11

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 or LibreNMS for example
  • OID is the optional SNMP tree to provide access to.

To configure SNMP v2c, 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.56.124:161

The above will allow SNMP agent to listen on loopback interface and on an interface IP 192.168.56.124, port 161/UDP.

Next, configure SNMP agent to allow the monitoring server (LibreNMS server in my case, with IP, 192.168.60.22) only to connect using the community string ([email protected]).

...
#
SECTION: Access Control Setup
#
This section defines who is allowed to talk to your running
snmp agent.
...
# Read-only access to everyone to the systemonly view
#rocommunity  public default -V systemonly
#rocommunity6 public default -V systemonly
rocommunity [email protected] 192.168.60.22

Save the changes 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.56.124:161      0.0.0.0:*                           4226/snmpd          
udp        0      0 127.0.0.1:161           0.0.0.0:*                           4226/snmpd

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

ufw allow from 192.168.60.22 to any port 161 proto udp comment "Allow SNMP Scan from Monitoring Server"

For IPTABLES, this is the rule;

iptables -A INPUT -s 192.168.60.22 -p udp --dport 161 -j ACCEPT -m comment --comment "Allow SNMP Checks"
cp /etc/iptables/rules.v4{,.bak}
iptables-save > /etc/iptables/rules.v4

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 [email protected] 192.168.56.124

If you received such an output, then all is fine

SNMPv2-MIB::sysDescr.0 = STRING: Linux jellyfish 5.15.0-27-generic #28-Ubuntu SMP Thu Apr 14 04:55:28 UTC 2022 x86_64
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (675) 0:00:06.75
SNMPv2-MIB::sysContact.0 = STRING: Me 
SNMPv2-MIB::sysName.0 = STRING: jellyfish
SNMPv2-MIB::sysLocation.0 = STRING: Sitting on the Dock of the Bay
SNMPv2-MIB::sysServices.0 = INTEGER: 72
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORID.1 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.2 = OID: SNMP-MPD-MIB::snmpMPDCompliance
SNMPv2-MIB::sysORID.3 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORID.4 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID.6 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.7 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.8 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.9 = OID: SNMP-NOTIFICATION-MIB::snmpNotifyFullCompliance
SNMPv2-MIB::sysORID.10 = OID: NOTIFICATION-LOG-MIB::notificationLogMIB
SNMPv2-MIB::sysORDescr.1 = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.3 = STRING: The management information definitions for the SNMP User-based Security Model.
SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.5 = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORDescr.6 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.7 = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr.8 = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr.9 = STRING: The MIB modules for managing SNMP Notification, plus filtering.
SNMPv2-MIB::sysORDescr.10 = STRING: The MIB module for logging SNMP Notifications.
SNMPv2-MIB::sysORUpTime.1 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.2 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORUpTime.3 = Timeticks: (0) 0:00:00.00
...

If you receive, Timeout: No Response from 192.168.56.124, then check your agent firewall as well as SNMP access control.

Configure SNMP Version 3 on Ubuntu 22.04/Debian 11

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]

On Debian 11, due to some “bug” with the net-snmp-create-v3-user script, you may see such an error as touch: cannot touch ‘/snmp/snmpd.conf’: No such file or directory.

Thus, before you generate the SNMP v3 authentication credentials, run the command below to update the paths;

cp /usr/bin/net-snmp-create-v3-user ~/
sed -ie '/prefix=/adatarootdir=${prefix}\/share' /usr/bin/net-snmp-create-v3-user

Next, generate the authentication user.

net-snmp-create-v3-user -ro -A [email protected] -a SHA -X [email protected] -x AES snmpadmin
adding the following line to /var/lib/snmp/snmpd.conf:
   createUser snmpadmin SHA "[email protected]" AES "[email protected]"
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. Note that we ran this command from the SNMP monitoring server.

snmpwalk -v3 -a SHA -A [email protected] -x AES -X [email protected] -l authPriv -u snmpadmin 192.168.56.124
SNMPv2-MIB::sysDescr.0 = STRING: Linux jellyfish 5.15.0-27-generic #28-Ubuntu SMP Thu Apr 14 04:55:28 UTC 2022 x86_64
SNMPv2-MIB::sysObjectID.0 = OID: NET-SNMP-MIB::netSnmpAgentOIDs.10
DISMAN-EVENT-MIB::sysUpTimeInstance = Timeticks: (3436) 0:00:34.36
SNMPv2-MIB::sysContact.0 = STRING: Me 
SNMPv2-MIB::sysName.0 = STRING: jellyfish
SNMPv2-MIB::sysLocation.0 = STRING: Sitting on the Dock of the Bay
SNMPv2-MIB::sysServices.0 = INTEGER: 72
SNMPv2-MIB::sysORLastChange.0 = Timeticks: (0) 0:00:00.00
SNMPv2-MIB::sysORID.1 = OID: SNMP-FRAMEWORK-MIB::snmpFrameworkMIBCompliance
SNMPv2-MIB::sysORID.2 = OID: SNMP-MPD-MIB::snmpMPDCompliance
SNMPv2-MIB::sysORID.3 = OID: SNMP-USER-BASED-SM-MIB::usmMIBCompliance
SNMPv2-MIB::sysORID.4 = OID: SNMPv2-MIB::snmpMIB
SNMPv2-MIB::sysORID.5 = OID: SNMP-VIEW-BASED-ACM-MIB::vacmBasicGroup
SNMPv2-MIB::sysORID.6 = OID: TCP-MIB::tcpMIB
SNMPv2-MIB::sysORID.7 = OID: UDP-MIB::udpMIB
SNMPv2-MIB::sysORID.8 = OID: IP-MIB::ip
SNMPv2-MIB::sysORID.9 = OID: SNMP-NOTIFICATION-MIB::snmpNotifyFullCompliance
SNMPv2-MIB::sysORID.10 = OID: NOTIFICATION-LOG-MIB::notificationLogMIB
SNMPv2-MIB::sysORDescr.1 = STRING: The SNMP Management Architecture MIB.
SNMPv2-MIB::sysORDescr.2 = STRING: The MIB for Message Processing and Dispatching.
SNMPv2-MIB::sysORDescr.3 = STRING: The management information definitions for the SNMP User-based Security Model.
SNMPv2-MIB::sysORDescr.4 = STRING: The MIB module for SNMPv2 entities
SNMPv2-MIB::sysORDescr.5 = STRING: View-based Access Control Model for SNMP.
SNMPv2-MIB::sysORDescr.6 = STRING: The MIB module for managing TCP implementations
SNMPv2-MIB::sysORDescr.7 = STRING: The MIB module for managing UDP implementations
SNMPv2-MIB::sysORDescr.8 = STRING: The MIB module for managing IP and ICMP implementations
SNMPv2-MIB::sysORDescr.9 = STRING: The MIB modules for managing SNMP Notification, plus filtering.
SNMPv2-MIB::sysORDescr.10 = STRING: The MIB module for logging SNMP Notifications.
SNMPv2-MIB::sysORUpTime.1 = Timeticks: (0) 0:00:00.00
...

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

That brings us to the end of our tutorial on how to install and configure SNMP on Ubuntu 22.04/Debian 11.

Other Tutorials

Install LibreNMS on Rocky Linux

Easily Install and Setup Cacti on Ubuntu 20.04

1 COMMENT

LEAVE A REPLY

Please enter your comment!
Please enter your name here