ping命令是用于排除故障,测试和诊断网络连接问题的最常用的命令之一。
Ping通过将一个或多个ICMP(Internet控制消息协议)Echo请求包发送到指定目标IP并等待回复来工作。当目标IP收到包时,它将响应ICMP回复。
使用ping命令,我们可以确定远程目标IP是处于活动状态还是非活动状态,找到与目标通信的往返延迟并检查是否存在数据包丢失。
ping命令是iputils(或iputils-ping)程序包的一部分,该程序包已预先安装在绝大多数Linux发行版上。ping命令也可用于Windows,MacOSX和FreeBSD。
ping [参数] [主机名或IP地址]
ping命令用于:确定网络和各外部主机的状态;跟踪和隔离硬件和软件问题;测试、评估和管理网络。如果主机正在运行并连在网上,它就对回送信号进行响应。每个回送信号请求包含一个网际协议(IP)和 ICMP 头,后面紧跟一个 tim 结构,以及来填写这个信息包的足够的字节。缺省情况是连续发送回送信号请求直到接收到中断信号(Ctrl-C)。
ping 命令每秒发送一个数据报并且为每个接收到的响应打印一行输出。ping 命令计算信号往返时间和(信息)包丢失情况的统计信息,并且在完成之后显示一个简要总结。ping 命令在程序超时或当接收到 SIGINT 信号时结束。Host 参数或者是一个有效的主机名或者是因特网地址。
-c 数目:在发送指定数目的包后停止。
> 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=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=6 ttl=106 time=253.948 ms ...
> 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 ...
> 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=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=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=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=8 ttl=106 time=255.902 ms 64 bytes from 8.8.8.8: icmp_seq=9 ttl=106 time=256.121 ms --- 8.8.8.8 ping statistics --- 10 packets transmitted, 10 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 254.084/255.914/260.055/1.513 ms
> 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=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=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=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 = 254.212/256.248/264.229/2.771 ms
> 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 --- json.im ping statistics --- 5 packets transmitted, 5 packets received, 0.0% packet loss round-trip min/avg/max/stddev = 28.012/29.435/31.498/1.341 ms
> 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.530 ms 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 ms
-i 3 发送周期为 3秒 -s 设置发送包的大小为1024 -t 设置TTL值为 255
默认情况下,ping将通过默认路由发送ICMP包。如果您的计算机上有多个接口,则可以使用以下-I选项指定源接口:
> 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 ...
下面的命令将执行ping 8.8.8.8使用eth0作为源接口
运行ping命令时,它将使用IPv4或IPv6,具体取决于您的计算机DNS设置。
ping -4 DESTINATION
ping -6 DESTINATION
强制ping使用IPv4传递-4选项或使用其别名ping4。对于IPv6传递-6选项或使用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=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 ...