In general to troubleshoot network connectivity ping command is used, where ping command uses the ICMP protocol behind the scenes. ICMP stands for Internet Control Message Protocol, it is used as complimentary protocol along with IP to send back error message or information about IP communication whether it is successful or not. When it comes to checking particular port is open or not ping command doesn’t work, since ICMP protocol works in network layer and port numbers are associated with TCP or UDP protocols of transport layer
To check whether particular port is open or not, there are other utilities such as
- Network Mapper (nmap)
Ping a Specific Port Using Telnet
Telnet stands for Teletype network. It is the type of protocol also a tool, which is used as standard TCP/IP protocol virtual terminal service to connect to the remote computer using local computer. Telnet operates based on client/server architecture. Where, local computer uses the Telnet client program while remote computer uses the Telnet serve program. Telnet uses the TCP protocol and port number 23. Telnet can also used as generic client to connect with remote computer with any port if particular port is open and send/receive data according to protocol of connected port.
To ping the specific port or check if specific port on remote host is open. Connect to the remote host on specific port using telnet as follows
telnet <remote host address> <port number>
telnet google.com 80
If port is open, telnet will be able to connect and you will see the prompt something like
$ telnet google.com 80 Trying 2404:6800:4009:82e::200e... Connected to google.com. Escape character is '^]'.
If port is not open, telnet will not be able to connect. And, you can see Telnet is stuck with Trying message
$ telnet google.com 89 Trying 2404:6800:4009:82e::200e... telnet: connect to address 2404:6800:4009:82e::200e: Operation timed out Trying 220.127.116.11...
To quit from telnet, press the key combination CTRL + ] and then q command
Ping a Specific Port Using Netcat
Netcat (nc) is the tool that is designed to read and write data into network connections that use TCP or UDP Transport protocols using IP protocol suite. You can use Netcat as either client or server.
Install netcat on Debian machines as follows
sudo apt install netcat
To ping or connect to the specific port on remote host use the follow command
nc -vz <remote host address> <port number>
-z Specifies that nc should just scan for listening daemons, without sending any data to them. It is an error to use this option in conjunction with the -l option. -v Have nc give more verbose output.
nc -vz google.com 80
netcat -vz google.com 80
Note that: on some machine netcat command is also available as nc
Here is the case, where port number 80 is open for http
$ nc -vz google.com 80 Connection to google.com port 80 [tcp/http] succeeded!
If specified port is not open, netcat will result in timeout
$ nc -vz google.com 89 nc: connectx to google.com port 89 (tcp) failed: Operation timed out
Ping a Specific Port Using nmap
nmap is a great networking tool which is used for network discovery, vulnerability scanning where it can also be used to ping specific port number on remote host.
Install nmap on Debian based system
apt install nmap
Command to ping specific port
nmap -p <port number <remote host address>
nmap -p google.com 80
To ping range or ports
nmap -p 80-180 google.com
Nmap is very powerful tool, using nmap you can also ping specific port across the block or network of computers by specifying CIDR block
nmap -p 80 192.168.4.1/24
Ping a Specific Port using Test-NetConnection on Windows Using PowerShell
To ping a specific port on windows platform using PowerShell, Test-NetConnection command is pretty helpful
Test-NetConnection <remote_host_address> -p <port_number>
Test-NetConnection google.com. -p 80