Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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
add comment

4 Answers 4

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
add comment
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
add comment

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
add comment

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

telnet somehost.example.com 21

tries to connect to port 21 on somehost.example.com

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
add comment

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.