ping Command Tutorial with Examples in Linux

This tutorial explains Linux “ping” command, options and its usage with examples.

ping – send ICMP ECHO_REQUEST to network hosts

DESCRIPTION

PING (Packet INternet Groper) command is the best way to test connectivity between two nodes. Whether it is Local Area Network (LAN) or Wide Area Network (WAN). Ping use ICMP (Internet Control Message Protocol) to communicate to other devices. You can ping host name of ip address using below command.

ping uses the ICMP protocol’s mandatory ECHO_REQUEST datagram to elicit an ICMP ECHO_RESPONSE from a host or gateway. ECHO_REQUEST datagrams (“pings”) have an IP and ICMP header, followed by a struct timeval and then an arbitrary number of “pad” bytes used to fill out the packet.

SYNOPSIS

ping -s [-d] [-l] [-L] [-n] [-r] [-R] [-v] [ -i interface_address ] [-I interval] [-t ttl] host [packetsize] [count]

OPTIONS :

advertisement
advertisement

-d
Set the SO_DEBUG socket option.
-l
Loose source route. Use this option in the IP header to send the packet to the given host and back again. Usually specified with the -R option.
-L
Turn off loopback of multicast packets. Normally, if there are members in the host group on the out- going interface, a copy of the multicast packets will be delivered to the local machine.
-n
Show network addresses as numbers. ping normally displays addresses as host names.
-r
Bypass the normal routing tables and send directly to a host on an attached network. If the host is not on a directly-attached network, an error is returned. This option can be used to ping a local host through an interface that has been dropped by the router daemon.
-R
Record route. Sets the IP record route option, which will store the route of the packet inside the IP header. The contents of the record route will only be printed if the -v option is given, and only be set on return packets if the target host preserves the record route option across echos, or the -l option is given.
-v
Verbose output. List any ICMP packets, other than ECHO_RESPONSE, that are received.
-i interface_address
Specify the outgoing interface address to use for multicast packets. The default interface address for multicast packets is determined from the (unicast) routing tables.
-I interval
Specify the interval between successive transmissions. The default is one second.
-t ttl
Specify the IP time to live for unicast and multicast packets. The default time to live for unicast packets is set with ndd (using the icmp_def_ttl variable). The default time to live for multicast is one hop.
host
The network host.
packetsize
Specified size of packetsize. Default is 64.
count
Amount of times to send the ping request.

EXAMPLES

1. Ping the host to see if its alive

$ ping google.com
PING google.com (74.125.200.102) 56(84) bytes of data.
64 bytes from plus.google.com (74.125.200.102): icmp_req=1 ttl=128 time=172 ms
64 bytes from plus.google.com (74.125.200.102): icmp_req=2 ttl=128 time=164 ms
64 bytes from plus.google.com (74.125.200.102): icmp_req=4 ttl=128 time=165 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 3 received, 25% packet loss, time 3013ms
rtt min/avg/max/mdev = 164.618/167.289/172.010/3.364 ms

2. Increase or Decrease the Time Interval Between Packets

Note: Join free Sanfoundry classes at Telegram or Youtube

Increase Ping Time Interval
Example: Wait for 5 seconds before sending the next packet.

$ ping -i 5 google.com

Decrease Ping Time Interval
Example: Wait 0.1 seconds before sending the next packet.

# ping -i 0.1 google.com

Note: Only super user can specify interval less than 0.2 seconds. If not, you’ll get the following error message.

advertisement

3. Send N packets and stop

$ ping -c 4 google.com
PING google.com (74.125.135.100) 56(84) bytes of data.
64 bytes from plus.google.com (74.125.135.100): icmp_req=1 ttl=128 time=251 ms
64 bytes from plus.google.com (74.125.135.100): icmp_req=2 ttl=128 time=180 ms
64 bytes from plus.google.com (74.125.135.100): icmp_req=3 ttl=128 time=179 ms
64 bytes from plus.google.com (74.125.135.100): icmp_req=4 ttl=128 time=179 ms
 
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 179.569/197.734/251.433/31.005 ms

4. Flood the network

Super users can send hundred or more packets per second using -f option. It prints a ‘.’ when a packet is sent, and a backspace is printed when a packet is received.

advertisement
# ping -f localhost
PING localhost (127.0.0.1) 56(84) bytes of data.
.^ 
--- localhost ping statistics ---
215594 packets transmitted, 215594 received, 0% packet loss, time 9417ms
rtt min/avg/max/mdev = 0.004/0.006/1.096/0.006 ms, ipg/ewma 0.043/0.006 ms

5. Print Only Ping Command Summary Statistics

$ ping -c 5 -q 127.0.0.1 
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
 
--- 127.0.0.1 ping statistics ---
5 packets transmitted, 5 received, 0% packet loss, time 3999ms
rtt min/avg/max/mdev = 0.041/0.049/0.055/0.009 ms

6. Change Ping Packet Size

Example: Change the default packet size from 56 to 100.

$ ping -s 100 localhost
PING localhost (127.0.0.1) 100(128) bytes of data.
108 bytes from localhost (127.0.0.1): icmp_seq=1 ttl=64 time=0.022 ms
108 bytes from localhost (127.0.0.1): icmp_seq=2 ttl=64 time=0.021 ms
108 bytes from localhost (127.0.0.1): icmp_seq=3 ttl=64 time=0.020 ms
^C
--- localhost ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 1998ms
rtt min/avg/max/mdev = 0.020/0.021/0.022/0.000 ms

Note : Ping Bytes Sent = Ping Packet Size + Ping Header Packet Size (28 bytes)

7. Timeout

The following example will ping for 5 seconds. i.e ping command will exit after 5 seconds irrespective of how many packets are sent or received.

$ ping -w 5 localhost

8. Specify path for ping to send the packet

You can also specify through which path the ping should send the packet to destination.

$ ping 192.168.3.33 192.168.7.1 192.168.4.45

Note: If one of the hop in the path is not reachable then you will have failure in pinging.

9. Record and print route of how ECHO_REQUEST sent and ECHO_REPLY received

$ ping -R 192.168.1.63
PING 192.168.1.63 (192.168.1.63) 56(84) bytes of data.
64 bytes from 192.168.1.63: icmp_seq=1 ttl=61 time=2.05 ms
RR:   192.168.9.118
        192.168.3.25
        192.168.10.35
        192.168.1.26
        192.168.1.63
        192.168.1.63
        192.168.10.4
        192.168.3.10
        192.168.4.25
64 bytes from 192.168.1.63: icmp_seq=2 ttl=61 time=2.00 ms      (same route)

10. Audible ping: Give beep when the peer is reachable

$ ping -a IP

Note: It can give beep only from terminal number 1 through 7 and gnome-terminal ( It will not work in console ).

Sanfoundry Global Education & Learning Series – 1000 Linux Tutorials.

If you wish to look at all Linux commands and their usage examples, go to Linux Commands Tutorial.

If you find any mistake above, kindly email to [email protected]

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.