Linux 系统的 ping 命令是常用的网络命令,它通常用来测试与目标主机的连通性。它通过发送 ICMP ECHO_REQUEST 数据包 到网络主机,并显示响应情况,这样我们就可以根据它输出的信息来确定目标主机是否可访问(但这不是绝对的)。有些服务器为了防止通过 ping 探测到,通过防火墙设置了禁止 ping 或者在内核参数中禁止 ping,这样就不能通过 ping 确定该主机是否还处于开启状态。
Linux 下的 ping 和 windows 下的 ping 稍有不同,Linux 下的 ping 不会自动终止,需要按 ctrl+c
终止或使用参数 -c
来指定要求完成的回应次数。
命令格式 - ping [参数] [主机名或IP地址]
命令功能 -
ctrl-c
) ;命令参数 -
命令实例 -
实例一:ping 的同的情况
命令
xxxxxxxxxx
11ping 192.168.120.205
输出
xxxxxxxxxx
121[root@localhost ~]# ping 192.168.120.205
2PING 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 ms
4bytes from 192.168.120.205: icmp_seq=2 ttl=64 time=0.181 ms
5bytes from 192.168.120.205: icmp_seq=3 ttl=64 time=0.191 ms
6bytes from 192.168.120.205: icmp_seq=4 ttl=64 time=0.188 ms
7bytes from 192.168.120.205: icmp_seq=5 ttl=64 time=0.189 ms
8
9--- 192.168.120.205 ping statistics ---
10packets transmitted, 5 received, 0% packet loss, time 4000ms
11rtt min/avg/max/mdev = 0.181/0.293/0.720/0.214 ms
12[root@localhost ~]#
实例二:ping 不同的情况
命令
xxxxxxxxxx
11ping 192.168.120.202
输出
xxxxxxxxxx
131[root@localhost ~]# ping 192.168.120.202
2PING 192.168.120.202 (192.168.120.202) 56(84) bytes of data.
3From 192.168.120.204 icmp_seq=1 Destination Host Unreachable
4From 192.168.120.204 icmp_seq=2 Destination Host Unreachable
5From 192.168.120.204 icmp_seq=3 Destination Host Unreachable
6From 192.168.120.204 icmp_seq=4 Destination Host Unreachable
7From 192.168.120.204 icmp_seq=5 Destination Host Unreachable
8From 192.168.120.204 icmp_seq=6 Destination Host Unreachable
9
10--- 192.168.120.202 ping statistics ---
11packets transmitted, 0 received, +6 errors, 100% packet loss, time 7005ms
12, pipe 4
13[root@localhost ~]#
实例三:ping 网关
命令
xxxxxxxxxx
11ping -b 192.168.120.1
输出
xxxxxxxxxx
181[root@localhost ~]# route
2Kernel IP routing table
3Destination Gateway Genmask Flags Metric Ref Use Iface
4192.168.120.0 * 255.255.255.0 U 0 0 0 eth0
5192.168.0.0 192.168.120.1 255.255.0.0 UG 0 0 0 eth0
610.0.0.0 192.168.120.1 255.0.0.0 UG 0 0 0 eth0
7default 192.168.120.240 0.0.0.0 UG 0 0 0 eth0
8[root@localhost ~]# ping -b 192.168.120.1
9PING 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 ms
11bytes from 192.168.120.1: icmp_seq=2 ttl=255 time=1.83 ms
12bytes from 192.168.120.1: icmp_seq=3 ttl=255 time=1.68 ms
13bytes from 192.168.120.1: icmp_seq=4 ttl=255 time=1.98 ms
14bytes from 192.168.120.1: icmp_seq=5 ttl=255 time=1.88 ms
15
16--- 192.168.120.1 ping statistics ---
17packets transmitted, 5 received, 0% packet loss, time 4000ms
18rtt min/avg/max/mdev = 1.682/1.880/2.020/0.129 ms
实例四:ping 指定次数
命令
xxxxxxxxxx
11ping -c 192.168.120.206
输出
xxxxxxxxxx
171[root@localhost ~]# ping -c 10 192.168.120.206
2PING 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 ms
4bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.260 ms
5bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.242 ms
6bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.271 ms
7bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.274 ms
8bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.295 ms
9bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.269 ms
10bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.270 ms
11bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.253 ms
12bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.289 ms
13
14--- 192.168.120.206 ping statistics ---
15packets transmitted, 10 received, 0% packet loss, time 9000ms
16rtt min/avg/max/mdev = 0.242/0.367/1.251/0.295 ms
17[root@localhost ~]#
实例五:时间间隔和次数限制的 ping
命令
xxxxxxxxxx
11ping -c 10 -i 0.5 192.168.120.206
输出
xxxxxxxxxx
331[root@localhost ~]# ping -c 10 -i 0.5 192.168.120.206
2PING 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 ms
4bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.235 ms
5bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.244 ms
6bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.300 ms
7bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.255 ms
8bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.264 ms
9bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.263 ms
10bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.331 ms
11bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.247 ms
12bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.244 ms
13
14--- 192.168.120.206 ping statistics ---
15packets transmitted, 10 received, 0% packet loss, time 4499ms
16rtt min/avg/max/mdev = 0.235/0.362/1.241/0.294 ms
17[root@localhost ~]# ping -c 10 -i 0.01 192.168.120.206
18PING 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 ms
20bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.195 ms
21bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.219 ms
22bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.204 ms
23bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=3.56 ms
24bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=1.93 ms
25bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.193 ms
26bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.193 ms
27bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.202 ms
28bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.211 ms
29
30--- 192.168.120.206 ping statistics ---
31packets transmitted, 10 received, 0% packet loss, time 90ms
32rtt min/avg/max/mdev = 0.193/0.716/3.564/1.080 ms
33[root@localhost ~]#
实例六:通过域名 ping 公网上的站点
命令
xxxxxxxxxx
11ping -c 5 www.58.com
输出
xxxxxxxxxx
121peida-VirtualBox ~ # ping -c 5 www.58.com
2PING 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 ms
4bytes from 211.151.111.30: icmp_req=2 ttl=49 time=16.4 ms
5bytes from 211.151.111.30: icmp_req=3 ttl=49 time=15.2 ms
6bytes from 211.151.111.30: icmp_req=4 ttl=49 time=14.6 ms
7bytes from 211.151.111.30: icmp_req=5 ttl=49 time=19.9 ms
8
9--- www.58.com ping statistics ---
10packets transmitted, 5 received, 0% packet loss, time 20101ms
11rtt min/avg/max/mdev = 14.618/16.192/19.917/1.965 ms
12peida-VirtualBox ~ #
实例七:多参数使用
命令
xxxxxxxxxx
11ping -i 3 -s 1024 -t 255 192.168.120.206
输出
xxxxxxxxxx
221[root@localhost ~]# ping -i 3 -s 1024 -t 255 192.168.120.206
2PING 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 ms
4bytes from 192.168.120.206: icmp_seq=2 ttl=64 time=0.694 ms
5bytes from 192.168.120.206: icmp_seq=3 ttl=64 time=0.300 ms
6bytes from 192.168.120.206: icmp_seq=4 ttl=64 time=0.481 ms
7bytes from 192.168.120.206: icmp_seq=5 ttl=64 time=0.415 ms
8bytes from 192.168.120.206: icmp_seq=6 ttl=64 time=0.600 ms
9bytes from 192.168.120.206: icmp_seq=7 ttl=64 time=0.411 ms
10bytes from 192.168.120.206: icmp_seq=8 ttl=64 time=0.281 ms
11bytes from 192.168.120.206: icmp_seq=9 ttl=64 time=0.318 ms
12bytes from 192.168.120.206: icmp_seq=10 ttl=64 time=0.362 ms
13bytes from 192.168.120.206: icmp_seq=11 ttl=64 time=0.408 ms
14bytes from 192.168.120.206: icmp_seq=12 ttl=64 time=0.445 ms
15bytes from 192.168.120.206: icmp_seq=13 ttl=64 time=0.397 ms
16bytes from 192.168.120.206: icmp_seq=14 ttl=64 time=0.406 ms
17bytes from 192.168.120.206: icmp_seq=15 ttl=64 time=0.458 ms
18
19--- 192.168.120.206 ping statistics ---
20packets transmitted, 15 received, 0% packet loss, time 41999ms
21rtt min/avg/max/mdev = 0.281/0.531/1.993/0.404 ms
22[root@localhost ~]#
说明
-i 3 发送周期为 3秒;
-s 1024 发送包的大小为 1024;
-t 255 TLL 值为255;