Checking ports using Bash


When we want to check whether a host is reachable (at all), we can use the ping command:


It sends ICMP Echo Requests to the given host and reports how many packages are received and responded to. The option -c can be used to set how many requests are sent.

Checking specific endpoints

A disadvantage of ping is that we cannot configure a specific endpoint (= host + port) to ping. More advanced tools offer alternatives. The following command pings the FTP port of

nping -p 21

nping belongs to the nmap package. On Ubuntu, nmap can be installed as follows: sudo apt-get install nmap .

Another tool is  Netcat. The advantage of nc is that the error code indicates whether the endpoint was pinged successfully, making it useful for checking endpoint availability in scripts.

nc -w 1 21
echo $? # returns 0
nc -w 1 1
echo $? # returns 1

On Ubuntu, Netcat can be installed as follows: sudo apt-get install netcat .


  • [1] Nmap project site
  • [2] Netcat project site

Leave a Reply