System administrators often need to check if a specific website, service or API is alive or not. The common method to do this is to simply ping the website, service or API URL, and analyze the response. Sometimes you may also need to ping a specific port to see if it allows connections. For example, you may need to ping a database to see if its firewall allows inbound connections. In this article, we will learn how to ping a specific port in Linux.
How to Ping Specific Port in Linux
There are several utilities to help you ping IP address and ports in Linux.
1. Using Telnet
Telnet is one of the most common utilities to ping websites, services, APIs and ports. Here is the command to install it on your Ubuntu/Debian system, if it isn’t present already.
$ sudo apt-get install telnet
Please note, telnet is applicable for both Windows & Unix systems. Once it is installed, you can run it with the following command.
$ telnet <ip_address> <port_number> $ telnet <domain_name> <port_number>
Here is an example to ping port 80
$ telnet 18.104.22.168 80 Trying 22.214.171.124... Connected to 126.96.36.199. Escape character is '^]'. $ telnet 188.8.131.52 389 Connected to 184.108.40.206. Escape character is '^]'.
2. Ping Specific Port Using nc
netcat is another useful utility to ping specific ports. Here is the command to install it in Ubuntu/Debian systems.
$ sudo apt-get install netcat
Here is the syntax to ping specific port.
$ nc -vz <host> <port_number> $ nc -vz <domain> <port_number>
Like telnet, you will always need to specify either hostname or domain name along with port number, that you want to ping.
You can use -v option for verbose output and -z option for port scanning. Here is an example command to test netcat.
$ nc -vz google.com 80 google.com [<ip_address>] 80 (http) open
The above command shows that you are able to connect to port 80. Here is an example when connection is refused.
$ nc -vz google.com 389 google.com [<ip_address>] 389 (ldap) : Connection refused
3. Ping Specific Port using nmap
nmap is another utility that allows you to ping websites, services, API & ports. Here is the command to install it Ubuntu/Debian systems.
$ sudo apt-get install nmap
Here is the syntax to ping a specific port using nmap command.
$ nmap -p <port_number> <ip_address> $ nmap -p <port_number> <domain_name>
Please note, unlike telnet and netcat, you need to use -p option followed by port number, in case of nmap command. The hostname or domain name or IP address needs to be mentioned after port number.
But nmap allows you to scan a range of IP addresses, if you mention the CIDR information instead of IP address. Here is an example to scan port range 220.127.116.11-18.104.22.168
$ nmap -p 389 22.214.171.124/24
Similarly, you can also scan a range of ports on single IP or range of IP addresses, using NMAP. Here is an example to scan ports 1-100 on single IP address.
$ nmap -p 1-100 126.96.36.199
Here is the command to scan ports 1-100 on IP address range 188.8.131.52-184.108.40.206
$ nmap -p 1-100 220.127.116.11/24
NMAP is very powerful command as it allows you to scan not only range or IP addresses but also range of ports.
4. Ping Specific Port Using PowerShell
If you are on Windows, you can use PowerShell to ping specific port using Test-NetConnection. Here is the command for this purpose.
$ Test-NetConnection <ip_address> -p <port_number>
Here is an example to ping specific port 100 on IP 18.104.22.168 using PowerShell.
$ Test-NetConnection 22.214.171.124 -p 100
In this article, we have learnt several ways to ping specific port. Among them, telnet is the most commonly used command while is nmap is the most versatile command. You can use any of them as per your requirement.