Ask Ubuntu is a question and answer site for Ubuntu users and developers. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

Let's say, i'd like to know whether certain port (e.g. 21) is open or closed.

What is the best shell command for that?

share|improve this question
Are you talking about a port on your machine or a port on another machine? – Colin Newell Dec 15 '11 at 16:19
on my machine.. – DrStrangeLove Dec 15 '11 at 17:35

My favorites are:

From a local machine

sudo lsof -i -n -P 

netstat -an | grep LISTEN | grep -v ^unix

netstat -ntulp

From a remote box

nmap -v -A ip_address
share|improve this answer
Why bash -c in second command? – enzotib Dec 14 '11 at 22:52
because you often can not pipe output of sudo commands directly. – bodhi.zazen Dec 14 '11 at 23:13
Mmh, I don't understand when you cannot. Surely in your example you can (a so you should). Can you show an example when you cannot? – enzotib Dec 15 '11 at 8:34
Good point, thank you. I have edited my post. – bodhi.zazen Dec 15 '11 at 16:17

netstat will tell you this. To check for all open (listening) ports:

netstat -l

You can also show all current connections:

netstat -an

See the manpage for loads more, netstat is a very powerful tool :)

share|improve this answer
nmap -A -T4 localhost -p 80

It even shows you OS version and the service on that port

share|improve this answer
You should note that nmap isn't part of base and needs installing with apt first – Caesium Dec 14 '11 at 22:26
@Caesium you're right sudo apt-get install nmap and also nmap is used in the matrix movie – pacofvf Dec 14 '11 at 22:27

You can use telnet to try to connect to a TCP port on a give host:

telnet 21

tries to connect to port 21 on

If the port is closed you get a message like

telnet: Unable to connect to remote host: Connection refused

otherwise you'll see whatever the program listening on that port tells you on connect.

share|improve this answer

Your Answer


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.