Linux ping command
The Linux ping command is used to test the network connection to another host.
The ping command measures network response time and packet loss by sending data packets to the target host and waiting for replies.
Running the ping command uses the ICMP (Internet Control Message Protocol) protocol to send a request for a reply; if the remote host's network functions properly, it will reply, thus indicating that the host is operating normally.
Syntax
ping [选项] [目标主机/IP地址]
Parameter Description:
-c: specify the number of packets to send, for example-c 4means send 4 packets.-i: specify the interval between packets in seconds, for example-i 0.5means send one every 0.5 seconds.-w: set the upper limit of the waiting time for packets, after which it stops automatically, for example-w 5means wait 5 seconds.-s: specify the size of each packet in bytes; the default is 56 bytes.-t: set the time to live (TTL) of packets, specifying the number of routing hops.-q: quiet mode; display only the start and end statistics, not detailed information for each packet.-f: flood mode; send packets rapidly to test network capacity; use with caution.-l: specify the number of packets to send at once, typically used for load testing.-v: display detailed output information for debugging.-4: force the use of the IPv4 protocol.-6: force the use of the IPv6 protocol.
Example
1. Check connectivity with the host:
# ping www.runoob.com //ping主机
PING aries.m.alikunlun.com (114.80.174.110) 56(84) bytes of data.
64 bytes from 114.80.174.110: icmp_seq=1 ttl=64 time=0.025 ms
64 bytes from 114.80.174.110: icmp_seq=2 ttl=64 time=0.036 ms
64 bytes from 114.80.174.110: icmp_seq=3 ttl=64 time=0.034 ms
64 bytes from 114.80.174.110: icmp_seq=4 ttl=64 time=0.034 ms
64 bytes from 114.80.174.110: icmp_seq=5 ttl=64 time=0.028 ms
64 bytes from 114.80.174.110: icmp_seq=6 ttl=64 time=0.028 ms
64 bytes from 114.80.174.110: icmp_seq=7 ttl=64 time=0.034 ms
64 bytes from 114.80.174.110: icmp_seq=8 ttl=64 time=0.034 ms
64 bytes from 114.80.174.110: icmp_seq=9 ttl=64 time=0.036 ms
64 bytes from 114.80.174.110: icmp_seq=10 ttl=64 time=0.041 ms
--- aries.m.alikunlun.com ping statistics ---
10 packets transmitted, 30 received, 0% packet loss, time 29246ms
rtt min/avg/max/mdev = 0.021/0.035/0.078/0.011 ms
//Must be terminated manually Ctrl+C
2. Limit the number of packets sent:
# ping -c 2 www.runoob.com
PING aries.m.alikunlun.com (114.80.174.120) 56(84) bytes of data.
64 bytes from 114.80.174.120: icmp_seq=1 ttl=54 time=6.18 ms
64 bytes from 114.80.174.120: icmp_seq=2 ttl=54 time=15.4 ms
--- aries.m.alikunlun.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1016ms
rtt min/avg/max/mdev = 6.185/10.824/15.464/4.640 ms
//Exit automatically after receiving two packets
3. Using multiple options:-i 3 sets the send interval to 3 seconds -s sets the packet size -t sets the TTL value to 255
# ping -i 3 -s 1024 -t 255 g.cn //ping主机
PING g.cn (203.208.37.104) 1024(1052) bytes of data.
1032 bytes from bg-in-f104.1e100.net (203.208.37.104): icmp_seq=0 ttl=243 time=62.5 ms
1032 bytes from bg-in-f104.1e100.net (203.208.37.104): icmp_seq=1 ttl=243 time=63.9 ms
1032 bytes from bg-in-f104.1e100.net (203.208.37.104): icmp_seq=2 ttl=243 time=61.9 ms
--- g.cn ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 6001ms
rtt min/avg/max/mdev = 61.959/62.843/63.984/0.894 ms, pipe 2
**4. Set the packet interval:** send packets at intervals of 0.2 seconds; this option is suitable for testing short-term network stability.
ping -i 0.2 www.runoob.com
**5. Specify the packet size:** set the packet size to 128 bytes, suitable for checking stability when transferring larger amounts of data.
ping -s 128 www.runoob.com
**6. Set the network timeout:** limit the test time to 10 seconds, suitable for testing connectivity when the network environment is uncertain.
ping -w 10 www.example.com
Common output analysis
The output of the ping command typically includes the following information:
- Number of packets sent and received: shows how many packets were sent and received during the test, and the packet loss rate.
- Round-trip time statistics: shows round-trip times (in milliseconds), including minimum, maximum, average, and standard deviation.
PING www.example.com (93.184.216.34): 56 data bytes
64 bytes from 93.184.216.34: icmp_seq=0 ttl=56 time=10.1 ms
64 bytes from 93.184.216.34: icmp_seq=1 ttl=56 time=10.3 ms
--- www.example.com ping statistics ---
2 packets transmitted, 2 packets received, 0% packet loss
round-trip min/avg/max/stddev = 10.1/10.2/10.3/0.1 ms
Advanced applications
**1. Quickly check network connection:** send one packet with a 1-second timeout to quickly check whether the network is connected.
ping -c 1 -W 1 www.example.com
** 2. Test network stability with large packets:** send 1024-byte packets a total of 10 times to test network capacity.
ping -s 1024 -c 10 www.example.com
**3. IPv6 network test:** force the IPv6 protocol to test connectivity, suitable for IPv6 environments.
ping -6 www.example.com
Notes
pingBecause the ICMP protocol is used, some firewalls may block ICMP packets, which can cause thepingcommand to fail to work properly.- Flood mode (
-foption) sends packets quickly; it is recommended to use it in a safe network environment, otherwise it may be regarded as an attack.
Using the ping command, you can quickly check network connection status and troubleshoot network faults; it is an essential tool for every network administrator.
Linux Command Reference