How To Install and Configure an SNMP Daemon and Client on Ubuntu 18.04

The author selected the Internet Archive to receive a donation as part of the Write for DOnations program.

Introduction

A large part of being a system administrator is collecting accurate information about your servers and infrastructure. There are a number of tools and options for gathering and processing this type of information. Many of them are built upon a technology called SNMP.

SNMP stands for simple network management protocol. It is a way that servers can share information about their current state, and also a channel through which an administer can modify pre-defined values. While the protocol itself is lightweight, the structure of programs that implement SNMP can quickly grow in complexity. For more information on the basics of the SNMP protocol, see our An Introduction to SNMP article.

In this guide, you will set up the tools to communicate using SNMP. You will be using two Ubuntu 18.04 servers to demonstrate. One will contain the SNMP manager, which will talk to the agent to implement network devices. This will be called the manager server. The other server will have the SNMP agent, which will act on the orders from the manager server. This will be called the agent server. You could choose to install the agent on the manager machine as well, but keeping them separate makes it easier to demonstrate what functionality is provided by each component.

Prerequisites

To follow this tutorial, you will need:

  • Two Ubuntu 18.04 servers set up by following the Initial Server Setup Guide for Ubuntu 18.04, including a non-root user with sudo privileges and a firewall configured with ufw.

Step 1 — Installing the SNMP Daemon and Utilities

You can begin to explore how SNMP can be implemented on a system by installing the daemon and tools on your Ubuntu servers.

From your local machine, log into the manager server as your non-root user:

Update the package index for the APT package manager:

  • sudo apt update

Next, install the SNMP software:

  • sudo apt install snmp snmp-mibs-downloader

The snmp package provides a collection of command line tools for issuing SNMP requests to agents. The snmp-mibs-downloader package will help to install and manage Management Information Base (MIB) files, which keep track of network objects.

Then, open up a new terminal on your local machine and log into the agent server:

On the agent server, update the package index:

  • sudo apt update

Then, install the SNMP daemon

  • sudo apt install snmpd

Note that you do not need the snmp-mibs-downloader package, since the agent server will not be managing MIB files.

Now that you have installed these components, you will configure your manager server.

Step 2 — Configuring the SNMP Manager Server

As mentioned before, most of the bulk of the work happens in the agent server, so your configuration on the manager server will be less involved. You just need to modify one file to make sure that SNMP tools can use the extra MIB data you installed.

On your manager server, open the /etc/snmp/snmp.conf file in your text editor with sudo privileges. This tutorial will use nano:

  • sudo nano /etc/snmp/snmp.conf

In this file, there are a few comments and a single un-commented line. To allow the manager to import the MIB files, comment out the mibs : line:

/etc/snmp/snmp.conf

# 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 and close snmp.conf by pressing CTRL+X, followed by Y, and then ENTER if you’re using nano.

You are now finished configuring the manager server, but you will still need to use this server to help configure your agent server, which you will do in the next step.

Step 3 — Configuring the 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:

OutputSNMPv2-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:

OutputUser 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:

OutputSNMPv3 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:

OutputDISMAN-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:

OutputUser 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.