How To Ping a Specific Port Number From Command Line in Linux
This post will guide you how to ping a specific port number from the command line interface in your Linux operating system. How do I check if the port on the remote Linux server is open or not under a Linux system.
- Ping Command
- Nmap Command
- Telnet Command
- Nc Command
- Ping a Specific Port Number Using Nmap Command
- Ping a Specific Port Number Using Telnet Command
- Ping a Specific Port Number Using Nc Command
Ping Command
Ping command is the most used toold for checking the network connection for a remote server. and you can use ping command with the IP or URL address of the remote server. ping uses the ICMP protocol’s mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway.
For example, you want to check if “bing.com” web server can be connected or not, just run the following ping command:
$ ping bing.com
Outputs:
devops@devops:~$ ping bing.com
PING bing.com (13.107.21.200) 56(84) bytes of data.
64 bytes from 13.107.21.200 (13.107.21.200): icmp_seq=1 ttl=115 time=45.0 ms
64 bytes from 13.107.21.200 (13.107.21.200): icmp_seq=2 ttl=115 time=44.4 ms
64 bytes from 13.107.21.200 (13.107.21.200): icmp_seq=3 ttl=115 time=46.0 ms
64 bytes from 13.107.21.200 (13.107.21.200): icmp_seq=4 ttl=115 time=44.8 ms
^C
--- bing.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 44.435/45.063/46.006/0.582 ms
As Ping Command is based on ICMP protocol, so you can not ping a specific port number of the remote server by ping command.
Nmap Command
Nmap (“Network Mapper”) is an open source tool for network exploration and security auditing. It was designed to rapidly scan large networks, although it works fine against single hosts. Nmap uses raw IP packets in novel ways to determine what hosts are available on the network, what services (application name and version) those hosts are offering, what operating systems (and OS versions) they are running, what type of packet filters/firewalls are in use, and dozens of other characteristics. While Nmap is commonly used for security audits, many systems and network administrators find it useful for routine tasks such as network inventory, managing service upgrade schedules, and monitoring host or service uptime.
The Syntax of the Namp command is as followss:
nmap [Scan Type...] [Options] {target specification}
Telnet Command
The telnet command is used for interactive communication with another host using the TELNET protocol. It begins in command mode, where it prints a telnet prompt (“telnet> “). If telnet is invoked with a host argument, it performs an open command implicitly;
The syntax of the Telnet command is as follows:
telnet [-468ELadr] [-S tos] [-b address] [-e escapechar] [-l user] [-n tracefile] [host [port]]
Nc Command
The nc (or netcat) utility is used for just about anything under the sun involving TCP, UDP, or UNIX-domain sockets. It can open TCP connections, send UDP packets, listen on arbitrary TCP and UDP ports, do port scanning, and deal with both IPv4 and IPv6. Unlike telnet(1), nc scripts nicely, and separates error messages onto standard error instead of sending them to standard output, as telnet(1) does with some.
The Syntax of the Nc command is as follows:
nc [-46bCDdFhklNnrStUuvZz] [-I length] [-i interval] [-M ttl] [-m minttl] [-O length] [-P proxy_username] [-p source_port] [-q seconds] [-s source] [-T keyword] [-V rtable] [-W recvlimit] [-w timeout] [-X proxy_protocol] [-x proxy_address[:port]] [-Z peercertfile] [destination] [port]
Ping a Specific Port Number Using Nmap Command
You can use the Nmap tool to ping a specific prot number in your Linux system from the command line. And it is not installed by default on your Linux system. So you need to install it by using apt command firstly, type:
# for CentOS or RHEL Linux
$ sudo yum install nmap
# for Ubuntu or Debian Linux
$ sudo apt install nmap
Outputs:
devops@devops:~$ sudo apt install nmap
[sudo] password for devops:
Reading package lists... Done
Building dependency tree
Reading state information... Done
The following additional packages will be installed:
libblas3 liblinear3
Suggested packages:
liblinear-tools liblinear-dev ndiff
The following NEW packages will be installed:
libblas3 liblinear3 nmap
0 upgraded, 3 newly installed, 0 to remove and 36 not upgraded.
Need to get 5,353 kB of archives.
After this operation, 24.5 MB of additional disk space will be used.
Do you want to continue? [Y/n] Y
Get:1 http://mirrors.aliyun.com/ubuntu bionic/main amd64 libblas3 amd64 3.7.1-4ubuntu1 [140 kB]
Get:2 http://mirrors.aliyun.com/ubuntu bionic/main amd64 liblinear3 amd64 2.1.0+dfsg-2 [39.3 kB]
Get:3 http://mirrors.aliyun.com/ubuntu bionic/main amd64 nmap amd64 7.60-1ubuntu5 [5,174 kB]
Fetched 5,353 kB in 3s (1,569 kB/s)
Selecting previously unselected package libblas3:amd64.
(Reading database ... 185929 files and directories currently installed.)
Preparing to unpack .../libblas3_3.7.1-4ubuntu1_amd64.deb ...
Unpacking libblas3:amd64 (3.7.1-4ubuntu1) ...
Selecting previously unselected package liblinear3:amd64.
Preparing to unpack .../liblinear3_2.1.0+dfsg-2_amd64.deb ...
Unpacking liblinear3:amd64 (2.1.0+dfsg-2) ...
Selecting previously unselected package nmap.
Preparing to unpack .../nmap_7.60-1ubuntu5_amd64.deb ...
Unpacking nmap (7.60-1ubuntu5) ...
Setting up libblas3:amd64 (3.7.1-4ubuntu1) ...
update-alternatives: using /usr/lib/x86_64-linux-gnu/blas/libblas.so.3 to provide /usr/lib/x86_64-linux-gnu/libblas.so.3 (libblas.so.3-x86_64-linux-gnu) in auto mode
Setting up liblinear3:amd64 (2.1.0+dfsg-2) ...
Setting up nmap (7.60-1ubuntu5) ...
Processing triggers for man-db (2.8.3-2ubuntu0.1) ...
Processing triggers for libc-bin (2.27-3ubuntu1) ...
devops@devops:~$
Once Nmap is instaled on your system, and you can use the following command to ping port 80 or 443 for a remove server (bing.com), type:
$ nmap -p 80 bing.com $ nmap -p 443 bing.com
Outputs:
devops@devops:~$ nmap -p 80 bing.com Starting Nmap 7.60 ( https://nmap.org ) at 2019-10-03 04:33 EDT Nmap scan report for bing.com (204.79.197.200) Host is up (0.049s latency). Other addresses for bing.com (not scanned): 13.107.21.200 2620:1ec:c11::200 rDNS record for 204.79.197.200: a-0001.a-msedge.net PORT STATE SERVICE 80/tcp open http Nmap done: 1 IP address (1 host up) scanned in 0.25 seconds devops@devops:~$ nmap -p 443 bing.com Starting Nmap 7.60 ( https://nmap.org ) at 2019-10-03 04:33 EDT Nmap scan report for bing.com (204.79.197.200) Host is up (0.045s latency). Other addresses for bing.com (not scanned): 13.107.21.200 2620:1ec:c11::200 rDNS record for 204.79.197.200: a-0001.a-msedge.net PORT STATE SERVICE 443/tcp open https Nmap done: 1 IP address (1 host up) scanned in 0.15 seconds
If you want to ping more that one port numbers(80-85), and you can use the following command:
$ nmap -p 80-85 bing.com
Outputs:
devops@devops:~$ nmap -p 80-85 bing.com
Starting Nmap 7.60 ( https://nmap.org ) at 2019-10-03 04:34 EDT
Nmap scan report for bing.com (204.79.197.200)
Host is up (0.048s latency).
Other addresses for bing.com (not scanned): 13.107.21.200 2620:1ec:c11::200
rDNS record for 204.79.197.200: a-0001.a-msedge.net
PORT STATE SERVICE
80/tcp open http
81/tcp filtered hosts2-ns
82/tcp filtered xfer
83/tcp filtered mit-ml-dev
84/tcp filtered ctf
85/tcp filtered mit-ml-dev
Nmap done: 1 IP address (1 host up) scanned in 1.55 seconds
devops@devops:~$
If you want to ping a protocol instead of the port number to a remote server (bing.com), type:
$ nmap -p http bing.com
Outputs:
devops@devops:~$ nmap -p http bing.com
Starting Nmap 7.60 ( https://nmap.org ) at 2019-10-03 04:35 EDT
Nmap scan report for bing.com (204.79.197.200)
Host is up (0.048s latency).
Other addresses for bing.com (not scanned): 13.107.21.200 2620:1ec:c11::200
rDNS record for 204.79.197.200: a-0001.a-msedge.net
PORT STATE SERVICE
80/tcp open http
8008/tcp filtered http
Nmap done: 1 IP address (1 host up) scanned in 1.50 seconds
Ping a Specific Port Number Using Telnet Command
You can also use another command named “telnet” to check the network connection to a specific port number of remote server. type:
$ telnet bing 80 $ telnet bing 443
Outputs:
devops@devops:~$ telnet bing.com 80 Trying 204.79.197.200... Connected to bing.com. Escape character is '^]'. devops@devops:~$ telnet bing.com 443 Trying 204.79.197.200... Connected to bing.com. Escape character is '^]'.
Ping a Specific Port Number Using Nc Command
You can also use Nc command to check connection on a specific port number to a remote server. type:
$ nc -vz {URL} {PortNUmber} $ nc -vz bing.com 80 $ nc -vz bing.com 443
Outputs:
devops@devops:~$ nc -vz bing.com 80 Connection to bing.com 80 port [tcp/http] succeeded! devops@devops:~$ nc -vz bing.com 443 Connection to bing.com 443 port [tcp/https] succeeded!
Conclusion
You should know that how to ping a specific port number to a remote system using nmap/nc/telnet commands in your Linux system.