How to ping and test for a specific port from Linux or Unix command line

I wanted to see if a specific port of a desktop computer or server is open from a Linux or Unix command line shell. How do I ping a specific port? Is it possible possible to ping an 192.168.1.254:80 or 192.168.1.254:23 ports?

You use the ping command to send ICMP ECHO_REQUEST packets to network computers, routers, switches and more. ping works with both IPv4 and IPv6. Ping is using ICMP protcol. The ping command can not be used to ping a specific port.
Can you ping a specific port of a server?
However, use any one of the following command to see if a port is open or not as follows:

ADVERTISEMENTS

Linux ping port using telnet command

The syntax is:
telnet {host} {port}
telnet www.cyberciti.biz 80
telnet 192.168.2.254 80

Sample outputs:

Trying 192.168.2.254...
Connected to router.
Escape character is '^]'.
^]
telnet> q
Connection closed.

To close your session, press Ctrl+]+q.

Use nc command

The syntax is:
nc -vz {host} {port}
nc -vz 192.168.2.254 80
nc -vz www.cyberciti.biz 443

Sample outputs:

found 0 associations
found 1 connections:
     1:	flags=82
	outif utun1
	src 10.8.0.2 port 54997
	dst 104.20.187.5 port 443
	rank info not available
	TCP aux info available

Connection to www.cyberciti.biz port 443 [tcp/https] succeeded!

Unix ping port using nmap command

The syntax is:
nmap -PNp {port} {host}
nmap -p {port} {host}
nmap -p 22 www.cyberciti.biz
nmap -p 443 192.168.2.254

Sample outputs:


Starting Nmap 7.40 ( https://nmap.org ) at 2017-05-24 01:00 IST
Nmap scan report for router (192.168.2.254)
Host is up (0.00034s latency).
PORT    STATE SERVICE
443/tcp open  https

Nmap done: 1 IP address (1 host up) scanned in 0.04 seconds

See “Top 32 Nmap Command Examples For Sys/Network Admins” for more info.

Use bash shell

The syntax is as follows:

## check for tcp port ##
## need bash shell ##
(echo >/dev/tcp/{host}/{port}) &>/dev/null && echo "open" || echo "close"
(echo >/dev/udp/{host}/{port}) &>/dev/null && echo "open" || echo "close"
(echo >/dev/tcp/www.cyberciti.biz/22) &>/dev/null && echo "Open 22" || echo "Close 22"
(echo >/dev/tcp/www.cyberciti.biz/443) &>/dev/null && echo "Open 443" || echo "Close 443"

Sample outputs:

Close 22
Open 443

Use nping command

Nping is an open-source tool for network packet generation, response analysis and response time measurement. Nping allows users to generate network packets of a wide range of protocols, letting them tunevirtually any field of the protocol headers. While Nping can be used as a simple ping utility to detect active hosts, it can also be used as a raw packet generator. The syntax is:
sudo nping --tcp -p {port} {host}
sudo nping --tcp -p 443 www.cyberciti.biz

Sample outputs:

Fig.01: nping just pinged www.cyberciti.biz host at port 443

Fig.01: nping just pinged www.cyberciti.biz host at port 443

🐧 Get the latest tutorials on SysAdmin, Linux/Unix, Open Source/DevOps topics:
CategoryList of Unix and Linux commands
File Managementcat
FirewallAlpine Awall CentOS 8 OpenSUSE RHEL 8 Ubuntu 16.04 Ubuntu 18.04 Ubuntu 20.04
Network Utilitiesdig host ip nmap
OpenVPNCentOS 7 CentOS 8 Debian 10 Debian 8/9 Ubuntu 18.04 Ubuntu 20.04
Package Managerapk apt
Processes Managementbg chroot cron disown fg jobs killall kill pidof pstree pwdx time
Searchinggrep whereis which
User Informationgroups id lastcomm last lid/libuser-lid logname members users whoami who w
WireGuard VPNAlpine CentOS 8 Debian 10 Firewall Ubuntu 20.04

ADVERTISEMENTS
5 comments… add one
  • macxcool May 23, 2017 @ 23:18

    The nping utility that comes with NMap allows you to ‘ping’ a port by making a TCP connection to it. This does more than just check if the port is open. It verifies that it can be connected to.

  • Jalal Hajigholamali May 24, 2017 @ 6:24

    Hi,
    In line
    nmap -p 443 192.168.2.254 443
    remove last 443
    thanks

  • 🐧 Vivek Gite May 24, 2017 @ 8:42

    Thanks for the heads up!

  • Jacek Jun 6, 2017 @ 12:56

    The shell approach is brilliant! Especially, when you’re working with some random docker containers. Thanks a lot!

Leave a Reply

Your email address will not be published.

Use HTML <pre>...</pre>, <code>...</code> and <kbd>...</kbd> for code samples.