Linux常用命令-ping

命令

ping

描述

send ICMP ECHO_REQUEST to network hosts
ICMP工具

用法

1
2
3
4
ping  [-aAbBdDfhLnOqrRUvV46] [-c count] [-F flowlabel] [-i interval] [-I interface]
[-l preload] [-m mark] [-M pmtudisc_option] [-N nodeinfo_option] [-w deadline]
[-W timeout] [-p pattern] [-Q tos] [-s packetsize] [-S sndbuf] [-t ttl]
[-T timestamp_option] [hop ...] destination

选项

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
Options:
-4 使用IPv4
-6 使用IPv6
-b 允许ping广播地址
-c 指定发送包的数量
-D 显示时间戳
-f 泛洪ping
-i 指定发送包的间隔时间,默认为1s
-I 指定出接口或源IP
-l 一次性发送多少个数据包
-L 抑制多播数据包的环回
-m 使用mark标记即将发出的数据包
-N 发送ICMPv6节点信息查询,详情请查看man ping
-n 显示数字而非主机名
-O 在发送下一个数据包之前报告未完成的ICMP ECHO回复
-q 只显示汇总信息,通常配合-c选项使用
-s 指定要发送的数据字节数。默认为56,与8字节的ICMP头数据组合时,转换为64个ICMP数据字节
-w 指定超时时间

注意

linux下的ping命令默认是一直ping,使用ctrl+c手动结束

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
$ ping  192.168.31.1
PING 192.168.31.1 (192.168.31.1) 56(84) bytes of data.
64 bytes from 192.168.31.1: icmp_seq=1 ttl=64 time=1.21 ms
$ ping www.baidu.com
PING www.baidu.com (110.242.68.3) 56(84) bytes of data.
64 bytes from 110.242.68.3 (110.242.68.3): icmp_seq=1 ttl=53 time=11.2 ms
$ ping -6 ::1
PING ::1(::1) 56 data bytes
64 bytes from ::1: icmp_seq=1 ttl=64 time=0.086 ms
# ipv6需要指定出接口
$ ping6 -I eth0 fe80::2daa:d1ea:5915:ce9f
PING fe80::2daa:d1ea:5915:ce9f(fe80::2daa:d1ea:5915:ce9f) from fe80::2daa:d1ea:5915:ce9f%eth0 eth0: 56 data bytes
64 bytes from fe80::2daa:d1ea:5915:ce9f%eth0: icmp_seq=1 ttl=64 time=0.119 ms
# -c 指定发送包的数量
$ ping -c 3 192.168.31.1
PING 192.168.31.1 (192.168.31.1) 56(84) bytes of data.
64 bytes from 192.168.31.1: icmp_seq=1 ttl=64 time=1.14 ms
64 bytes from 192.168.31.1: icmp_seq=2 ttl=64 time=1.26 ms
64 bytes from 192.168.31.1: icmp_seq=3 ttl=64 time=1.28 ms
--- 192.168.31.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2005ms
rtt min/avg/max/mdev = 1.146/1.231/1.280/0.073 ms
# -D 显示时间戳,可以通过命令date --date "@1641369881.417123"转换时间格式
$ ping -D 192.168.31.1
PING 192.168.31.1 (192.168.31.1) 56(84) bytes of data.
[1641369881.417123] 64 bytes from 192.168.31.1: icmp_seq=1 ttl=64 time=1.14 ms
--- 192.168.31.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2006ms
rtt min/avg/max/mdev = 1.145/1.250/1.312/0.080 ms
# -I 指定源地址
$ ping -I 192.168.31.11 192.168.31.1
PING 192.168.31.1 (192.168.31.1) from 192.168.31.11 : 56(84) bytes of data.
64 bytes from 192.168.31.1: icmp_seq=1 ttl=64 time=1.35 ms
64 bytes from 192.168.31.1: icmp_seq=2 ttl=64 time=1.33 ms
--- 192.168.31.1 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1002ms
rtt min/avg/max/mdev = 1.337/1.345/1.353/0.008 ms
# -w 指定超时时间
$ ping -w 3 192.168.31.1
PING 192.168.31.1 (192.168.31.1) 56(84) bytes of data.
64 bytes from 192.168.31.1: icmp_seq=1 ttl=64 time=1.09 ms
64 bytes from 192.168.31.1: icmp_seq=2 ttl=64 time=1.28 ms
64 bytes from 192.168.31.1: icmp_seq=3 ttl=64 time=1.26 ms
--- 192.168.31.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 1.093/1.214/1.286/0.095 ms
# -qc 只显示汇总结果
$ ping -qc 3 192.168.31.1
PING 192.168.31.1 (192.168.31.1) 56(84) bytes of data.
--- 192.168.31.1 ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2004ms
rtt min/avg/max/mdev = 0.926/3.360/8.215/3.433 ms
$ ping -qc 3 192.168.31.10
PING 192.168.31.10 (192.168.31.10) 56(84) bytes of data.
--- 192.168.31.10 ping statistics ---
3 packets transmitted, 0 received, +3 errors, 100% packet loss, time 2002ms
pipe 3
# -O 发送包前返回ICMP ECHO回复
$ ping -O 192.168.31.10
PING 192.168.31.10 (192.168.31.10) 56(84) bytes of data.
no answer yet for icmp_seq=1
no answer yet for icmp_seq=2
no answer yet for icmp_seq=3
From 192.168.31.11 icmp_seq=1 Destination Host Unreachable
From 192.168.31.11 icmp_seq=2 Destination Host Unreachable
From 192.168.31.11 icmp_seq=3 Destination Host Unreachable
# -s 指定要发送的数据字节数
$ ping -s 100 192.168.31.1
PING 192.168.31.1 (192.168.31.1) 100(128) bytes of data.
108 bytes from 192.168.31.1: icmp_seq=3 ttl=64 time=1.36 ms