3 ways to check if a port is open on remote Linux system

When installing or configuring an application in the Linux system, the associated port should also be open which allows the application for external access. If the application port is not open, it will make the program throw errors and hence malfunction.

For instance, when you configure the Apache Web server on Linux, you must open ports 80 and 443 that listens to incoming connections for Apache on the firewall, and that allows users to access websites hosted on your web server through the browser.

Please note: You also need to open these network ports in the hardware firewall against your server IP, if you already managing it.

In this article, we will show you, how to check if a network port is opened or closed on a remote Linux system using the below three methods.

  • 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.

All of these commands allow you to scan against one server at a time, but if you want to scan open ports on multiple servers, please check out the following post that contains the shell script:

1) Check open ports with netcat

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

It allows user to scan a single port or a port range.

Common Syntax for nc (netcat):

$ nc [-options] [host_name or ip] [port_number]

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

If it’s a 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

2) Check open ports using nmap command

nmap (Network Mapper) is a powerful open source network scanning tool that is designed to rapidly scan large networks, but can also be used to scan a single host.

It is specifically used for security auditing and penetration testing, but it can also be used for port scanning, host scanning, and network inventory.

Common Syntax for nmap:

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

If it’s a 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

3) Check open ports with telnet

The telnet command is used for interactive communication with another host using the TELNET protocol. It doesn’t provides built-in security so traffic between a TELNET client and TELNET server is not encrypted.

Common Syntax for telnet:

$ telnet [HostName or IP] [PortNumber]

If it’s a 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 found only the above three methods. If you are aware of any other ways, please let us know by sharing your findings in the comments section below.

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

Leave a Reply

Your email address will not be published. Required fields are marked *