6

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?

2
  • 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
3

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
3
  • Why bash -c in second command? – enzotib Dec 14 '11 at 22:52
  • because you often can not pipe output of sudo commands directly. – Panther 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
2

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 :)

0
nmap -A -T4 localhost -p 80

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

2
  • 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 – Francisco Valdez Dec 14 '11 at 22:27
0

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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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