Pinging a port in Linux is a routine operation any dev or engineer needs to do in their day to day life. While it is such an important operation which doesn’t look important on the surface but can be crucial for checking the status of the service or just checking if the server is up or not, knowing different ways is really valuable. In this article, let us explore some of the way to do it.
Telnet is a widely used tool for communicating with remote servers. It can also be used to check the status of a specific port. To ping a port using Telnet, open a terminal and type the following command
telnet <hostname or IP address> <port number>
telnet example.com 80
If the connection is successful, you’ll see a message indicating the successful connection. Otherwise, an error message will be displayed.
Netcat (nc) Command
Netcat, often abbreviated as nc, is a simple networking tool that can be used for various purposes, including checking port connectivity. To ping a port using Netcat, use the following syntax
nc -zv <hostname or IP address> <port number>
The ‘-z’ option makes Netcat operate in scanning mode, and ‘-v’ provides verbose output. For instance
nc -zv example.com 80
This command will attempt to establish a connection to the port 80 on the specified host.
Nmap is a network scanning tool that can provide detailed information about a host and its open ports. On top of that, it can also be used to ping a specific port to check port connection using Nmap.
nmap -p <port number> <hostname or IP address>
nmap -p 80 example.com
Nmap will display information about the specified port and its status, helping you determine if the service is reachable.
Curl is a command-line tool for making HTTP requests, but it can also be used to check the status of a port. The following command checks if a connection can be established to a specific port
curl telnet://<hostname or IP address>:<port number>
Replace <hostname or IP address> and <port number> with your target information. For example
Curl will attempt to establish a connection to the specified port and display relevant information.
Using the Socket Command
The socket command is another option to check port connectivity. The basic syntax is as follows
echo -n | timeout 1 bash -c 'cat < /dev/null > /dev/tcp/<hostname or IP address>/<port number>' && echo "Port is open" || echo "Port is closed"
echo -n | timeout 1 bash -c 'cat < /dev/null > /dev/tcp/google.com/443' && echo "Port is open" || echo "Port is closed"
This command uses a combination of echo, timeout, and bash to check if a connection can be established to the specified port.
In conclusion, Linux systems provide multiple methods to ping a port or list of ports, each method with its own strengths and use cases. Whether you have the luxury of using simple telnet commands or cursed with a box without access to install programs, any one of the above methods should help you accomplish the task.