Linux 系统的 ping 命令是常用的网络命令,它通常用来测试与目标主机的连通性。它通过发送 ICMP ECHO_REQUEST 数据包 到网络主机,并显示响应情况,这样我们就可以根据它输出的信息来确定目标主机是否可访问(但这不是绝对的)。有些服务器为了防止通过 ping 探测到,通过防火墙设置了禁止 ping 或者在内核参数中禁止 ping,这样就不能通过 ping 确定该主机是否还处于开启状态。
Linux 下的 ping 和 windows 下的 ping 稍有不同,Linux 下的 ping 不会自动终止,需要按 ctrl+c 终止或使用参数 -c 来指定要求完成的回应次数。
命令格式 - ping [参数] [主机名或IP地址]
命令功能 -
ctrl-c ) ;命令参数 -
命令实例 -
实例一:ping 的同的情况
命令
xxxxxxxxxx11ping 192.168.120.205输出
xxxxxxxxxx121[root@localhost ~]# ping 192.168.120.2052PING 192.168.120.205 (192.168.120.205) 56(84) bytes of data.3bytes from 192.168.120.205: icmp_seq=1 ttl=64 time=0.720 ms4bytes from 192.168.120.205: icmp_seq=2 ttl=64 time=0.181 ms5bytes from 192.168.120.205: icmp_seq=3 ttl=64 time=0.191 ms6bytes from 192.168.120.205: icmp_seq=4 ttl=64 time=0.188 ms7bytes from 192.168.120.205: icmp_seq=5 ttl=64 time=0.189 ms89--- 192.168.120.205 ping statistics ---10packets transmitted, 5 received, 0% packet loss, time 4000ms11rtt min/avg/max/mdev = 0.181/0.293/0.720/0.214 ms12[root@localhost ~]#实例二:ping 不同的情况
命令
xxxxxxxxxx11ping 192.168.120.202输出
xxxxxxxxxx131[root@localhost ~]# ping 192.168.120.2022PING 192.168.120.202 (192.168.120.202) 56(84) bytes of data.3From 192.168.120.204 icmp_seq=1 Destination Host Unreachable4From 192.168.120.204 icmp_seq=2 Destination Host Unreachable5From 192.168.120.204 icmp_seq=3 Destination Host Unreachable6From 192.168.120.204 icmp_seq=4 Destination Host Unreachable7From 192.168.120.204 icmp_seq=5 Destination Host Unreachable8From 192.168.120.204 icmp_seq=6 Destination Host Unreachable910--- 192.168.120.202 ping statistics ---11packets transmitted, 0 received, +6 errors, 100% packet loss, time 7005ms12, pipe 413[root@localhost ~]#实例三:ping 网关
命令
xxxxxxxxxx11ping -b 192.168.120.1输出
xxxxxxxxxx181[root@localhost ~]# route2Kernel IP routing table3Destination Gateway Genmask Flags Metric Ref Use Iface4192.168.120.0 * 255.255.255.0 U 0 0 0 eth05192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0610.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth07default 192.168.120.240 0.0.0.0 UG 0 0 0 eth08[root@localhost ~]# ping -b 192.168.120.19PING 192.168.120.1 (192.168.120.1) 56(84) bytes of data.10bytes from 192.168.120.1: icmp_seq=1 ttl=255 time=2.02 ms11bytes from 192.168.120.1: icmp_seq=2 ttl=255 time=1.83 ms12bytes from 192.168.120.1: icmp_seq=3 ttl=255 time=1.68 ms13bytes from 192.168.120.1: icmp_seq=4 ttl=255 time=1.98 ms14bytes from 192.168.120.1: icmp_seq=5 ttl=255 time=1.88 ms1516--- 192.168.120.1 ping statistics ---17packets transmitted, 5 received, 0% packet loss, time 4000ms18rtt min/avg/max/mdev = 1.682/1.880/2.020/0.129 ms实例四:ping 指定次数
命令
xxxxxxxxxx11ping -c 192.168.120.206输出
xxxxxxxxxx171[root@localhost ~]# ping -c 10 192.168.120.2062PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.3bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.25 ms4bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.260 ms5bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.242 ms6bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.271 ms7bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.274 ms8bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.295 ms9bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.269 ms10bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.270 ms11bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.253 ms12bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.289 ms1314--- 192.168.120.206 ping statistics ---15packets transmitted, 10 received, 0% packet loss, time 9000ms16rtt min/avg/max/mdev = 0.242/0.367/1.251/0.295 ms17[root@localhost ~]#实例五:时间间隔和次数限制的 ping
命令
xxxxxxxxxx11ping -c 10 -i 0.5 192.168.120.206输出
xxxxxxxxxx331[root@localhost ~]# ping -c 10 -i 0.5 192.168.120.2062PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.3bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.24 ms4bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.235 ms5bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.244 ms6bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.300 ms7bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.255 ms8bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.264 ms9bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.263 ms10bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.331 ms11bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.247 ms12bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.244 ms1314--- 192.168.120.206 ping statistics ---15packets transmitted, 10 received, 0% packet loss, time 4499ms16rtt min/avg/max/mdev = 0.235/0.362/1.241/0.294 ms17[root@localhost ~]# ping -c 10 -i 0.01 192.168.120.20618PING 192.168.120.206 (192.168.120.206) 56(84) bytes of data.19bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=0.244 ms20bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.195 ms21bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.219 ms22bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.204 ms23bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=3.56 ms24bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=1.93 ms25bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.193 ms26bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.193 ms27bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.202 ms28bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.211 ms2930--- 192.168.120.206 ping statistics ---31packets transmitted, 10 received, 0% packet loss, time 90ms32rtt min/avg/max/mdev = 0.193/0.716/3.564/1.080 ms33[root@localhost ~]#实例六:通过域名 ping 公网上的站点
命令
xxxxxxxxxx11ping -c 5 www.58.com输出
xxxxxxxxxx121peida-VirtualBox ~ # ping -c 5 www.58.com2PING www.58.com (211.151.111.30) 56(84) bytes of data.3bytes from 211.151.111.30: icmp_req=1 ttl=49 time=14.7 ms4bytes from 211.151.111.30: icmp_req=2 ttl=49 time=16.4 ms5bytes from 211.151.111.30: icmp_req=3 ttl=49 time=15.2 ms6bytes from 211.151.111.30: icmp_req=4 ttl=49 time=14.6 ms7bytes from 211.151.111.30: icmp_req=5 ttl=49 time=19.9 ms89--- www.58.com ping statistics ---10packets transmitted, 5 received, 0% packet loss, time 20101ms11rtt min/avg/max/mdev = 14.618/16.192/19.917/1.965 ms12peida-VirtualBox ~ #实例七:多参数使用
命令
xxxxxxxxxx11ping -i 3 -s 1024 -t 255 192.168.120.206输出
xxxxxxxxxx221[root@localhost ~]# ping -i 3 -s 1024 -t 255 192.168.120.2062PING 192.168.120.206 (192.168.120.206) 1024(1052) bytes of data.3bytes from 192.168.120.206: icmp_seq=1 ttl=64 time=1.99 ms4bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.694 ms5bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.300 ms6bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.481 ms7bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.415 ms8bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.600 ms9bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.411 ms10bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.281 ms11bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.318 ms12bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.362 ms13bytes from 192.168.120.206: icmp_seq=11 ttl=64 time=0.408 ms14bytes from 192.168.120.206: icmp_seq=12 ttl=64 time=0.445 ms15bytes from 192.168.120.206: icmp_seq=13 ttl=64 time=0.397 ms16bytes from 192.168.120.206: icmp_seq=14 ttl=64 time=0.406 ms17bytes from 192.168.120.206: icmp_seq=15 ttl=64 time=0.458 ms1819--- 192.168.120.206 ping statistics ---20packets transmitted, 15 received, 0% packet loss, time 41999ms21rtt min/avg/max/mdev = 0.281/0.531/1.993/0.404 ms22[root@localhost ~]#说明
-i 3 发送周期为 3秒;
-s 1024 发送包的大小为 1024;
-t 255 TLL 值为255;