The netstat is a command-line utility that displays network connections, routing tables, and a number of network interface and protocol statistics. It is available for Windows, Linux, IBM OS/2, Unix, BSD, Solaris, and many other platforms.
In this article, we will discuss the usage of the netstat command in Linux with some examples.
- 1 How to install netstat in Linux
- 2 Usage of netstat command in Linux
- 3 Conclusion
How to install netstat in Linux
In Linux generally, it comes bundled with the operating system and gets installed on your system when you install the operating system. If it is not installed on your system you can use one of the given commands to install it.
This command is part of the net-tools package in Linux.
To install it debian based system that includes Ubuntu, Linux Mint, etc then use –
sudo apt install net-tools -y
OR if you are using CentOS /RHEL or any other rpm based system then use –
sudo yum install -y net-tools
You can verify the installation of netstat using the given command –
This will display the version of
net-tools installed on your system.
Usage of netstat command in Linux
The most basic use of the netstat command is using it without any parameters.
This will display output something like given in the image below.
As you can see in the image the output is broadly given in two parts. The first part shows active internet connections on the system. This shows the following information.
- Proto – Protocol of the connection its value could be TCP, UDP, TCP6, etc.
- Recv-Q – It shows bytes received or ready to be received.
- Send-Q – Send queue of bytes ready to be sent.
- Local address – This shows the address details and port on the local machine.
- Foreign address– It shows address details and port of the remote end of the connection. You will see an asterisk (*) if a port is not yet established.
- State – State of the local socket, most commonly ESTABLISHED, LISTENING, CLOSED, or blank.
The second part of the output shows all the active “Unix Domain” open sockets with the following details:
- Proto – Protocol used by the socket the value will always be Unix
- RefCnt – Reference count of the number of attached processes to this socket.
- Flags – Usually this is ACC or blank.
- Type – This shows the type of socket.
- State – State of the socket, most often CONNECTED, LISTENING, or blank.
- I-Node – File system inode (index node) associated with this socket.
- Path – System path to the socket.
How to list all the ports and connections
You can use the given command to display the list of ports and connections regardless of the protocols that they use.
netstat -a | less
You can scroll and see the whole output of this command.
How to list only TCP port connections
By using the option
-t with netstat command you display TCP connections only. Now use the given command –
You can see the output of this in the given image –
How to list only UDP port connections
Similar to TCP if you use the option
-u with the netstat command you display UDP connections only. Execute the given command in your terminal.
You can see the output of this command in the given image.
How to display the list of all actively listening ports
If you want to display all the actively listening ports on your system then use the given command.
You can see the output of this in the given image.
Display statistics by protocol
By using the option
-s with netstat command you can pull and display network statistics by protocols.
You can see the output in the given image.
You can use the filter for a specific protocol for example to display information TCP port use the option
-t with the above command.
How to view I/O by interface
To view send/receive stats by the interface you need to use the option
-i with the netstat command.
This will display output as given in the image below.
There are several other usages of the netstat command. You can see a detailed list of options that you can use with it on its manual page.
netstat command is an easy-to-use and powerful tool that anyone can use. I hope you understand how to use it on a Linux system.
Now if you have a query then write us in the comments below.