Three ways to check whether a port is open on remote Linux system?

This is an important topic for all the users who are working in IT infrastructure team, because they have to check whether the port is open or not on Linux server before proceeding to any other steps.

If it’s not open then they can directly ask the Linux admin to check on this and if it’s open then we need to check with application team, etc,.

In this article, we will show you, how to check this using three methods.

If you are looking for multiple server scan then you have to write a small shell script to achieve this. Simply navigate to the following URL to Check Whether A Port Is Open On Multiple Remote Linux System?

It can be done using the following Linux commands.

  • nc: Netcat is a simple Unix utility which reads and writes data across network connections, using TCP or UDP protocol.
  • nmap: Nmap (“Network Mapper”) is an open source tool for network exploration and security auditing. It was designed to rapidly scan large networks.
  • telnet: The telnet command is used for interactive communication with another host using the TELNET protocol.

How to check whether a Port is open on the remote Linux system using nc (netcat) Command?

nc stands for netcat. Netcat is a simple Unix utility which reads and writes data across network connections, using TCP or UDP protocol.

It is designed to be a reliable “back-end” tool that can be used directly or easily driven by other programs and scripts.

At the same time, it is a feature-rich network debugging and exploration tool, since it can create almost any kind of connection you would need and has several interesting built-in capabilities.

Netcat has three main modes of functionality. These are the connect mode, the listen mode, and the tunnel mode.

Common Syntax for nc (netcat):

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

In this example, we are going to check whether the port 22 is open or not on the remote Linux system.

If it’s success then you will be getting the following output.

# nc -zvw3 192.168.1.8 22

Connection to 192.168.1.8 22 port [tcp/ssh] succeeded!

Details:

  • nc: It’s a command.
  • z: zero-I/O mode (used for scanning).
  • v: For verbose.
  • w3: timeout wait seconds
  • 192.168.1.8: Destination system IP.
  • 22: Port number needs to be verified.

If it fails then you will be getting the following output.

# nc -zvw3 192.168.1.95 22

nc: connect to 192.168.1.95 port 22 (tcp) failed: Connection refused

How to check whether a port is open on the remote Linux system using nmap Command?

Nmap (“Network Mapper”) is an open source tool for network exploration and security auditing. It was designed to rapidly scan large networks, although it works fine against single hosts.

Nmap uses raw IP packets in novel ways to determine what hosts are available on the network, what services (application name and version) those hosts are offering, what operating systems (and OS versions) they are running, what type of packet filters/firewalls are in use, and dozens of other characteristics.

While Nmap is commonly used for security audits, many systems and network administrators find it useful for routine tasks such as network inventory, managing service upgrade schedules, and monitoring host or service up time.

Common Syntax for nmap:

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

If it’s success then you will be getting the following output.

# nmap 192.168.1.8 -p 22

Starting Nmap 7.70 ( https://nmap.org ) at 2019-03-16 03:37 IST
Nmap scan report for 192.168.1.8
Host is up (0.00031s latency).

PORT   STATE SERVICE
22/tcp open  ssh

Nmap done: 1 IP address (1 host up) scanned in 13.06 seconds

If it fails then you will be getting the following output.

# nmap 192.168.1.8 -p 80
Starting Nmap 7.70 ( https://nmap.org ) at 2019-03-16 04:30 IST
Nmap scan report for 192.168.1.8
Host is up (0.00036s latency).

PORT   STATE  SERVICE
80/tcp closed http

Nmap done: 1 IP address (1 host up) scanned in 13.07 seconds

How to check whether a Port is open on the remote Linux system using telnet command?

The telnet command is used for interactive communication with another host using the TELNET protocol.

Common Syntax for telnet:

$ telnet [HostName or IP] [PortNumber]

If it is  success then you will be getting the following output.

$ telnet 192.168.1.9 22
Trying 192.168.1.9...
Connected to 192.168.1.9.
Escape character is '^]'.
SSH-2.0-OpenSSH_5.3
^]
Connection closed by foreign host.

If it fails then you will be getting the following output.

$ telnet 192.168.1.9 80
Trying 192.168.1.9...
telnet: Unable to connect to remote host: Connection refused

We had found only the above three methods. If you are aware of any other ways, please let us know by adding your findings in the comments section.

Magesh Maruthamuthu

Love to play with all Linux distribution

You may also like...