Ping Command

The ping command is one of the popular command used in linux for troubleshooting, testing and diagnosing network connectivity issues.

The ping command sends a request to destination ip or domain name and wait for the reply if request is received by other party it will respond back with ICMP (Internet Control Message Protocol) echo reply.

The ping command will continue to send ICMP packages to the Destination IP address until it receives an interrupt. To stop the command, just hit the Ctrl+C key combination.

Syntax

ping [OPTIONS] [IP or Domain]

Let's have a look at the example:

# ping google.com
ping google.com

# output of above command
PING google.com (172.217.164.238): 56 data bytes
64 bytes from 172.217.164.238: icmp_seq=0 ttl=54 time=5.985 ms
64 bytes from 172.217.164.238: icmp_seq=1 ttl=54 time=5.860 ms
64 bytes from 172.217.164.238: icmp_seq=2 ttl=54 time=6.551 ms
^C
--- google.com ping statistics ---
3 packets transmitted, 3 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 5.860/6.132/6.551/0.301 ms

The ping command resolves the domain name into an IP address and starts sending ICMP packages to the destination IP.

If the destination IP is reachable it will respond back and the ping command prints a line that includes the following fields:

# sample output
64 bytes from 172.217.164.238: icmp_seq=0 ttl=54 time=5.985 ms

# explanation 
number of bytes: 64 ICMP data bytes
IP address: from 172.217.164.238:
The ICMP sequence number for each packet: icmp_seq=0
The Time to Live: ttl=54
The ping time: time=5.985 ms

Send N packets and stop

To limit number of packets using ping command use following command:

# command syntax
ping -c [packets] destination

# example
ping -c 1 google.com

# sample output of above command
PING google.com (172.217.164.206): 56 data bytes
64 bytes from 172.217.164.206: icmp_seq=0 ttl=54 time=4.422 ms

--- google.com ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 4.422/4.422/4.422/0.000 ms

Increase Ping Time Interval

Example: Wait for 5 seconds before sending the next packet.

# increase the interval time
ping -i 5 google.com

# decrease default interval time
ping -i 0.1 google.com

Check whether the local network interface is up and running

# ping without name
ping 0

# example using name
ping localhost

# ping using ip
ping 127.0.0.1

Flood the network

Super users can send hundred or more packets per second using -f option.

sudo ping -f localhost

Give beep when the peer is reachable

Using -a option it can beep when target is reachable:

ping -a google.com

Print Only Ping Command Summary Statistics

Use option -q to view only the ping statistics summary as shown below:

# example command
ping -c 5 -q 127.0.0.1

# example output
PING 127.0.0.1 (127.0.0.1): 56 data bytes

--- 127.0.0.1 ping statistics ---
5 packets transmitted, 5 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.045/0.076/0.120/0.027 ms

Change Ping Packet Size

To change the default packet size from 56 to N use -s option as below:

# example command
ping -s 100 localhost

# example output of above command
PING localhost (127.0.0.1): 100 data bytes
108 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.044 ms
^C
--- localhost ping statistics ---
1 packets transmitted, 1 packets received, 0.0% packet loss
round-trip min/avg/max/stddev = 0.044/0.044/0.044/0.000 ms

In above example event though we specified 100 bytes it displays 108 bytes this is because it adds the header byte size along with the packet size specified.