Linux ping command

Linux 命令大全Linux Command Reference

The Linux ping command is used to detect the network connection to another host.

The ping command sends data packets to the target host and waits for a response, allowing you to measure network response time and packet loss rate.

The ping command uses the ICMP (Internet Control Message Protocol) transport protocol to send a request for a response. If the remote host's network function is working properly, it will reply to the message, thus confirming that the host is operating normally.

Syntax

ping [选项] [目标主机/IP地址]

Parameter Description:

  • -c: Specifies the number of data packets to send, for example-c 4means sending 4 data packets.
  • -i: Specifies the interval between sending packets (in seconds), for example-i 0.5means sending once every 0.5 seconds.
  • -w: Sets the upper limit of waiting time for sending packets; it stops automatically after that time, for example-w 5means waiting 5 seconds.
  • -s: Specifies the size of each data packet (in bytes), the default is 56 bytes.
  • -t: Sets the Time to Live (TTL) of the packet, specifying the number of routing hops.
  • -q: Quiet mode, only displays the start and end statistics, not the detailed information of each packet.
  • -f: Flood mode, sends packets rapidly, used to test network carrying capacity; use with caution.
  • -l: Specifies the number of packets sent at once, usually used for load testing.
  • -v: Displays detailed output information for debugging.
  • -4: Forces the use of IPv4 protocol.
  • -6: Forces the use of IPv6 protocol.

Examples

1. Check whether the host is reachable:

# ping www.example.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

//需要手动终止Ctrl+C

2. Limit the number of packets sent:

# ping -c 2 www.example.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

//收到两次包后,自动退出

3. Using multiple parameters:-i 3 sets the sending 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:Sends packets at an interval of 0.2 seconds. This parameter is suitable for testing network stability over a short period.

ping -i 0.2 www.example.com

5. Specify the packet size:Sets the packet size to 128 bytes, suitable for checking stability during larger data transfers.

ping -s 128 www.example.com

6. Set network timeout:Limits the test time to 10 seconds, suitable for testing connectivity in uncertain network environments.

ping -w 10 www.example.com

Common Output Explanation

The output of the ping command usually contains the following information:

  • Number of packets sent and received: Shows how many packets were sent and received during the test, as well as the packet loss rate.
  • Round-trip time statistics: Shows round-trip times such as minimum, maximum, average, and standard deviation (in milliseconds).
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 Usage

1. Quickly detect network connectivity:Send one packet and set a timeout of 1 second 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 carrying capacity.

ping -s 1024 -c 10 www.example.com

3. IPv6 network test:Force the use of IPv6 protocol to test connectivity, suitable for IPv6 environments.

ping -6 www.example.com

Notes

  • pingSince it uses the ICMP protocol, some firewalls may block ICMP packets, which can cause thepingcommand to fail to work properly.
  • Flood mode (-fparameter) sends packets very quickly; it is recommended to use it in a secure network environment, otherwise it may be considered an attack.

Using the ping command, you can quickly detect network connection status and troubleshoot network faults. It is an essential tool for every network administrator.

Linux 命令大全Linux Command Reference

Other Extensions