The ping command is one of the most common commands used to troubleshoot, test, and diagnose network connection problems.

Ping works by sending one or more ICMP (Internet Control Message Protocol) Echo request packets to a specified destination IP address and waiting for a response. When the destination IP receives a packet, it responds with an ICMP reply.

Using the ping command, we can determine whether the remote destination IP is active or inactive, find the round-trip delay in communication with the destination, and check for packet loss.

The ping command is part of the iputils (or iputils-ping) package that is pre-installed on most Linux distributions. The ping command is also available on Windows, MacOSX, and FreeBSD.

The command format

Ping [parameter] [Host name or IP address]

Command function

The ping command is used to check the status of the network and external hosts. Tracking and isolating hardware and software problems; Test, evaluate, and manage the network. If the host is running and connected to the network, it responds to the echo signal. Each echo signal request contains an Internet Protocol (IP) and ICMP header, followed by a Tim structure, and enough bytes to fill in the packet. The default is to send back signal requests continuously until an interrupt signal is received (CtrL-c).

The ping command sends one datagram per second and prints one line of output for each received response. The ping command calculates signal round trip times and packet loss statistics, and displays a brief summary when done. The ping command ends when the program times out or when a SIGINT signal is received. The Host parameter is either a valid Host name or Internet address.

The command parameter

  • -d Uses the SO_DEBUG function of the Socket.
  • -f Limit detection. Send network packets to a machine in large numbers and quickly to see how it responds.
  • -n Outputs only numeric values.
  • -q displays no information about the sent packet, only the final result.
  • -r Directly sends the packets to the remote host, ignoring the common Routing Table. It is usually to check whether there is a problem with the local network interface.
  • -r Records the routing process.
  • -v Displays the detailed command execution process.
  • -c Number: stops after sending a specified number of packets.

  • -i Seconds: Indicates the interval at which a network packet is sent to a machine. The default value is once a second.
  • -i Network interface: sends packets using the specified network interface.
  • -l Preload: Sets the packet to be sent before the requested message is sent.
  • -p Template style: Sets the template style of filled packets.
  • -s bytes: specifies the number of data bytes to be sent. The default value is 56, plus the 8-byte ICMP header, making a total of 64ICMP data bytes.
  • -t TTL: Sets the TTL value.

Ping succeeds

> ping 8.8.8.8 ping 8.8.8.8 (8.8.8.8): 56 data bytes 64 bytes from 8.8.8.8: Icmp_seq =0 TTL =106 time=255.389 ms 64 bytes from 8.8.8.8: ICmp_seq =1 TTL =106 time=253.781 ms 64 bytes from 8.8.8.8: Icmp_seq =2 TTL =106 time=258.403 ms 64 bytes from 8.8.8.8: ICmp_seq =3 TTL =106 time=255.721 ms 64 bytes from 8.8.8.8: icmp_seq=3 TTL =106 time=255.721 ms 64 bytes from 8.8.8.8: Icmp_seq =4 TTL =106 time=255.086 ms 64 bytes from 8.8.8.8: ICmp_seq =5 TTL =106 time=255.165 ms 64 bytes from 8.8.8.8: icmp_seq=5 TTL =106 time=255.165 ms 64 bytes from 8.8.8.8: Icmp_seq =6 TTL =106 time=253.948 ms...Copy the code

The ping fails

> ping 8.8.9.9
PING 8.8.9.9 (8.8.9.9): 56 data bytes
Request timeout for icmp_seq 0
Request timeout for icmp_seq 1
Request timeout for icmp_seq 2
Request timeout for icmp_seq 3
Request timeout for icmp_seq 4
Request timeout for icmp_seq 5
Request timeout for icmp_seq 6
...
Copy the code

Ping specified times

