Take the 2-minute tour ×
Server Fault is a question and answer site for professional system and network administrators. It's 100% free, no registration required.

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

share|improve this question

9 Answers 9

up vote 32 down vote accepted

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     
share|improve this answer
1  
"netstat -antp | grep 80" where 80 is port number to search for. –  Neutralizer May 17 '13 at 9:30

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.

share|improve this answer

All good answers.

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

If not 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

share|improve this answer

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)
share|improve this answer
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 tcp@0.0.0.0: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

share|improve this answer

lsof (list open files) is a good tool to see if a process is listening on a port

lsof -P | grep :<port-number>

netstat is a good tool for seeing if there are any active connections.

netstat -n | grep :<port-number>

share|improve this answer

You don't mention what protocol you want to use, i.e. TCP or UDP - and it's also important to realise that "port" isn't quite as granular the system supports to disambiguate sockets. E.g. if your system has multiple IP addresses then port 80 might be in use on all IP addresses (either the application has bound to "0.0.0.0" or "::" or to each IP address in succession), or it might be in use only on a subset of those IP addresses.

The best, and surest, way to determine if a port/address is free and available is to attempt to bind to it. Netcat is handy for this.

nc -l [-s a.b.c.d] -p NN

will attempt to bind to TCP port NN on (optional, the default will be all addresses) a.b.c.d. Add the -u option to do the same in UDP.

Next, to tell if the port is truly "open" as you ask - you need to start looking at potential firewall rules. Again the easiest thing is to try to connect to the port. Use netcat as above, on the server, and from a client use netcat to attempt to the connect to the port you opened.

nc [-u] a.b.c.d NN

will connect to port NN on a.b.c.d, using UDP if the -u flag is specified. You can then type input into the client end, and it should show up on the server. If it doesn't, you need to look into system and network specific tools.

share|improve this answer

I use fuser (in package psmisc):

fuser -n tcp PORT

Brings back the pid of process bound to this port.

If this is to know if a port is listening, the good old telnet does the trick :)

telnet 127.0.0.1 PORT

share|improve this answer

This one-liner will get you a list of all TCP ports in use. It works in bash on Ubuntu and OS X.

netstat -ant | sed -e '/^tcp/ !d' -e 's/^[^ ]* *[^ ]* *[^ ]* *.*[\.:]\([0-9]*\) .*$/\1/' | sort -g | uniq

The list will have one port per line without any extra information.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.