howtouselinux

6 ways to Check a remote port is open in Linux

Table of Contents

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.

  • 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

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.

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()”

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:

 

David Cao
David Cao

Hey there! I am David, a Cloud & DevOps Enthusiast and 18 years of experience as a Linux engineer. I work with AWS, Git & GitHub, Linux, Python, Ansible, and Bash. I am a technical blogger and a Software Engineer, enjoy sharing my learning and contributing to open-source.