> ping -c 10 8.8.8.8 ping 8.8.8.8 (8.8.8.8): 56 data bytes 64 bytes from 8.8.8.8: Icmp_seq =0 TTL =106 time=256.369 ms 64 bytes from 8.8.8.8: ICmp_seq =1 TTL =106 time=255.471 ms 64 bytes from 8.8.8.8: icmp_seq=1 TTL =106 time=255.471 ms 64 bytes from 8.8.8.8: Icmp_seq =2 TTL =106 time=255.381 ms 64 bytes from 8.8.8.8: ICmp_seq =3 TTL =106 time=255.038 ms 64 bytes from 8.8.8.8: icmp_seq=3 TTL =106 time=255.038 ms 64 bytes from 8.8.8.8: Icmp_seq =4 TTL =106 time=260.055 ms 64 bytes from 8.8.8.8: ICmp_seq =5 TTL =106 time=255.733 ms 64 bytes from 8.8.8.8: icmp_seq=5 TTL =106 time=255.733 ms 64 bytes from 8.8.8.8: Icmp_seq =6 TTL =106 time=254.986 ms 64 bytes from 8.8.8.8: ICmp_seq =7 TTL =106 time=254.084 ms 64 bytes from 8.8.8.8: icmp_seq=7 TTL =106 time=254.084 ms 64 bytes from 8.8.8.8: Icmp_seq =8 TTL =106 time=255.902 ms 64 bytes from 8.8.8.8: Icmp_seq =9 TTL =106 time= 126.129ms -- 8.8.8.8 ping statistics -- 30 packets transmitted, 30 packets received 0.0% packet loss round - trip min/avg/Max/stddev 260.055/1.513 = 254.084/255.914 / msCopy the code

Interval and number of pings

> ping -c 10 -i 0.5 8.8.8.8 ping 8.8.8.8 (8.8.8.8): 56 data bytes 64 bytes from 8.8.8.8: Icmp_seq =0 TTL =106 time=255.032 ms 64 bytes from 8.8.8.8: ICmp_seq =1 TTL =106 time=256.937 ms 64 bytes from 8.8.8.8: icmp_seq=1 TTL =106 time=256.937 ms 64 bytes from 8.8.8.8: Icmp_seq =2 TTL =106 time=255.129 ms 64 bytes from 8.8.8.8: ICmp_seq =3 TTL =106 time=256.560 ms 64 bytes from 8.8.8.8: icmp_seq=3 TTL =106 time=256.560 ms 64 bytes from 8.8.8.8: Icmp_seq =4 TTL =106 time=264.229 ms 64 bytes from 8.8.8.8: ICmp_seq =5 TTL =106 time=254.791 ms 64 bytes from 8.8.8.8: icmp_seq=5 TTL =106 time=254.791 ms 64 bytes from 8.8.8.8: Icmp_seq =6 TTL =106 time=254.212 ms 64 bytes from 8.8.8.8: ICmp_seq =7 TTL =106 time=254.998 ms 64 bytes from 8.8.8.8: Icmp_seq =8 TTL =106 time=255.029 ms 64 bytes from 8.8.8.8: Icmp_seq =9 TTL =106 time=255.559 ms -- 8.8.8.8 ping statistics -- 10 packets transmitted, 10 packets received 0.0% packet loss round - trip min/avg/Max/stddev 264.229/2.771 = 254.212/256.248 / msCopy the code

Ping the domain name through the domain name

> ping -c 5 json.im ping json.im (150.109.147.28): 56 data bytes 64 bytes from 150.109.147.28: Icmp_seq =0 TTL =48 time=28.733 ms 64 bytes from 150.109.147.28: Icmp_seq =1 TTL =48 time=28.412 ms 64 bytes from 150.109.147.28: Icmp_seq =2 TTL =48 time=28.012 ms 64 bytes from 150.109.147.28: Icmp_seq =3 TTL =48 time=31.498 ms 64 bytes from 150.109.147.28: Icmp_seq =4 TTL =48 time=30.518 ms -- im ping statistics -- 5 packets transmitted, 5 packets received 0.0% packet loss round - trip min/avg/Max/stddev 31.498/1.341 = 28.012/29.435 / msCopy the code

Multi-parameter use

