Linux Netstat Command Line Tips and Tricks

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.

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

Simply run the netstat command with the -a option.

netstat -a

You should see the following output.

Ubuntu_netstat_all

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

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

netstat -at

You should see the following output.

Ubuntu_netstat_tcp_output

Similarly, only list UDP connections using the u option.

netstat -au

You should see the following output.

Ubuntu_netsta_udp_output

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

netstat -l

You should see the following output.

Ubuntu_netstat_list_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.

netstat -ant

You should see the following output.

Ubuntu_netstat_dns_output

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.

netstat -nlpt

You should see the following output.

Ubuntu_netstat_process_details_output

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

netstat -ltpe

You should see the following output.

Ubuntu_netstat_process_with_uname_output

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:

netstat -s

You should see the following output.

Ubuntu_netstat_network_stat_outptut

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

netstat -g

You should see the following output.

Ubuntu_netstat_multicast_output

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

netstat -ie

You should see the following output.

Ubuntu_netstat_interface_output

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

Leave a Reply

Yeah! You've decided to leave a comment. That's fantastic! Check out our comment policy here. Let's have a personal and meaningful conversation.