Monday, September 27, 2010

How To Check Which Port Is Listening or Open on Linux

How To Check Which Port Is Listening or Open on Linux

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 actually listen 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.

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 multi cast memberships.

planetmy:/ # netstat -nan | grep 22
tcp 0 0 0.0.0.0:22 0.0.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 192.168.1.2
Starting nmap 3.50 ( http://www.insecure.org/nmap/ ) at 2008-09-12 10:13 GMT
Interesting ports on 192.168.1.2:
(The 1655 ports scanned but not shown below are in state: closed)
PORT STATE SERVICE
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 192.168.1.2 22
Trying 192.168.1.2?
Connected to 192.168.1.2.
Escape character is ?^]?.
SSH-1.99-OpenSSH_4.2

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

planetmy:/ # telnet 192.168.1.2 122
Trying 192.168.1.2?
telnet: connect to address 192.168.1.2: Connection refused

The output show as above mean port 122 is closed.

Thanks to:
http://www.freecodeclub.com/

No comments:

Post a Comment