Get the highlights in your inbox every week.
Diagnose connectivity issues with the Linux ping command | Opensource.com
Diagnose connectivity issues with the Linux ping command
One of the most fundamental diagnostic tools for networked connectivity is the ping command.
Networked computers are so common these days that most of us take it for granted that a computer on one side of a room can contact one on the other side of the room, much less the other side of the world. When it works as designed, networking is what makes the Internet, the cloud, file shares, media streaming, remote administration, printing, and much more possible. When something goes wrong, it can sometimes be challenging to diagnose. One of the most fundamental diagnostic tools for networked connectivity is the
The basic ping
When you can't reach a computer on your local network, or a server on the Internet, you can ping it. A ping sends an Internet Control Message Protocol (ICMP) packet to a destination IP address. ICMP is, by design, a rudimentary format used mostly for diagnostics: It's essentially a call and response signal.
But there's an order to troubleshooting, and it starts as close to home as possible. When in doubt, first ping your own computer to ensure you're running a networking stack. The computer you're operating is also called your localhost, and it has a special IP address assigned for speaking to itself: 188.8.131.52.
ping command understands the localhost hostname, its IP address, and a shortcut of just
You can control how many signals you send with the
-c (as in count) option.
$ ping 0 -c1
PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.069 ms
--- 0 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.069/0.069/0.069/0.000 ms
After you've established that your local networking stack is up and running, you can ping your router. The address of a router usually starts with 192,168, or 10. The exact IP address depends on your router's configuration.
When you don't specify how many pings to send, you can stop
ping from running with Ctrl+C.
$ ping 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
From 192.168.0.100: icmp_seq=2 Redirect Host(New nexthop: 192.168.0.1)
From 192.168.0.100: icmp_seq=3 Redirect Host(New nexthop: 192.168.0.1)
From 192.168.0.100: icmp_seq=4 Redirect Host(New nexthop: 192.168.0.1)
From 192.168.0.100: icmp_seq=5 Redirect Host(New nexthop: 192.168.0.1)
If you can reach your router, that means your wired or wireless connection is working.
What about other hosts on my network? You can ping all kinds of devices. Not all are guaranteed to respond (some devices drop ICMP packets), but many do. For instance, I can ping my printer:
$ ping 192.168.0.4
Pinging beyond your network
Beyond establishing that your own network is working as expected, you can also ping out into the wider world beyond your router. Again, not all servers are permitted to receive, much less respond to, ICMP. However, there are some that do, and a vital server to the working of the Internet is a nameserver.
Google's DNS server is relatively easy to remember, and it does respond to pings:
$ ping -c 2 184.108.40.206
PING 220.127.116.11 (18.104.22.168) 56(84) bytes of data.
64 bytes from 22.214.171.124: icmp_seq=1 ttl=116 time=53.3 ms
64 bytes from 126.96.36.199: icmp_seq=2 ttl=116 time=53.5 ms
--- 188.8.131.52 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 53.304/53.424/53.544/0.120 ms
When a site has apparently disappeared, you might be able to probe the worldwide DNS network to find out what its host server's address is, and then ping that server. This at least tells you whether the host is down or whether it's just a web server issue.
For example, say you're trying unsuccessfully to reach example.com. First, find the IP address using the
$ host example.com
example.com has address 184.108.40.206
example.com has IPv6 address 2606:2800:220:1:248:1893:25c8:1946
example.com mail is handled by 0
And then ping the website's host by IP:
$ ping 220.127.116.11 -c 1
Ping and IPv6
Ping works over IPv4 as well as IPv6. Using only one of them explicitly can be enforced by specifying
You can change the size of the ICMP packets you're sending with the
-s option. The default packet size is 56, which translates into 64 ICMP data bytes when combined with the 8-byte header. This command sends 43 bytes:
$ ping -s 35 -c 5 18.104.22.168
You can print a timestamp before each ping report in your terminal with the
-D option. This provides the UNIX epoch time, plus microseconds:
$ ping -D 22.214.171.124
PING 126.96.36.199 (188.8.131.52) 56(84) bytes of data.
[1634013430.297468] 64 bytes from 184.108.40.206: icmp_seq=1 ttl=116 time=53.3 ms
[1634013431.298738] 64 bytes from 220.127.116.11: icmp_seq=2 ttl=116 time=53.1 ms
You can change the time interval between pings using the
-i option. This changes the ping interval to two seconds:
$ ping -i 2
You can also stop pinging after some value of time (in seconds) with the
$ ping -w 6
There are many implementations of ping. The
iputils package provides a
ping command, Busybox has a
ping command, and there's one from BSD and others. There's even a GUI for
ping: Gping is available for Linux, macOS, and Windows. You can find more information for
gping on Github.
Learn to ping
ping command is simple, but it can be eyes and ears out on the vast expanse that is your network. Next time you have connectivity issues, let
ping be the first tool you turn to.