Linux

How to Install and use netstat on Ubuntu 20.04

Below error indicates that Netstat is not currently installed on your system. Not to worry; we’ll guide you through the installation process step by step in this post.

root@myserver:~# netstat

Command ‘netstat’ not found, but can be installed with:

apt install net-tools

Network Statistics short for “Netstat“, is a command-line tool that provides detailed information about network connections, routing tables, interface statistics, masquerade connections etc.

Netstat is highly used by network administrator, a system administrator, or just a curious user.

In this tutorial we will try to understand how to install and use Netstat effectively, by the end of this post we will have an idea on which netstat command to use for diagnosing network issues and monitor the system’s network activity.

Let’s start the process of installing Netstat on Ubuntu 20.04

1. Login to the Ubuntu server and become a root user

# sudo su

2. Update the server

Before installing any new software, it’s a good practice to update your package lists to ensure you’re getting the latest available updates from the repo.

Run the below command and Press ‘Y’ to start the Update && Upgrade process

# apt update && apt upgrade

root@cloudscope:/# apt update && apt upgrade
Hit:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu focal InRelease
Get:2 http://us-east-1.ec2.archive.ubuntu.com/ubuntu focal-updates InRelease [114 kB]
Get:3 http://us-east-1.ec2.archive.ubuntu.com/ubuntu focal-backports InRelease [108 kB]
Get:4 http://security.ubuntu.com/ubuntu focal-security InRelease [114 kB]
Get:5 http://us-east-1.ec2.archive.ubuntu.com/ubuntu focal/universe amd64 Packages [8628 kB]
Get:6 http://us-east-1.ec2.archive.ubuntu.com/ubuntu focal/universe Translation-en [5124 kB]
Get:7 http://us-east-1.ec2.archive.ubuntu.com/ubuntu focal/universe am

Also Read: How to install netcat

3. Install Netstat

Install netstat and its dependencies using the below command, This command will install the Net-tools package, which includes Netstat.

# apt install net-tools

root@cloudscope:~# apt install net-tools
Reading package lists… Done
Building dependency tree
Reading state information… Done
The following NEW packages will be installed:
net-tools
0 upgraded, 1 newly installed, 0 to remove and 0 not upgraded.
Need to get 196 kB of archives.
After this operation, 864 kB of additional disk space will be used.
Get:1 http://us-east-1.ec2.archive.ubuntu.com/ubuntu focal/main amd64 net-tools amd64 1.60+git20180626.aebd88e-1ubuntu1 [196 kB]
Fetched 196 kB in 0s (8122 kB/s)
Selecting previously unselected package net-tools.
(Reading database … 90754 files and directories currently installed.)
Preparing to unpack …/net-tools_1.60+git20180626.aebd88e-1ubuntu1_amd64.deb …
Unpacking net-tools (1.60+git20180626.aebd88e-1ubuntu1) …
Setting up net-tools (1.60+git20180626.aebd88e-1ubuntu1) …
Processing triggers for man-db (2.9.1-1) …

4. Verify Installation

# dpkg -L net-tools

root@cloudscope:~# dpkg -L net-tools
/.
/bin
/bin/netstat
/sbin
/sbin/ifconfig
/sbin/ipmaddr
/sbin/iptunnel
/sbin/mii-tool
/sbin/nameif
/sbin/plipconfig
/sbin/rarp
/sbin/route
/sbin/slattach
/usr
/usr/sbin
/usr/sbin/arp
/usr/share
/usr/share/doc
/usr/share/doc/net-tools
/usr/share/doc/net-tools/NEWS.Debian.gz
/usr/share/doc/net-tools/README
/usr/share/doc/net-tools/TODO
/usr/share/doc/net-tools/changelog.Debian.gz
/usr/share/doc/net-tools/copyright

5. Check Version

Run the below command to check the netstat version

# netstat --version

root@cloudscope:~# netstat –version
net-tools 2.10-alpha
Fred Baumgarten, Alan Cox, Bernd Eckenfels, Phil Blundell, Tuan Hoang, Brian Micek and others
+NEW_ADDRT +RTF_IRTT +RTF_REJECT +FW_MASQUERADE +I18N +SELINUX
AF: (inet) +UNIX +INET +INET6 +IPX +AX25 +NETROM +X25 +ATALK +ECONET +ROSE -BLUETOOTH
HW: +ETHER +ARC +SLIP +PPP +TUNNEL -TR +AX25 +NETROM +X25 +FR +ROSE +ASH +SIT +FDDI +HIPPI +HDLC/LAPB +EUI64

6. Using Netstat

To view a list of all active network connections, simply run the below netstat command

# netstat -ntlp

root@cloudscope:~# netstat -ntlp
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State PID/Program name
tcp 0 0.0.0.0:22 0.0.0.0:* LISTEN 12774/sshd: /usr/sb
tcp 0 0 127.0.0.53:53 0.0.0.0:* LISTEN 12948/systemd-resol
tcp6 0 0 :::22 :::* LISTEN 12774/sshd: /usr/sb

Also Read: Transfer files using netcat

We can use grep with netstat to filter out the port or protocol that we require. Suppose we need to verify if port 22 is listening or not

# netstat -ntlp | grep 22

root@cloudscope:~# netstat -ntlp | grep 22
tcp 0 0 0.0.0.0:22 0.0.0.0:* LISTEN 12774/sshd: /usr/sb
tcp6 0 0 :::22 :::* LISTEN 12774/sshd: /usr/sb

7. Other Options

To check more on the netstat options, use the help command

# netstat --help

Conclusion

Netstat is a valuable tool for network analysis. By installing and using it with the above examples, you can gain insights into your network connections, troubleshoot network issues, and enhance your system administration skills. Make Netstat your ally in managing and monitoring network activities on your Ubuntu 20.04 system.