Bash ping Command - Send Request to Network Hosts
Using the ping Command
The ping command is used to send ICMP ECHO_REQUEST to network hosts.
It's a useful tool for checking network connectivity and diagnosing network issues.
Basic Usage
To ping a host, use ping hostname:
Example
ping google.com
PING google.com (172.217.14.206) 56(84) bytes of data.
64 bytes from 172.217.14.206: icmp_seq=1 ttl=118 time=14.5 ms
64 bytes from 172.217.14.206: icmp_seq=2 ttl=118 time=14.2 ms
64 bytes from 172.217.14.206: icmp_seq=3 ttl=118 time=14.3 ms
64 bytes from 172.217.14.206: icmp_seq=4 ttl=118 time=14.4 ms
^C
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 14.200/14.350/14.500/0.112 ms
On Linux, ping keeps sending packets until you press Ctrl+C. Then it prints the statistics.
Options
The ping command has options to change how it works:
-c- Send a specific number of ping requests-i- Wait a specific number of seconds between sending each packet-t- Set the IP Time to Live (TTL)-q- Quiet output, only show summary-s- Specify the number of data bytes to be sent
Send a Specific Number of Ping Requests
The -c option allows you to send a specific number of ping requests.
This is useful for limiting the number of packets sent.
Example: Send a Specific Number of Ping Requests
ping -c 4 google.com
PING google.com (172.217.14.206) 56(84) bytes of data.
64 bytes from 172.217.14.206: icmp_seq=1 ttl=118 time=14.5 ms
64 bytes from 172.217.14.206: icmp_seq=2 ttl=118 time=14.2 ms
64 bytes from 172.217.14.206: icmp_seq=3 ttl=118 time=14.3 ms
64 bytes from 172.217.14.206: icmp_seq=4 ttl=118 time=14.4 ms
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 14.200/14.350/14.500/0.112 ms
Understanding Ping Results
The output of the ping command provides several key pieces of information:
- Bytes: The size of the ICMP packet sent
- Time: The round-trip time it took for the packet to reach the host and return, measured in milliseconds
- TTL (Time to Live): The remaining lifespan of the packet, which decreases by one for each hop
- Packet Loss: The percentage of packets that were lost during transmission
- Round-Trip Time Statistics: Includes minimum, average, maximum, and mean deviation (mdev) of the round-trip times
In the example above:
- Each packet is 64 bytes
- The round-trip times vary slightly, indicating network latency
- The TTL value is 118, showing the packet's remaining lifespan
- There is 0% packet loss, indicating a stable connection
- The round-trip time statistics provide insights into network performance
Wait a Specific Number of Seconds Between Sending Each Packet
The -i option allows you to wait a specific number of seconds between sending each packet.
This can be useful for reducing network traffic when using ping.
Example: Wait a Specific Number of Seconds Between Sending Each Packet
ping -c 2 -i 2 google.com
PING google.com (172.217.14.206) 56(84) bytes of data.
64 bytes from 172.217.14.206: icmp_seq=1 ttl=118 time=14.5 ms
64 bytes from 172.217.14.206: icmp_seq=2 ttl=118 time=14.2 ms
--- google.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 14.200/14.350/14.500/0.150 ms
Set the IP Time to Live (TTL)
The -t option allows you to set the IP Time to Live (TTL).
This determines the maximum number of hops a packet can take before being discarded.
The TTL you set applies to the packets you send. The ttl= value in each reply is set by the remote host, so it does not change.
Example: Set the IP Time to Live (TTL)
ping -c 1 -t 64 google.com
PING google.com (172.217.14.206) 56(84) bytes of data.
64 bytes from 172.217.14.206: icmp_seq=1 ttl=118 time=14.5 ms
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 14.500/14.500/14.500/0.000 ms
Quiet Output
The -q option enables quiet output, only showing the summary of the ping results.
This is useful for scripts or when you only need the final statistics.
Example: Quiet Output
ping -c 4 -q google.com
PING google.com (172.217.14.206) 56(84) bytes of data.
--- google.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 14.200/14.350/14.500/0.112 ms
Specify Data Bytes
The -s option allows you to specify the number of data bytes to be sent.
This can be useful for testing network performance with different packet sizes.
Example: Specify Data Bytes
ping -c 1 -s 128 google.com
PING google.com (172.217.14.206) 128(156) bytes of data.
136 bytes from 172.217.14.206: icmp_seq=1 ttl=118 time=14.5 ms
--- google.com ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 14.500/14.500/14.500/0.000 ms