How to Test Port Connectivity in Linux [All Possible Ways]

How to Test Port Connectivity in Linux [All Possible Ways]

In this tutorial, we will learn about how to test port connectivity in Linux using all possible ways. In Linux, sometimes you must have faced the issue where you try to reach a service running on certain port but the connectivity fails. There could be many possible reason for this like port is not enabled, server IP is incorrect or some  firewall rules are preventing the connection to get established and so on.  We will look at some of the Linux utility and commands which helps to troubleshoot the port connectivity issues. So, let us begin the tutorial.

 

How to Test Port Connectivity in Linux [All Possible Ways]

Also Read: How to Install Sublime Text 4 on AlmaLinux 8 [4 Easy Steps]

In Linux, there are various ways available to check the port connectivity in a client-server architecture. The client is referred to the Linux machine which tries to reach a service on a certain port running on another Linux machine called server. We will use 4 important and powerful Linux utilities namely, timeout, nmap, telnet and nc for debugging the port connectivity issues . We will learn about what these commands and utilities are followed by how to install them and how to use them to troubleshoot the port connectivity issue.

Prerequisite

  • Centos/Rhel Operating System Installed.
  • Sudo privilege/root user privilege required.
  • Basic understanding of Linux command. 

 

1.   Using Timeout utility

About timeout utility

The first and most preferable utility which can be used to troubleshoot the port connectivity issue is timeout. This utility is by default available in most of the Linux distributions. The binary of timeout utility is found in the path  /usr/bin/timeout. Linux users  prefer to use this utility as the user does not have to do any additional configuration in the system to use this utility since it is present in the system by default.

To check which package contains the timeout utility, you can execute below command. You will see that it is part of the “coreutils” package.

[[email protected] ~]# rpm -qf `which timeout`
coreutils-8.22-24.el7_9.2.x86_64

 

How to use timeout utility

If you are using the timeout utility for the first time and not sure about how the utility works, I suggest you to go through the man page of timeout utility and look at all the features it supports. To do so,  execute below command to check the complete usage of timeout utility.

[[email protected] ~]# timeout --help

 

There are many usage of timeout utility but we will only focus only on its usage to troubleshoot the port connectivity issue. It follows the below syntax.

timeout <value> bash -c "</dev/tcp/<server>/<port>"

Here,
value: timeout value until which target server will be polled.
server: target server IP
port: port number

 

Let us now ping a server which has the IP 100.73.42.115 on port 22 . The port 22 is listening so the connectivity must be established. Execute below command

[[email protected]  ~]# timeout 4 bash -c "</dev/tcp/100.73.42.115/22"

The above command will not return any output and exit after 4 sec. To know if the command is success or not , check the exit status to command using below command. If the exit status is 0, that means server is reachable on port 22.

[[email protected]  ~]# echo $?
0

 

NOTE:

We can also user server hostname instead of server IP with the timeout command.

 

Next, we will use execute the above command again. This time, we will use server hostname instead of server IP to get the same output as shown below.

[[email protected]  ~]# hostname
linuxnasa.com

[[email protected]  ~]# timeout 4 bash -c "</dev/tcp/linuxnasa.com/22"
[[email protected]  ~]# echo $?
0

 

Let us now check the port 2222 for the same server. We know that this port is not listening, so the connectivity to the server on this port must fail. Let us see this behavior by executing below command. If ‘Connection refused’ is returned by the output, it means the server is not reachable on port 2222.

[[email protected]  ~]# timeout 4 bash -c "</dev/tcp/100.73.42.115/2222"
bash: connect: Connection refused
bash: /dev/tcp/100.73.42.115/2222: Connection refused

 

Another use case is when the server IP itself is either wrong or non pingable from client machine. In such case, the timeout command will return the non-zero status post executing the command as shown below.

[[email protected]  ~]# timeout 4 bash -c "</dev/tcp/199.73.55.10/22"
[[email protected]  ~]# echo $?
124

 

2.  Using nmap Command

