check remote ports in linux

How to Check Remote Port is Open in Linux

Often Linux system administrators need to check if a port is open or not on a remote server. In this article, we will learn several ways to check if remote port is open in Linux.


How to Check Remote Port is Open in Linux

We will look at 5 different ways to check remote port. Most tools are already available in most Linux systems. Couple of them use python which is also installed in most Linux systems.


1. Using nc command

You can use the following nc command with options to check remote port 22’s status on remote IP 54.43.32.21. Replace the remote IP as per your requirement.

$ nc [-options] [HostName or IP] [PortNumber]

$ nc -zvw10 54.43.32.21 22

In the above command,

  • z: zero-I/O mode which is used for scanning
  • v: for verbose output
  • w10: timeout wait seconds


2. Using nmap

You can also use the popular nmap command to check if remote port is open. Here is the nmap command to check if port 22 is open on IP 54.43.32.21

$ nmap [-options] [HostName or IP] [-p] [PortNumber]

$ nmap 54.43.32 -p 22


3. Using telnet

Alternatively, you can also use telnet command to ping a remote port and see if it is open or not. Here is the telnet command to check if port 22 is open on IP 54.43.32.21

$ telnet [HostName or IP] [PortNumber]

$ telnet 54.43.32.21 22


4. Using cURL

Although cURL command is typically used to download files & packages, you can also use it to test if a remote port is open or not. Here is the cURL command to check if remote port 22 on IP 54.43.32.21 is open or not, by issuing telnet command via cURL.

$ curl -v telnet://54.43.32.21:22

The above 4 methods allow you to directly use Linux commands and utilities to directly check remote port status from terminals and shell scripts. However, if you are running an application or website which is based on python, you can also use the following couple of methods to check remote port status. They are useful for embedding within applications and programs.


5. Using python

Python offers 2 libraries, telnetlib and socket to check remote port status. Here is the python command to use telnetlib to check status of port 22 on IP 54.43.32.21.

python -c "import telnetlib; tel=telnetlib.Telnet('54.43.32.21','22',10); print tel; tel.close()"

In the above code, we basically import telnetlib, and run its Telnet() function to check and print remote port status.

Here is the python command to use socket library to do the same.

Python -c "import socket; s = socket.socket(); s.settimeout(10); s.connect(('54.43.32.21', 22));"

Here also, we first import the socket library and then call the connect() function to check status of remote port.

In this article, we have learnt several ways to check status of port on remote IP. You can also use them with URLs and hostnames, not just IP addresses. If you want to check the status directly from terminal, you can use the Linux commands. If you want to embed the process within your application or script, you can use the python commands mentioned above.

Also read:

How to Generate GRUB Password in Linux
How to Boot Into Single User Mode in Linux
How to Restore Deleted Tmp Directory in Linux
How to Change Linux Partition Label Name
How to Disable Output Buffering in Python

Leave a Reply

Your email address will not be published.