Follow steps below to check and change the default port (161) number on which snmp service would be listening.
NOTE: Similarly for each service there will be a port over which the service is binded to. There is a common miss conception that the port mapping is done over /etc/services file. This is just an alias mapping for the users to refer when connecting across the network. A protocol mapped again the port in the /etc/services file does not mean that the port is blocked or unblocked by a firewall or is definitely mapped again the service. It can be altered as the procedure shown below. It is a good practice to keep the file /etc/services intact as it’s a good reference point.
1. Make sure the service is started using the service command.
# service snmpd restart Stopping snmpd: [ OK ] Starting snmpd: [ OK ]
# service snmpd status snmpd (pid 14222) is running...
2. The below commands help to list the current ports over which the service would be listening
# netstat -npl | grep snmp tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 14222/snmpd udp 0 0 0.0.0.0:161 0.0.0.0:* 14222/snmpd
# lsof -Pnl +M -i4 | grep -i snmp snmpd 14222 0 9u IPv4 811629 0t0 TCP 127.0.0.1:199 (LISTEN) snmpd 14222 0 11u IPv4 811630 0t0 UDP *:161
Here,
-i4 is for ipv4
-i6 is for ipv6
3. Open the below file in any of the editor and uncomment the line starting with ‘OPTIONS‘ and add the agents to listen on. For more information can refer man page on snmpd.conf and snmpd.
# vi /etc/sysconfig/snmpd.options
# cat /etc/sysconfig/snmpd.options # snmpd command line options OPTIONS="-Lsd -Lf /dev/null -p /var/run/snmpd.pid -a -x tcp:127.0.0.1:801 udp:127.0.0.1:802"
4. Restart the service for the changes to take effect.
# service snmpd restart Stopping snmpd: [ OK ] Starting snmpd: [ OK ]
5. The below output shows the new ports its litening on.
# netstat -npl | grep snmp tcp 0 0 127.0.0.1:801 0.0.0.0:* LISTEN 14255/snmpd tcp 0 0 127.0.0.1:199 0.0.0.0:* LISTEN 14255/snmpd udp 0 0 127.0.0.1:802 0.0.0.0:* 14255/snmpd
# lsof -Pnl +M -i4 | grep -i snmp snmpd 14255 0 9u IPv4 811826 0t0 TCP 127.0.0.1:801 (LISTEN) snmpd 14255 0 11u IPv4 811827 0t0 TCP 127.0.0.1:199 (LISTEN) snmpd 14255 0 12u IPv4 811828 0t0 UDP 127.0.0.1:802
NOTE: For CentOS/RHEL 6.x version the files are /etc/sysconfig/snmpd and /etc/sysconfig/snmptrapd.