How to use netstat command in Linux with examples

Netstat command is one of tools used for troubleshooting network connectivity. When used it shows list of all open ports/sockets. In addition to connected ports/sockets it can also list listening sockets that are waiting for incoming connections.

Example of netstat command use

List all ports using netstat command

Use switch -a to list all ports listening as well as non listening ports.

Syntax

netstat -a

This command list out all ports and can make troubleshooting difficult and you have to find information you are looking for. netstat has several useful options which can make troubleshooting really helpful.

List all tcp ports using netstat command

Using -at switch will show all (listening and non listening ports) tcp ports.

Syntax

netstat -at

List all udp ports using netstat command

Using -au switch will show all (listening and non listening ports) udp ports.

Syntax

netstat -au

List listening ports using netstat command

Using -l switch will show all listening ports only  

Syntax

netstat -l

List all listening tcp ports using netstat command

Using -lt switch will show all listening ports tcp ports.

Syntax

netstat -lt

List all listening udp ports using netstat command

Using -lu switch will show all listening ports udp ports.

Syntax

netstat -lu

List all listening unix ports using netstat command

Using -lx switch will show all listening ports unix ports.

Syntax

netstat -lx

List PID and name of program for the listed ports

Using -p switch will display information of related to program name and pid for specific port. You can use -p switch with any other option of netstat

Syntax

netstat -lxp

netstat -atp

List details without hostname in netstat output

Using -n switch will display netstat information without resolving hostname, only ip address will be shown. You can use -n switch with any other option of netstat.

Syntax

netstat -lxpn

netstat -atpn

Print network statistics with netstat command

Using -s switch will display network statics for particular protocol. It will display information like packets received and packet transmitted by particular protocol.

Syntax

netstat -s

To print statistics for any particular protocol use switch -s with switch for a particular protocol like -t for tcp and -u for udp.

Syntax

netstat -st

netstat -su