Netstat is a command line utility that can be used to monitor both incoming and outgoing network connections as well as view routing tables, interface statistics, etc. It can be used to list all the connected TCP and UDP socket connections and also list listening sockets that are waiting for incoming connections.

Netstat is available on all Unix-like operating systems. It is very useful for every system administrator to monitor and troubleshoot network-related problems and determine network traffic performance.

In this tutorial we will discuss how to use netstat to find information about network connections and open ports on a Linux system.

List all the TCP and UDP connections

The simplest way to use netstat is to list all the TCP and UDP connections.

Simply run the netstat command with the -a option.

You should see the following output.

The above command shows all the established and listening TCP and UDP socket connections

List only TCP or UDP connections

You can only list TCP connections using the -t option.

Similarly, only list UDP connections using the u option.

List all listening connections

You can list all active listening ports connections using the -l option.

Disable reverse DNS lookup for faster output

By default, the netstat command tries to find the hostname of each IP address in the connection by doing a reverse DNS lookup. This slows down the output.

You can disable reverse DNS lookup with the -n option.

List the process name and user ID

When viewing the open listening ports and connections, it’s necessary to know the process name which has opened that port or connection.

You can get process details using the -p option.

You can get the username along with process name using the e option.

List network statistics

The netstat command can also be used to print network statistics of the total number of packets received and transmitted by protocol type.

To list statistics of all packet types, run:

Displaying IPv4 and IPv6 information

You can use the -g option to display the multicast information for both IPv4 and IPv6 protocols.

Display network interface statistics

You can also print the information of your network interface. You can do this with the -i and -e options:

Conclusion

In above post we have explained most of what netstat is used for. If you are looking for more advanced information, read the netstat manual. You can also leave your feedback and suggestions in the comments box below.

Image credit: Jon ‘ShakataGaNai’ Davis via Wikimedia Commons

Our latest tutorials delivered straight to your inbox