首页·终端命令

ping

网络

用法

ping -c <count> <host>

发送 ICMP echo 请求,看对方是否应答、要多久;没有应答不等于机器挂了,很多防火墙只丢 ICMP 而 TCP 照常。

常用选项

选项含义
-c 4Stop after four packets. Without it, ping runs until Ctrl+C.
-i 0.2Interval between packets; anything under 0.2s needs root.
-s 1472Payload size. With -M do on Linux this is how you find the MTU.
-W 1How long to wait for a reply: seconds on Linux, milliseconds on macOS.
-t 5Total seconds before giving up, on macOS/BSD (on Linux that flag is -w).
-4 / -6Force IPv4 or IPv6.

示例

ping -c 4 1.1.1.1

Four packets to an address that does not need DNS to work.

ping -c 3 -s 1472 -M do 8.8.8.8

Check whether 1500-byte packets survive the path (Linux).

连不上的时候,这些能把范围缩小——是名字解析不了、没有路由,还是端口被挡。

怎么看

  • 方括号 [ ] 表示这部分可以不写。
  • 省略号 … 表示可以写多个。
  • 选项区分大小写——有些命令里 -r 和 -R 不是一回事。

常见问题

Q. ping 是做什么的?

发送 ICMP echo 请求,看对方是否应答、要多久;没有应答不等于机器挂了,很多防火墙只丢 ICMP 而 TCP 照常。

Q. 怎么写?

ping -c <count> <host> —— 方括号表示可以省略的部分。

Q. 值得记的选项有几个?

这里列了 6 个,完整列表在 man ping。这条命令属于网络。

相关命令

man 手册: man ping