Skip to Content

6 ways to Check a remote port status in Linux

Checking remote port status is a common task for Linux admin.

Now we collect 6 different ways for this task. We don’t need to install any package if we use the following two python commands.

We need to install the package if we choose nc, nmap,telnet.

Methods to check if a remote port is open in Linux

The following commands can be used to check if a port is open on the remote server in Linux.

Get Your Free Linux training!

Join our free Linux training and discover the power of open-source technology. Enhance your skills and boost your career! Learn Linux for Free!
  • Use nc command nc -zvw10 192.168.0.1 22
  • Use nmap command nmap 192.168.0.1 -p 22
  • Use telnet command telnet 192.168.0.1 22
  • Use python telnet module
  • Use python socket module
  • Use curl command

 

Related: 3 ways to check open ports in Linux

 

Introducing the Network Connectivity Test Command Generator from How to Use Linux, a handy tool designed to streamline network troubleshooting. This user-friendly generator allows you to create various connectivity test commands, such as nc, nmap, and curl, tailored to your specific needs.

Use nc command to check the remote port is open in Linux

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

$ nc -zvw10 192.168.0.1 22

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

 

The “nc” command stands for “netcat”. The “nc” command is a very versatile command that can be used for a variety of purposes, including network administration and data transmission.

For example, the “nc” command can be used to create a simple TCP connection between two computers.  The “nc” command can be used to connect to a remote server on a given port and send/receive data.

For example, if you want to connect to a remote server on port xx, you would use the following command: nc  -zv <remote server> port

In this example, “<remote server>” is the IP address or hostname of the remote server, and “<port>” is the port that you want to connect to.

I needed to see if the port 22 (SSH) on a remote machine was open, so I opened a terminal and ran the following command:

$ nc -vz hostname.com 22

The -v option enabled verbose output, and the -z option instructed nc to only scan for open ports, without actually establishing a connection.

The output showed me the results of the port scan:

Connection to hostname.com 22 port [tcp/ssh] succeeded!

This told me that the port 22 was open and that I could connect to the remote machine using SSH.

In another scenario, if the port was not open, the output would look something like this:

nc: connect to hostname.com port 22 (tcp) failed: Connection refused

You can also use the “nc” command to open a port in Linux. To do this, you would use the following command: nc -l -p 1234

In this example, “-l” is used to listen for a connection on port 1234

Use nmap to check the remote port is open in Linux

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

nmap 192.168.0.1 -p 22

The “nmap” command is a command-line tool used for network exploration and security auditing. The “nmap” command can be used to scan for open ports on a remote server, as well as to identify the operating system and services running on that server.

For example, if you want to scan for open ports on a remote server, you would use the following command:

nmap <remote server> -p port

In this example, “<remote server>” is the IP address or hostname of the remote server, and “<port>” is the port that you want to scan.

Use telnet to check the remote port is open in Linux

$ telnet [HostName or IP] [PortNumber]

telnet 192.168.0.1 22

The telnet command is a command-line tool used for network communication. The telnet command can be used to connect to a remote server on a given port.

For example, if you want to connect to a remote server on port, you would use the following command: telnet <remote server> port

In this example, “<remote server>” is the IP address or hostname of the remote server, and “<port>” is the port that you want to connect to.

Use python telnet to check remote port is open in Linux

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

If you are using Python3, using the following command:

python3 -c "import telnetlib; tel=telnetlib.Telnet('10.248.169.140','5432',10); print(tel); tel.close()"

Telnetlib is a module in Python that allows you to communicate with remote servers using the Telnet protocol. The Telnet protocol is a text-based protocol used for communicating with remote servers.

To use the Telnetlib module, you first need to import it into your Python program: import telnetlib

Next, you need to create an instance of the Telnet object: telnet = telnetlib.Telnet()

The Telnet object has a number of methods that allow you to send and receive data. For example, the send() method allows you to send text data to the remote server, and the recv() method allows you to receive text data from the remote server.

Use python socket to check remote port is open in Linux

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

The “socket” module is a module in Python that allows you to create and use sockets. A socket is a communication channel that allows two processes to connect and send/receive data.

The “socket” module has a number of functions that allow you to do a variety of things, including creating sockets, binding sockets to addresses, and sending/receiving data.

In order to use the “socket” module, you first need to import it into your Python program. You can do this by using the following command: import socket

Once you have imported the “socket” module, you can then use its functions to create sockets and communicate with other processes.

Use curl to check remote port is open in Linux

We have another solution for this with the curl command. curl -v telnet://192.168.0.1:22

The “curl” command is a tool used for transferring data with URL syntax. The “curl” command can be used to send data to a remote server, or it can be used to download data from a remote server.

If you want to download data from a remote server, you can use the following command: curl <remote server> port -o filename.txt

In this example, “<remote server>” is the IP address or hostname of the remote server, and “<port>” is the port that you want to download data from.

The “curl” command can also be used to check whether a port is open or not. To do this, you would use the following command:

curl -v telnet://<remote server>:port

In this example, “<remote server>” is the IP address or hostname of the remote server, and “<port>” is the port that you want to check.

 

Understanding TCP Connection with Examples

4 Ways to Check Network Status on Linux

Linux Learning Guide: