Network Connections can be continuously monitored using netstat and ss

Netstat – derived from Network Statistics, is a command-line utility used by system administrators to track or analyze network statistics. It displays a whole range of statistics, such as open ports and corresponding addresses on the host system, route table and masked connections.
This tutorial shows how “netstat” and “ss” can be used to monitoring current network connections they tracking in near real time on Linux.
Install net-tools to use netstat
On many modern Linux distributions, netstat is replaced by the new ss utility, if it is not pre-installed, netstat can be installed afterwards. The package that contains netstat is net-tools.
$ yum install net-tools [CentOS/RHEL]
$ apt install net-tools [Debian/Ubuntu]
Track Network Connections using netstat
The netstat command runs through filters so that only the remote addresses are displayed, and with the watch command it is carried out continuously at intervals. The output shows the network statistics and connection tracking in real time of current https requests on a web server.
$ watch -n 5 "netstat -nt | grep :443 | tail -n +3 | awk '{print \$5}' | cut -d: -f1 | sort | uniq -c | sort -n"
Here the remote addresses are showing at an interval of 5 seconds for requests via https (TCP port 443).
SMTP connection tracking
If you want to track the current requests of an SMTP relay, port 25 is filtered out and then refresh every 10 seconds.
$ watch -n 10 "netstat -nt | grep :25 | tail -n +3 | awk '{print \$5}' | cut -d: -f1 | sort | uniq -c | sort -n"
Basically, the interval check is possible with any service, any ports and interval times in seconds can be selected.
Watch IPv6 network connections
The connections can also be watching using the new command-line utility, the ss command stand for – another utility to investigate sockets. Basically it directly queries the kernel and can respond much faster than netstat.
$ watch -n 3 "ss -nH | grep :443 | awk '{print \$6}' | sort | uniq -c | sort -n"
The tail
and cut
filters are no longer used here, as the ss utility has its own filter operators.
The connections for IPv4 and for IPv6 are displayed, each with an IPv4-as-IPv6 address followed by peer source port.
MultiTail advanced features along with SS
Using multitail there are other features. For example several commands can be divided into windows, as shown in the example with multitail.
$ multitail -R 3 -l "ss -nH | grep :443 | awk '{print \$6}' | sort | uniq -c | sort -n" -cS apache /var/log/apache2/access.log
The output shows the apache.log together with connections on a Debian web server. Whereby multitail is split horizontally into two windows, -R 3 specifies the interval of 3 seconds, -l for the external command, here “ss -nH” Suppress header line. The command line tool can be deployed with “apt install multitail”.
netstat and ss network connections divided
Note. Deploy Miltitail run sudo apt install multitail
Conclusion
This tutorial discusses how the netstat utility can be used to periodically run the netstat command using watch. Network statistics and connections can be continuously tracked or monitored using netstat and ss. It is also important to point out that netstat is deprecated. Instead ss Utility has taken its place to display refined network statistics. It can also be said that the “older” netstat command has been replaced by both ss and some ip commands.
You might also be interested in the related post here.