Skip to Content

We Tested 6 Ways to Check Remote Ports in Linux —And Found 2 Unexpected Methods That Work Wonders

For Linux administrators, checking the status of remote ports is a routine yet essential task.

🔍 In our quest to streamline this process, we’ve tested six different methods to assess remote port status.

Along the way, we uncovered two unexpected techniques that offer remarkable benefits.

Curious about what we found? Dive in to discover how these methods can transform your network diagnostics. 💡

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 to Check if the Remote Port is Open in Linux

$ nc [-options] [HostName or IP] [PortNumber]
$ nc -zvw10 192.168.0.1 22

The nc command, also known as netcat, is a powerful tool for network diagnostics and troubleshooting. It supports a variety of functions including port scanning and banner grabbing.

Options:

  • -z: Zero-I/O mode, used for scanning without sending any data.
  • -v: Verbose mode, provides detailed output about the connection attempt.
  • -w10: Sets a timeout of 10 seconds for the connection attempt. Adjust the time as needed.

Example usage:

nc -zv 192.168.0.1 22

If the port is open, you’ll see a message indicating that the connection succeeded. If it’s closed or blocked, you’ll receive an error message.

The -l option can be used to listen on a specific port:

$ nc -l -p 1234

This command starts nc in listening mode on port 1234, allowing you to accept incoming connections on that port.

Use nmap to Check if the Remote Port is Open in Linux

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

The nmap (Network Mapper) command is a comprehensive network scanning tool used for network discovery and security auditing.

Options:

  • -p: Specifies the port or port range to scan. For example, -p 22 scans port 22.
  • -T4: Sets timing for faster scans. T4 is a good balance between speed and accuracy.
  • -A: Enables OS detection, version detection, script scanning, and traceroute.

Example usage:

nmap 192.168.0.1 -p 22

nmap provides a detailed output including port status and service information. It’s useful for comprehensive network assessments.

Use telnet to Check if the Remote Port is Open in Linux

$ telnet [HostName or IP] [PortNumber]
$ telnet 192.168.0.1 22

The telnet command is a simple tool used for text-based communication with remote systems. While telnet is often used for remote logins, it can also check port connectivity.

Example usage:

telnet 192.168.0.1 22

If the port is open, you’ll see a successful connection message. If the port is closed, you’ll receive a connection refused or timeout message.

Note: Telnet is considered insecure and is generally replaced by SSH for secure connections.

Use Python Telnet to Check if the 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:

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

The telnetlib module in Python allows you to create Telnet sessions to communicate with remote servers.

Example usage:

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

This one-liner attempts to establish a Telnet connection to the specified IP address and port (in this case, 192.168.0.1 on port 22) with a 10-second timeout.

If successful, it prints the Telnet object and then closes the connection.

Use Python Socket to Check if the 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 in Python provides low-level network interface functions.

It can be used to check any TCP port, not just Telnet. This method is good for general port checking and can be easily integrated into larger Python scripts.

Pros:

  • Works with any TCP port
  • Highly customizable within Python scripts
  • Efficient for bulk checking

 

Example usage:

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

This command attempts to create a socket connection to the specified IP address and port, with a timeout of 10 seconds. If successful, it indicates that the port is open.

Use curl to Check if the Remote Port is Open in Linux

curl -v telnet://192.168.0.1:22

The curl command is a tool for transferring data with URL syntax, which can also be used to check port connectivity using the Telnet protocol.

Pros:

  • No need for Python
  • Quick and easy for one-off checks
  • Verbose output provides connection details

 

Example usage:

curl -v telnet://192.168.0.1:22

This command attempts to connect to the specified port using the Telnet protocol. The -v flag enables verbose mode, showing connection details.

Note: Each method has its use cases and limitations. Choose the method based on your needs for port checking, available tools, and the level of detail required.

While traditional tools like nc, nmap, and telnet are reliable for checking remote ports, curl and Python’s telnetlib offer unique and unexpected advantages.

curl provides a quick and versatile way to check port status using existing tools, while telnetlib in Python allows for advanced, programmatic checks.

By understanding the strengths and limitations of each method, you can choose the most appropriate tool for your specific needs, ensuring efficient and effective network management.