> ping -i 1 -S 1024 -t 123 json. Im ping json.im (150.109.147.28): 1024 data bytes 1032 bytes from 150.109.147.28: Icmp_seq =0 TTL =48 time=29.613 ms 1032 bytes from 150.109.147.28: Icmp_seq =1 TTL =48 time= 28.530ms 1032 bytes from 150.109.147.28: Icmp_seq =2 TTL =48 time=32.768 ms 1032 bytes from 150.109.147.28: Icmp_seq =3 TTL =48 time=29.014 ms 1032 bytes from 150.109.147.28: Icmp_seq =4 TTL =48 time=30.239 ms 1032 bytes from 150.109.147.28: Icmp_seq =5 TTL =48 time=27.815 ms 1032 bytes from 150.109.147.28: Icmp_seq =6 TTL =48 time=31.311 ms 1032 bytes from 150.109.147.28: Icmp_seq =7 TTL =48 time=34.554 ms 1032 bytes from 150.109.147.28: Icmp_seq =8 TTL =48 time=41.600 ms 1032 bytes from 150.109.147.28: Icmp_seq =9 TTL =48 time=29.513 ms 1032 bytes from 150.109.147.28: Icmp_seq =10 TTL =48 time=28.320 ms 1032 bytes from 150.109.147.28: Icmp_seq =11 TTL =48 time=29.449 ms 1032 bytes from 150.109.147.28: Icmp_seq =12 TTL =48 time=29.127 ms 1032 bytes from 150.109.147.28: Icmp_seq =13 TTL =48 time=28.583 ms 1032 bytes from 150.109.147.28: ICmp_seq =14 TTL =48 time=28.064 msCopy the code

-i 3 Sending interval: 3 seconds -s Sets the size of packets to be sent to 1024 -t Sets the TTL value to 255

Specify the source interface (NIC)

By default, ping sends ICMP packets over the default route. If you have multiple interfaces on your computer, you can specify the source interface using the following -i option:

> ping -i eth0 8.8.8.8 ping 8.8.8.8 (8.8.8.8) from 192.168.0.3 eth0:56 (84) bytes of data. 64 bytes from 8.8.8.8: Icmp_seq =2 TTL =109 time=201 ms 64 bytes from 8.8.8.8: ICmp_seq =3 TTL =109 time=201 ms 64 bytes from 8.8.8.8: Icmp_seq =4 TTL =109 time=200 ms 64 bytes from 8.8.8.8: ICmp_seq =5 TTL =109 time=201 ms 64 bytes from 8.8.8.8: Icmp_seq =6 TTL =109 time=201 ms 64 bytes from 8.8.8.8: ICmp_seq =7 TTL =109 time=201 ms 64 bytes from 8.8.8.8: Icmp_seq =9 TTL =109 time=201 ms 64 bytes from 8.8.8.8: ICmp_seq =10 TTL =109 time=201 ms...Copy the code

The following command will run ping 8.8.8.8 using eth0 as the source interface

Specifying Internet Protocol

When you run the ping command, it will use either IPv4 or IPv6, depending on your computer’s DNS Settings.

ping -4 DESTINATION ping -6 DESTINATION

Force ping to use IPv4 to pass the -4 option or use its alias ping4. For IPv6, pass the -6 option or use ping6.

> ping -4 8.8.8.8 ping 8.8.8.8 (8.8.8.8) 56(84) bytes of data. 64 bytes from 8.8.8.8: Icmp_seq =2 TTL =109 time=200 ms 64 bytes from 8.8.8.8: ICmp_seq =4 TTL =109 time=201 ms 64 bytes from 8.8.8.8: Icmp_seq =5 TTL =109 time=200 ms 64 bytes from 8.8.8.8: ICmp_seq =6 TTL =109 time=200 ms 64 bytes from 8.8.8.8: icmp_seq=6 TTL =109 time=200 ms 64 bytes from 8.8.8.8: Icmp_seq =8 TTL =109 time=201 ms 64 bytes from 8.8.8.8: ICmp_seq =13 TTL =109 time=200 ms 64 bytes from 8.8.8.8: Icmp_seq =16 TTL =109 time=201 ms 64 bytes from 8.8.8.8: ICmp_seq =17 TTL =109 time=200 ms 64 bytes from 8.8.8.8: Icmp_seq =19 TTL =109 time=200 ms 64 bytes from 8.8.8.8: ICmp_seq =21 TTL =109 time=201 ms...Copy the code

Original link :rumenz.com/rumenbiji/l… Wechat official account: entry station