Get Knowledge - Information

Linux: Check whether a remote server port is open on Linux

342

Check whether a remote server port is open on Linux

As a system administrator network engineer or application developer, there is a need to check whether a port on a remote server is open so that you can tell whether the service under check is running or not. In this post, we Will 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 ports. The simple usage for this command is

telnet [host] [port]

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

1
2
3
4
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 this:

1
2
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 a utility that can do lots of TCP and UDP-related stuff including packet transmission, 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 ports 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:

1
2
3
4
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 this:

1
2
3
4
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 ports but also can be used for UDP port scans. 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 is 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

1
2
$ 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

1
2
3
$ 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, other third-party libraries can be used to check ports. Also, every programming language should provide the capability for doing this. Hence choose the one that suits your needs most.

The article was originally published here.

TO GET MORE KNOWLEDGE ABOUT Linux, PLEASE VISIT OUR SITE: Forupon.com.

Comments are closed.