Linux command FAQ: Can you share some examples of the Linux ping command?
You typically use the Linux ping command to test to see whether a remote server is up and running, or to test problems with a network. For instance, my internet connection here in Alaska tends to get flaky from time to time, and when things appear to be going bad, I use the ping command to verify the problem.
In its basic use, you just issue the ping command followed by the name of a server or website, like this:
The ping command then sends little packets of information to the remote server, and if all goes well, they are acknowledged by that remote server. (From a security perspective, that remote server must also be configured to respond to ping requests. Some Linux administrators disable this feature.)
For instance, when my network connection is working fine, here's what the ping command results look like when I ping Google:
$ ping www.google.com PING www.l.google.com (188.8.131.52): 56 data bytes 64 bytes from 184.108.40.206: icmp_seq=0 ttl=50 time=48.582 ms 64 bytes from 220.127.116.11: icmp_seq=1 ttl=50 time=48.655 ms 64 bytes from 18.104.22.168: icmp_seq=2 ttl=50 time=49.171 ms 64 bytes from 22.214.171.124: icmp_seq=3 ttl=50 time=50.554 ms ^C --- www.l.google.com ping statistics --- 4 packets transmitted, 4 packets received, 0% packet loss round-trip min/avg/max/stddev = 48.582/49.204/50.554/0.691 ms
The thing I always look at is the "time" information, shown at the end of each line of output. For me, anything under 50 msec usually indicates that my network connection is working fine. When my network connection goes bad, the output of that time field fluctuates drastically (at which point I call my internet service provider).
One important thing to note: When you issue the ping command as shown, you normally use the [Control][c] key sequence to stop the command. That's how you do things on Linux, Unix, and Mac OS X systems; the last time I tried this on Windows the ping command ran for only a few times and then stopped itself, so things may still be different there.
If you remember the movie The Hunt for Red October, there's a point at which Sean Connery is in the submarine, and he tells the other man, "Give me one ping." This was a pretty funny moment for Unix administrators like myself who had used the ping command for so many years, because it was a great, real world description of the ping command.
It also demonstrated something else: There are times when you don't want the ping command to run forever, you may just want to issue one ping, five pings, or ten pings, etc. In that case, you use the -c option ("count") of the ping command to control the number of pings issued, like this:
$ ping -c 5 www.google.com PING www.l.google.com (126.96.36.199): 56 data bytes 64 bytes from 188.8.131.52: icmp_seq=0 ttl=50 time=49.198 ms 64 bytes from 184.108.40.206: icmp_seq=1 ttl=50 time=46.662 ms 64 bytes from 220.127.116.11: icmp_seq=2 ttl=50 time=52.202 ms 64 bytes from 18.104.22.168: icmp_seq=3 ttl=50 time=50.108 ms 64 bytes from 22.214.171.124: icmp_seq=4 ttl=50 time=51.690 ms --- www.l.google.com ping statistics --- 5 packets transmitted, 5 packets received, 0% packet loss round-trip min/avg/max/stddev = 46.662/49.972/52.202/1.975 ms
You can also use the ping command to ping a port on a Unix or Linux system. Here's the description from the Linux ping man page:
-p port Set the base UDP port number used in probes. This option is used with the -U option. The default base port number is 33434. The ping utility starts setting the destination port number of UDP packets to this base and increments it by one at each probe.
Honestly, I don't know much about this, and I don't have a need for this, so I'll leave it at that. One writer suggested using a port-scanning tool named nmap if you need to "ping" a port. nmap lets you see whether ports are "open" (listening), and I use it to test open ports on firewall setups, so in regards to trying to "ping a port", that's a good suggestion.
There are more things you can do with the Unix/Linux ping command, but far and away these are the most common ping command options I use. For more information, use the Unix man command to get more information on the ping command, like this:
One final note about the ping command: It has become much more common for Unix and Linux administrators to turn off the ping command on their servers. So, if you can't ping one of your own servers, make sure you have the ping service enabled on that server, or that a firewall isn't blocking your ping attempts.