How do I find out whether a port is available on Ubuntu 8.04?

Is there any command I can run from bash that will tell me whether a port is already open?


Solution 1:

Use "netstat" to check the presently using ports.

netstat -antp

Proto Recv-Q Send-Q Local Address        Foreign Address   State    PID/Program name
tcp        0      0 xxx.xxx.xxx.xxx      0.0.0.0:*         LISTEN   16297/named     
tcp        0      0 xxx.xxx.xxx.xxx:53   0.0.0.0:*         LISTEN   16297/named     
tcp        0      0 xxx.xxx.xxx.xxx:53   0.0.0.0:*         LISTEN   16297/named     
tcp        0      0 127.0.0.1:53         0.0.0.0:*         LISTEN   16297/named     

Solution 2:

This (netstat) is the fastest solution...

netstat -lnt

...but this gives you more control (at the cost of speed (sometimes a lot of speed))...

lsof -n -i -a -u www-data

The above example for example shows you all the TCP ports open and in the LISTEN state, AND (-a) belonging to the Apache (www-data) user.