The ‘ netstat’ command can be used to know get information like network connections, routing details, interface statistics, and also information about other connections that related to the server.
There are many netstat commands with various switches, some commonly used and effective ones are shown below with examples.
- List all listening and non-listening ports.
List all ports using netstat -a
netstat -a
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:tsrmagt *:* LISTEN
tcp 0 0 :tpcsrvr :* LISTEN
Active UNIX domain sockets (servers and established)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 3700802683 /var/lib/mysql/mysql.sock
unix 12 [ ] DGRAM 3648417819 /dev/log
unix 2 [ ACC ] STREAM LISTENING 3624753617 @/com/ubuntu/upstart
- List all TCP ports using netstat -at
netstat -at
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 *:tsrmagt *:* LISTEN
tcp 0 0 :tpcsrvr :* LISTEN
- List all UDP ports using netstat -au
root@test [~]# netstat -au
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 nixlinux.com:domain :
udp 0 0 localhost.localdomain:domain :
- List sockets which are in the listening state.
netstat -l
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :tsrmagt :* LISTEN
tcp 0 0 *:tpcsrvr *:* LISTEN
- List only listening TCP ports.
netstat -lt
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
tcp 0 0 :tsrmagt :* LISTEN
tcp 0 0 *:tpcsrvr *:* LISTEN
tcp 0 0 :imaps :* LISTEN
- List only listening UDP ports.
netstat -lu
Active Internet connections (only servers)
Proto Recv-Q Send-Q Local Address Foreign Address State
udp 0 0 nixlinux.com:domain :
udp 0 0 localhost.localdomain:domain :
- List only listening UNIX ports.
netstat -lx
Active UNIX domain sockets (only servers)
Proto RefCnt Flags Type State I-Node Path
unix 2 [ ACC ] STREAM LISTENING 3700802683 /var/lib/mysql/mysql.sock
unix 2 [ ACC ] STREAM LISTENING 3624753617 @/com/ubuntu/upstart
- Find out the port which a program is running
netstat -ap | grep mysql
tcp 0 0 :mysql :* LISTEN 1140/mysqld
unix 2 [ ACC ] STREAM LISTENING 3700802683 1140/mysqld /var/lib/mysql/mysql.sock
unix 3 [ ] STREAM CONNECTED 911426796 1140/mysqld /var/lib/mysql/mysql.sock
unix 3 [ ] STREAM CONNECTED 790932289 1140/mysqld /var/lib/mysql/mysql.sock
- Find out which all processes using a specific port.
netstat -an | grep ‘:80’
tcp 0 0 0.0.0.0:80 0.0.0.0:* LISTEN
tcp 0 0 :::80 :::* LISTEN
- Find out number of connections to a particular port.
root@test [~]# netstat -an | grep ‘:80’ | wc -l
3
Thank you 🙂