In this tutorial, we’ll look at the different ways to check for a host’s network availability in Linux.
The ping command is a simple network utility command-line tool in Linux. It’s a handy tool for quickly checking a host’s network connectivity. It works by sending an ICMP message ECHO_REQUEST to the target host. If the host reply with ECHO_REPLY, then we can safely conclude that the host is available.
2.1. Checking Host’s Network Availability
For a given IP address, hostname, or fully qualified domain name (FQDN), we can check its availability using ping:
$ ping -c 4 www.baeldung.com PING www.baeldung.com (220.127.116.11): 56 data bytes 64 bytes from 18.104.22.168: icmp_seq=0 ttl=60 time=37.340 ms 64 bytes from 22.214.171.124: icmp_seq=1 ttl=60 time=14.541 ms 64 bytes from 126.96.36.199: icmp_seq=2 ttl=60 time=16.787 ms 64 bytes from 188.8.131.52: icmp_seq=3 ttl=60 time=57.872 ms --- www.baeldung.com ping statistics --- 4 packets transmitted, 4 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 14.541/31.635/57.872/17.561 ms
By default, the ping command will send packets indefinitely to the host until it’s terminated. To limit the number of packets that we send, we’ve specified the flag -c along with the value 4 to indicate only 4 packets should be sent to the host.
From the output, we can see that ping transmitted 4 packets, and it received 4 responses from the host. The fact that we’re receiving responses from the host means that the host is reachable via the network.
Additionally, the ping command also provides latency information between our node and the host. A statistical summary of the latency will also be provided.
Let’s attempt to ping a host that isn’t reachable via network:
$ ping -c 4 10.107.0.7 PING 10.107.0.7 (10.107.0.7): 56 data bytes Request timeout for icmp_seq 0 Request timeout for icmp_seq 1 Request timeout for icmp_seq 2 --- 10.107.0.7 ping statistics --- 4 packets transmitted, 0 packets received, 100.0% packet loss
Similarly, we’re only sending 4 packets to the host 10.107.0.7 using the flag -c.
From the output, we can see that we’ve transmitted 4 packets, but we’ve received 0 packets in response. This means that the host isn’t actively replying to our ECHO_REQUEST packet. It could mean the host is simply not available or is actively dropping all the ICMP packets. Either way, this means that the host is unable to be reached through the network.
Although ping might be good enough for a quick check, it is not suitable for scripting purposes, according to the man page.
While being very similar to ping in terms of its functionality, fping also allows the user to specify multiple hosts checked against in a single command. Besides that, the man page mentioned that fping is meant to be used in scripts.
Therefore, fping should be preferred over ping when we want to build sophisticated scripts to check hosts’ availability.
For Debian based Linux, we can install fping using the apt-get command:
$ apt-get install -y fping
On the other hand, we’ll use yum to install fping for RHEL based Linux:
$ yum install -y epel-release $ yum install -y fping
3.2. General Syntax
Let’s take a look at fping command syntax:
fping [ options ] [ systems... ]
The options are a list of optional flags that we can pass to the fping command. Then, the command accepts a list of hosts to be ping against in the place of systems.
3.3. Checking Multiple Hosts
Let’s ping multiple hosts using fping:
$ fping www.baeldung.com 10.107.0.7 www.baeldung.com is alive 10.107.0.7 is unreachable
Without surprise, the result is the same as when we check the hosts using ping. However, what’s different is that instead of issuing 2 ping commands, a single fping command followed by the list of hosts is sufficient.
Additionally, the output is much more concise as compared to the ping command. This could be beneficial if the intention is to simply check the availability and not the performance statistics.
3.4. Checking a Range of IPs
To check the network availability of a range of IP addresses, we can use fping to generate the list of IPs and check instead of typing the IP one by one. Concretely, we can check the availability of the IPs from 192.168.1.1 to 192.168.1.5 using fping:
$ fping -g 192.168.1.1 192.168.1.5
The flag -g will generate and ping the list of IPs, starting from the first IP until the second IP. Concretely, the command above will check the network availability of 192.168.1.1, 192.168.1.2, 192.168.1.3, 192.168.1.4, and 192.168.1.5.
We can also provide the range with a CIDR notation:
$ fping -g 192.168.1.0/29
The command above will generate a list of IPs starting from 192.168.1.0 to 192.168.1.7 and ping against them.
3.5. Printing Only Unreachable Hosts in Output
Sometimes we might want to only get a list of unreachable hosts. Using fping, we can return only the unreachable hosts:
$ fping -u www.baeldung.com 10.107.0.7 10.107.0.7
With the flag -u, the output is further trimmed to only unreachable show hosts.
nc is a utility tool that allows us to perform tasks related to TCP and UDP in Linux.
Contrary to ping and fping that uses ICMP packets, nc allows us to connect directly to a TCP/UDP port. This provides the added benefits of checking if a particular port is open, in addition to the network availability of a host.
4.1. Checking the Availability of a Port on Host
For any given host, we can check if a particular port is open and listening using nc:
$ nc -z www.baeldung.com 443 $ echo $? 0
The flag -z will instruct nc to check whether the specified port is open on the host rather than opening up a connection.
Based on the exit code 0 from the nc command, we can conclude that the host www.baeldung.com has its port 443 opened.
Let’s now check for port 444, which should be closed for incoming connection:
$ nc -w 3 -z www.baeldung.com 444 $ echo $? 1
In the command, we’ve additionally specified a timeout value of 3 seconds using the flag -w.
As expected, the exit code of nc is 1, which means port 444 of the host www.baeldung.com is not accepting connections.
The same thing happens if we attempt to check against a nonexistent host:
$ nc -w 3 -z 10.107.0.7 443 $ echo $? 1
Since the host 10.107.0.7 is unreachable, the nc command returned an exit code of 1.
In this article, we’ve seen how can we quickly check a host’s network availability using ping.
Then, we’ve introduced fping as a superior option due to the additional functionalities it offers. These functionalities including the ability to ping multiple hosts, either by providing exact IPs or by a range. Besides that, fping also produces much more concise output, which is useful for scripting purposes.
Finally, we’ve also explored the option to check for the availability of a particular port on the host using nc.