Linux Netstat Command Line Tips and Tricks

netstat-network-connection-featured

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.

Ubuntu_netstat_all

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.

You should see the following output.

Ubuntu_netstat_tcp_output

Similarly, only list UDP connections using the u option.

You should see the following output.

Ubuntu_netsta_udp_output

List all listening connections

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

You should see the following output.

Ubuntu_netstat_list_output

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.

You should see the following output.

Ubuntu_netstat_dns_output

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 should see the following output.

Ubuntu_netstat_process_details_output

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

You should see the following output.

Ubuntu_netstat_process_with_uname_output

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:

You should see the following output.

Ubuntu_netstat_network_stat_outptut

Displaying IPv4 and IPv6 information

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

You should see the following output.

Ubuntu_netstat_multicast_output

Display network interface statistics

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

You should see the following output.

Ubuntu_netstat_interface_output

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

2 comments

  1. Excellent Article. Thanks for the info.

  2. I think it would be great to show some advance usage of netstat commands especially the most used/popular ones.

Comments are closed.

Sponsored Stories