“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 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.
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.
Lastly, to make it more perfect, you can get a script as example below:
#!/bin/bash
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 ]
then
/etc/init.d/$INITS restart
fi
No comments:
Post a Comment