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

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.


Solution 3:

All good answers.

However you don't mention if you are logged onto the computer in question. ;P

If not, then nmap is your friend.

for starters try:

nmap -Otarget

amap is also a good choice which will also attempt to guess server software by grabbing banner pages.

for starters try:

amaptarget1-6000


Solution 4:

Try

lsof -i :<port number>

If you get any results, something is listening and bound, eg

# lsof -i :80
COMMAND   PID   USER   FD   TYPE   DEVICE SIZE/OFF NODE NAME
nginx    1833 nobody    3u  IPv4 51091229      0t0  TCP odessa.cheney.net:http->79.173.188.214:52918 (ESTABLISHED)
nginx    1833 nobody    5u  IPv4 46221856      0t0  TCP odessa.cheney.net:http->66.36.243.182:37876 (CLOSE_WAIT)
nginx    1833 nobody    9u  IPv4 34733048      0t0  TCP localhost.localdomain:http (LISTEN)
nginx    1833 nobody   10u  IPv4 34733049      0t0  TCP odessa.cheney.net:http (LISTEN)
nginx    1833 nobody   14u  IPv4 46221857      0t0  TCP odessa.cheney.net:http->66.36.243.182:37880 (CLOSE_WAIT)
nginx    1833 nobody   15u  IPv4 51091030      0t0  TCP odessa.cheney.net:http->msnbot-65-55-106-132.search.msn.com:51708 (ESTABLISHED)
nginx   11832   root    9u  IPv4 34733048      0t0  TCP localhost.localdomain:http (LISTEN)
nginx   11832   root   10u  IPv4 34733049      0t0  TCP odessa.cheney.net:http (LISTEN)

Solution 5:

netstat -tlnp  

Show tcp ports that are listening, show numbers only (don't resolve names - makes it was faster) and show the process that is doing the listening (the p only works if you are root)

netstat -ulnp  

Show udp ports that are listening, show numbers only (don't resolve names - makes it was faster) and show the process that is doing the listening (the p only works if you are root)

netstat -unp  

Show udp ports that are open but not listening, show numbers only (don't resolve names- makes it was faster) and show the process that is doing the listening (the p only works if you are root)

netstat -an

Show all ports in use, show numbers only - don't resolve names

lsof -i <proto>@<host>:<port>

e.g

lsof -i tcp@localhost:25

to see if anything is listening on port localhost 25/TCP, or

lsof -i [email protected]:636 

to see if there are any sockets either local or remote either listening (local) or connected to (local or remote) for any host/interface

Tags:

Bash

Ubuntu

Port