Today's Question:  What does your personal desk look like?        GIVE A SHOUT

Check whether a remote server port is open on Linux

  sonic0002        2017-12-23 11:45:20       66,998        0    

As a system administrator or network engineer or application developer, there is a need to check whether a port on remote server is open so that you can tell whether the service under check is running or not. In this post, we would cover a few methods to check whether a remote server port is open or not on Linux.

telnet

telnet is the most frequently used command on both Windows and Linux to check port. The simple usage for this command is 

telnet [host] [port]

When the port is open, the output will be like:

Trying 192.168.56.160...
Connected to 192.168.56.160.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2

When the port is not open or some other issue occurs, the output will be like:

Trying 192.168.56.160...
telnet: Unable to connect to remote host: Connection refused

This command is usually available on Linux.

nc

nc or netcat is an utility which can do lots of TCP and UDP related stuff including packet transimission, port scanning etc. To check whether a port is open, the syntax is:

nc -vz [host] [port]

The -v is to print the output in verbose mode and -z is for scanning the listening service at the specified port.

When the port is open, the output looks like

Connection to 192.168.56.160 22 port [tcp/ssh] succeeded

When the port is not open, the output looks like

nc: connect to 192.168.56.160 port 443 (tcp) failed: Connection refused

Pretty straightforward.

nmap

nmap is an open source utility for network scanning. It can be used to not only scan open port but also can do much more.  It can be used to check multiple hosts and ports at once.  This utility usually needs explicit installation.

The basic syntax is:

nmap -p [port] [host]

When the port is open, the output looks like:

Nmap scan report for 192.168.56.160
Host is up (0.88s latency).
PORT   STATE SERVICE
22/tcp open  ssh

When the port is not open, the output looks like:

Nmap scan report for 192.168.56.160
Host is up (1.0s latency).
PORT    STATE  SERVICE
443/tcp closed https

This can not only be used to scan TCP port but also can be used for UDP port scan. It would provide more fancy stuff like the service information of the port shown above.

echo > /dev/tcp/...

If you are familiar with Linux, you should know that everything is a file and the status for a host and port is also available with file handler. In case no telnet or nc available(frequently seen in a docker container), you can use this method to check whether a remote port is open. The syntax looks like

echo > /dev/tcp/[host]/[port] && echo "Port is open"
echo > /dev/udp/[host]/[port] && echo "Port is open"

It depends on which kind of protocol you are trying, the command is a bit different. For service like ssh, this is usually TCP protocol and you can use echo > /dev/tcp...

When the port is open, the output looks like

$ echo > /dev/tcp/192.168.56.160/22 && echo "Port is open"
Port is open

When the port is not open, the output looks like

$ echo > /dev/tcp/192.168.56.160/443 && echo "Port is open"
bash: connect: Connection refused
bash: /dev/tcp/192.168.56.160/443: Connection refused

This method usually is the last but safest method to use as it doesn't require installing external utility.

Apart from these commands, there are other third party libraries which can be used to check port. Also every programming language should also provide the capability for doing this. Hence choose the one suits your need most.

LINUX  TELNET  PORT  NC  NMAP 

Share on Facebook  Share on Twitter  Share on Weibo  Share on Reddit 

  RELATED


  0 COMMENT


No comment for this article.