netstat Command in Linux (9 Useful Examples)

Linux netstat is used to display information about the networking subsystems. It is available on Unix, Unix-like operating systems, and also on Windows.

In this tutorial, we will learn about Linux netstat and how to use it.

netstat command in Linux

Netstat command displays network connections, listening port information, routing tables, interface statistics.

Netstat tool is part of the net-tools package. Iproute2 emerged as a replacement for net-tools. To get netstat which is obsolete, you need to install net-tools. You can see some of the equivalent functionality of netstat using the ss and IP command.

Linux Netstat syntax:

$ netstat [Option1] [Option2] [Option3]

If you run netstat without any options it displays a list of open sockets

$ netstat

In the output you may see two lists: Active Internet and Unix domain. Active internet list the connections established to external devices whereas UNIX domain lists connection internally between operating system processes and applications.

1. List all Connections

To display all listening and non-listening sockets of TCP, UDP, and UNIX socket connections, use the following syntax.

$ netstat -a
List all Connections

The output will be pretty long, to read better use pipe with less or more. For example netstat -a | less.

2. List all TCP ports

To list all the TCP port connections, use the following syntax.

$ netstat -at
List all TCP ports

Handy command to check listening and ports and application on Linux:

$ netstat -tulpn | grep LISTEN

All the netstat options in the command are explained in the next sections.

3. List all UDP ports

To list all the UDP port connections, use the following syntax.

$ netstat -au
List all UDP ports

4. List Listening Ports

To list all the listening ports, use the netstat command with the -l option.

$ netstat -l
List Listening Ports

You can specify and display only the TCP or UDP or UNIX listening ports.

List TCP Listening Ports

To list TCP listening ports, use the following syntax.

$ netstat -lt
List TCP Listening Ports

List UDP Listening Ports

To list UDP listening ports, use the following syntax.

$ netstat -lu
List UDP Listening Ports

List UNIX Listening Ports

To list UNIX listening ports, use the following syntax.

$ netstat -lx
List UNIX Listening Ports

5. Display Summary Statistics

To display the summary statistic by protocol, use the -s option.

$ netstat -s
 Display Statistics by Protocol

You can also filter the statistics by the protocol and only display the statistics for TCP or UDP ports.

Display Statistics for TCP port

To display the statistics for TCP ports only, use the following syntax.

$ netstat -st
Display Statistics for TCP port

Display Statistics for UDP port

To display the statistics for UDP ports only, use the following syntax.

$ netstat -su
Display Statistics for UDP port

6. Display Service name with PID

To display service name with PID, use the -p option. For example, to list the UNIX connections with PID, use the following syntax.

$ netstat -xp
Display Service name with PID

List Listening Programs

To list all the listening programs, use the following syntax.

$ netstat -lp
List Listening Programs

7. Display IPv4 and IPv6 Information

To display the IPv4 and IPv6 information, use the -g option.

$ netstat -g
Display IPv4 and IPv6 Information

8. Display Interface Information

To display the interface information, use the -i option.

$ netstat -i
Display Interface Information

9. Display Kernel Routing Table

To display the kernel routing table, use the -r option.

$ netstat -r
Display Kernel Routing Table

You can add -n option to numerical display of addresses and port numbers.

Conclusion

In this tutorial, we learned about Linux netstat with useful examples. You can get more information from the Linux netstat man page.

Leave a Comment