About nmap command

In Linux, nmap (Network Mapper) is an open-source command-line tool mostly used for network and security auditing. To use the nmap in Linux, we have to install this package We will first check if nmap is installed in the system using below command. If there is no output returned by below command, then the nmap package is not installed.

[[email protected]  ~]# rpm -qa | grep nmap

 

Next, we will install the nmap package using below command.

[[email protected]  ~]# yum -y install nmap

 

How to use nmap command

To use the nmap command-line tool to check if port connectivity is working or not, we can use below syntax.

nmap <server> -PN -p <port-no> | egrep 'open|closed|filtered'

 

Let us now execute below command following the above syntax. If the server IP is reachable and port is open on the server, it will return the output as “open” as shown below.

[[email protected] ~]# nmap 100.73.42.115 -Pn -p 22 | egrep -io 'open|closed|filtered'
open

 

If the server IP is correct but the port is not listening on the server, it will return the output as “closed” as shown below.

[[email protected] ~]# nmap 100.73.42.115 -Pn -p 8080 | egrep -io 'open|closed|filtered'
closed

 

If the server IP is not reachable or invalid, then it will return the output as “filtered” as shown below.

[[email protected] ~]# nmap 199.73.55.10 -Pn -p 3333 | egrep -io 'open|closed|filtered'
filtered

 

3.  Using telnet command

About telnet command

In Linux, telnet is a command-line tool which is used to create a remote connection with a system over TCP/IP network. We uses the telnet command to manage the remote server using the command line. To check the port connectivity from the client server onto the remote server, we can use below command syntax.

telnet <server-IP> <port>

 

How to use telnet command

To use the telnet command, first step is to check if telnet is installed in the system using below command. If no output is returned then the package is not installed.

[[email protected] ~]# rpm -qa | grep telnet

 

Next, install the telnet package in the system using below command.

[[email protected] ~]# yum -y install telnet

 

Let us now test if server IP  100.73.42.115 is reachable on port 22. The port 22 is listening so connectivity to the server on port 22 must pass.

[[email protected] ~]# telnet 100.73.42.115 22
# telnet 100.73.42.115 22
Trying 100.73.42.115...
Connected to 100.73.42.115.
Escape character is '^]'.
SSH-2.0-OpenSSH_7.4
^]  <-- At this stage press Ctrl+] to get the telnet prompt
telnet> close
Connection closed.

 

4. Using nc command

About nc command

In Linux, nc (Net Cat) is  a command-line tool which is famous among the Linux system administrators for data transfer and network diagnostics. The nc command is quite versatile as we can open a port just by using single command and start listening on it. To check the port connectivity using nc command, we follow the below syntax.

nc <server-IP> <port> -z -v

 

How to use nc command

In order to use the nc command on Linux, first verify if nc package is installed in the system using below command. If no output is returned then the package is not installed.

[[email protected] ~]# rpm -qa | grep nmap-ncat

 

Next, install the nc package in the system using below command.

[[email protected] ~]# yum -y install nmap-ncat

 

Next, we will  check  if server IP 100.73.42.115 is reachable on port 22 . The port 22 is opened and listening so connectivity must pass. If the connection is established, it will return the output as shown below.

Success

[[email protected] ~]# nc -z -v 100.73.42.115 22
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connected to 100.73.42.115:22.
Ncat: 0 bytes sent, 0 bytes received in 0.01 seconds.

 

Next, let us execute the same nc command but for port 25 which is not opened. You will notice that it refuses to connect to the server on port 25 which is the expected behavior.

Failed

[[email protected] ~]# nc -z -v 100.73.42.115 25
Ncat: Version 7.50 ( https://nmap.org/ncat )
Ncat: Connection refused.

 

Summary

We have learnt about different Linux network commands which are helpful to troubleshoot the connectivity issues between client and server machines. It is really up to the end user and also on the requirement based on which a suitable tool can be chosen to troubleshoot the issue.

 

Leave a Comment