Thursday 9 May 2013

How-to: Display open ports with linux


To list all open ports on a linux box use the '-ant' option.

root@bt:~# netstat -ant
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State
tcp        0      0 127.0.0.1:7337          0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:6001            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:6002            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:6003            0.0.0.0:*               LISTEN
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0      0 172.16.0.3:22           10.242.2.6:1763         ESTABLISHED
tcp6       0      0 ::1:7337                :::*                    LISTEN
tcp6       0      0 :::5901                 :::*                    LISTEN
tcp6       0      0 :::5902                 :::*                    LISTEN
tcp6       0      0 :::5903                 :::*                    LISTEN
tcp6       0      0 :::22                   :::*                    LISTEN



If the list is huge you can grep the port number to filter out only the port you want. Here we can confirm that SSH (TCP port 22) is open (ie in the LISTEN state):

root@bt:~# netstat -ant | grep 22
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN
tcp        0     52 172.16.0.3:22           10.242.2.6:1763         ESTABLISHED
tcp6       0      0 :::22                   :::*                    LISTEN


Lastly to see the PID and the program that each socket is bound to, use the -p option.

root@bt:/# netstat -antp
Active Internet connections (servers and established)
Proto Recv-Q Send-Q Local Address           Foreign Address         State       PID/Program name
tcp        0      0 127.0.0.1:7337          0.0.0.0:*               LISTEN      1157/postgres
tcp        0      0 0.0.0.0:6001            0.0.0.0:*               LISTEN      4960/Xvnc4
tcp        0      0 0.0.0.0:6002            0.0.0.0:*               LISTEN      29698/Xvnc4
tcp        0      0 0.0.0.0:6003            0.0.0.0:*               LISTEN      28771/Xvnc4
tcp        0      0 0.0.0.0:22              0.0.0.0:*               LISTEN      1336/sshd
tcp        0    404 172.16.0.3:22           10.242.2.6:1763         ESTABLISHED 32617/0
tcp6       0      0 ::1:7337                :::*                    LISTEN      1157/postgres
tcp6       0      0 :::5901                 :::*                    LISTEN      4960/Xvnc4
tcp6       0      0 :::5902                 :::*                    LISTEN      29698/Xvnc4
tcp6       0      0 :::5903                 :::*                    LISTEN      28771/Xvnc4
tcp6       0      0 :::22                   :::*                    LISTEN      1336/sshd

 

1 comment:

Anonymous said...

if you also need to see open udp ports:

netstat -tulpn