How To Check Which Port Is Listern or Open on Linux

Posted by Planet Malaysia on September 12, 2008


For some security reason you may configure SSH or any other protocol using different kind of port number on Linux server. Sometimes it’s important to know which ports are listern or open to the system network, it may open for network instruction or hacking.

Basically there are few methods to see which ports are open on Linux.

“How to check and open ports in Linux”

Option 1:
Check /etc/services file
planetmy:/ # cat /etc/services | grep xxx (xxx = port number)

If the command return no output mean no port configure to listen on the particular port number. For port SSH/22, you should be able to see:
ssh 22/tcp # SSH Remote Login Protocol
ssh 22/udp # SSH Remote Login Protocol

Option 2:
Use netstat command – Print network connections, routing tables, interface statistics, masquerade connections, and multicast memberships.

planetmy:/ # netstat -nan | grep 22
tcp 0 0* LISTEN 7110/sshd

If the command output return ‘LISTEN’, mean the particular port is open or listen on network.

Option 3:
use lsof command – list open files

planetmy:/ # lsof -i -n -P|grep 631
cupsd 17934 lp 0u IPv4 56540196 TCP *:631 (LISTEN)
cupsd 17934 lp 2u IPv4 56540197 UDP *:631

Option 4:
use nmap command – Network exploration tool and security scanner

planetmy:/ # nmap -sS -O
Starting nmap 3.50 ( ) at 2008-09-12 10:13 GMT
Interesting ports on
(The 1655 ports scanned but not shown below are in state: closed)
22/tcp open ssh
111/tcp open rpcbind
427/tcp open svrloc
631/tcp open ipp
Device type: general purpose
Running: Linux 2.4.X|2.5.X
OS details: Linux Kernel 2.4.0 – 2.5.20, Linux Kernel 2.4.18 – 2.5.70 (X86)
Nmap run completed — 1 IP address (1 host up) scanned in 4.146 seconds

The output show the system is running SSH on port 22.

Option 5:
use telnet command – user interface to the TELNET protocol

planetmy:/ # telnet 22
Connected to
Escape character is ‘^]’.

The output show as above mean SSH port 22 is listening on the network

planetmy:/ # telnet 122
telnet: connect to address Connection refused

The output show as above mean port 122 is closed.

Lastly, to make it more perfect, you can get a script as example below:

PORT=:22 #The port number
INITS=sshd #The name of the service in /etc/init.d/
COUNT=$(netstat -lpn | grep $ | wc -l)
if [ $COUNT -lt 1 ]
/etc/init.d/$INITS restart

Possibly Related Posts:


6 Responses to “How To Check Which Port Is Listern or Open on Linux”

  1. Wing Loon on September 13th, 2008 6:06 pm

    Correct me if I am wrong. Option 1 is just a reference file. It can’t determine whether a program is listening on that port.

    If the number is not in the /etc/services file, a program can still run as daemon and listen to whatever port numbers.

  2. deepak on September 14th, 2008 2:26 am


    Can anybody tell me that how to redirect output for ‘scp’ command, as it created a file when I redirect it, but the file is empty,

    I want to log network speed.


  3. deepak on September 14th, 2008 2:27 am

    or Is there any other command by which I can log the network speed. ?

  4. Planet Malaysia on September 16th, 2008 11:11 am
  5. EverLight on February 17th, 2011 7:03 pm

    To check ssh port 22, you can try or , you can check any outbound tcp port with this method online.

  6. 查看Linux端口情况 | Min的技术分享 – on July 24th, 2011 1:36 pm

Leave a Reply

Planet Malaysia

  • Follow us on Twitter

  • web