使用 Linux 的 ping 命令诊断网络连接问题
ping

> 
> 在本文中，我们将讨论网络连接最基本的诊断工具之一：`ping` 命令。
> 
> 
> 


![](/data/attachment/album/202211/20/094045mhhkqhepke4qebks.jpg)


如今，联网计算机变得十分普遍，以至于我们大多数人都理所当然地认为，房间一侧的计算机可以连接上房间另一侧的计算机，更不用说能连接上世界的另一端的计算机了。如此，网络使互联网、云、文件共享、媒体流、远程管理、打印等服务成为可能。但是当网络出现问题时，有时很难诊断到底是其中哪一环节出现了问题。下面，我们就来介绍：网络连接最基本的诊断工具之一—— `ping` 命令。


### 基本的 ping 命令


当你无法访问本地网络上的计算机或互联网上的服务器时，你可以 `ping` 它的 IP 地址。`ping` 将 <ruby> 互联网控制报文协议 <rt>  Internet Control Message Protocol </rt></ruby>（ICMP）数据包发送到目标 IP 地址。当我们要对网路连接状况进行判断时，ICMP 是个非常有用的协议，本质上 ICMP 是一个响应和应答信号。


让我们由近及远地进行故障排除。请先 `ping` 你自己的计算机，以确保你的计算机正在运行 <ruby> 网络栈 <rt>  networking stack </rt></ruby>。你正在操作的计算机称为 <ruby> 主机 <rt>  localhost </rt></ruby>，本地回环地址是：`127.0.0.1`。


`ping` 命令能用主机的 <ruby> 主机名 <rt>  hostname </rt></ruby>、IP 地址（即 `127.0.0.1`）或者仅仅用简写 `0` 来表示 “主机”。


你可以使用 `-c` 选项，来控制发送数据包的 <ruby> 次数 <rt>  count </rt></ruby>。



```
$ ping 0 -c 1
PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.069 ms

--- 0 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.069/0.069/0.069/0.000 ms

```

在你确认本地网络栈已启动并运行后，接下来，你可以 `ping` 你的路由器的 IP 地址。路由器的 IP 地址通常以 `192.168` 或 `10` 开头。实际的 IP 地址取决于路由器的配置。


当你没有指定要发送多少次请求时，你可以用 `Ctrl+C`，来终止 `ping` 的运行。



```
$ ping 192.168.0.1 
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
From 192.168.0.100: icmp_seq=2 Redirect Host(New nexthop: 192.168.0.1)
From 192.168.0.100: icmp_seq=3 Redirect Host(New nexthop: 192.168.0.1)
From 192.168.0.100: icmp_seq=4 Redirect Host(New nexthop: 192.168.0.1)
From 192.168.0.100: icmp_seq=5 Redirect Host(New nexthop: 192.168.0.1)
^C

```

如果你能 `ping` 通路由器，则表示你的有线或无线连接能正常工作。


对于你的局域网上的其他主机呢？你可以 `ping` 各种设备，但是并非所有设备都能保证响应，因为一些设备会丢弃 ICMP 数据包，但许多设备会做出响应。例如，我可以 `ping` 我的打印机：



```
$ ping 192.168.0.4 

```

### ping 路由器以外的其他服务器


在确定你自己的网络内部都能连通以后，你还可以 `ping` 通到路由器以外的其他服务器。同样地，并非所有服务器都能接收 ICMP 数据包，更不用说响应 ICMP 数据包了。然而，也有一些服务器可以接收并响应 ICMP 数据包，而在互联网中的一个重要服务器是 <ruby> 域名服务器 <rt>  nameserver </rt></ruby>。


谷歌的 <ruby> 域名解析服务器 <rt>  DNS server </rt></ruby>的 IP 地址很容易记住，而且它会响应 `ping` 请求：



```
$ ping -c 2 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=1 ttl=116 time=53.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=53.5 ms

--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 53.304/53.424/53.544/0.120 ms

```

当你连不上一个网站时，你可以查询全球 DNS 网络，以找出其主机服务器的地址，然后 `ping` 该服务器。这至少可以告诉你，网站不通的原因是主机已关闭，或者只是 Web 服务器问题。


例如，假设你尝试访问 `example.com`，但是发现失败了。首先，使用 `host` 命令找到 `example.com` 的 IP 地址：



```
$ host example.com
example.com has address 93.184.216.34
example.com has IPv6 address 2606:2800:220:1:248:1893:25c8:1946
example.com mail is handled by 0

```

然后，`ping` 该网站的的 IP 地址：



```
$ ping 93.184.216.34 -c 1

```

### 使用 IPv6


`ping` 不仅可以使用 IPv4，还能使用 IPv6。可以通过指定 `-4` 或 `-6` 选项，来只使用 IPv4 或 IPv6。


### 设置数据包大小


你可以使用 `-s` 选项，来更改要发送的 ICMP 数据包的 <ruby> 大小 <rt>  size </rt></ruby>。默认的数据大小为 56 字节，加上 8 字节包头，总共得到 64 字节的 ICMP 数据包。以下的示例将发送的 ICMP 数据包大小修改为 35+8=43 个字节：



```
$ ping -s 35 -c 5 8.8.8.8

```

你可以使用 `-D` 选项，使得在终端中的每个 `ping` 回复之前，先打印出当前的时间戳。该时间戳为 UNIX 时间戳，加上微秒：



```
$ ping -D 8.8.8.8 
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
[1634013430.297468] 64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=53.3 ms
[1634013431.298738] 64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=53.1 ms

```

### 设置时间间隔/长短


你可以使用 `-i` 选项，来更改两次 `ping` 请求之间的 <ruby> 时间间隔 <rt>  interval </rt></ruby>。以下的示例将 `ping` 间隔更改为 2 秒：



```
$ ping -i 2 

```

你也可以使用 `-w` 选项，来在一段时间后终止 `ping`，单位为秒。



```
$ ping -w 6

```

### ping 的变体


`ping` 有很多变体。例如，`iputils` 包提供了 `ping` 命令；[Busybox](https://opensource.com/article/21/8/what-busybox) 也有`ping` 命令；BSD 也有；甚至还有一个图形界面的 `ping`：`gping`，它可用于 Linux、macOS 和 Windows。你可以在 [GitHub](https://github.com/orf/gping) 上找到更多有关 `gping` 的信息。


### 一起来学习吧


`ping` 命令很简单，但它可以帮你诊断网络连接问题。下次再遇到网络连接问题时，让 `ping` 命令成为你解决问题的第一个工具吧。




---


via: <https://opensource.com/article/21/10/linux-ping-command>


作者：[Seth Kenlon](https://opensource.com/users/seth) 选题：[lujun9972](https://github.com/lujun9972) 译者：[chai001125](https://github.com/chai001125) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Networked computers are so common these days that most of us take it for granted that a computer on one side of a room can contact one on the other side of the room, much less the other side of the world. When it works as designed, networking is what makes the Internet, the cloud, file shares, media streaming, remote administration, printing, and much more possible. When something goes wrong, it can sometimes be challenging to diagnose. One of the most fundamental diagnostic tools for networked connectivity is the `ping`

command.


## The basic ping

When you can't reach a computer on your local network, or a server on the Internet, you can ping it. A ping sends an Internet Control Message Protocol (ICMP) packet to a destination IP address. ICMP is, by design, a rudimentary format used mostly for diagnostics: It's essentially a call and response signal.

But there's an order to troubleshooting, and it starts as close to home as possible. When in doubt, first ping your own computer to ensure you're running a networking stack. The computer you're operating is also called your *localhost*, and it has a special IP address assigned for speaking to itself: 12.0.0.1.

The `ping`

** **command understands the *localhost* hostname, its IP address, and a shortcut of just `0`

.

You can control how many signals you send with the `-c`

(as in *count*)** **option.

```
$ ping 0 -c1
PING 0 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.069 ms
--- 0 ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.069/0.069/0.069/0.000 ms
```


After you've established that your local networking stack is up and running, you can ping your router. The address of a router usually starts with 192,168, or 10. The exact IP address depends on your router's configuration.

When you don't specify how many pings to send, you can stop `ping`

from running with **Ctrl**+**C**.

```
$ ping 192.168.0.1
PING 192.168.0.1 (192.168.0.1) 56(84) bytes of data.
From 192.168.0.100: icmp_seq=2 Redirect Host(New nexthop: 192.168.0.1)
From 192.168.0.100: icmp_seq=3 Redirect Host(New nexthop: 192.168.0.1)
From 192.168.0.100: icmp_seq=4 Redirect Host(New nexthop: 192.168.0.1)
From 192.168.0.100: icmp_seq=5 Redirect Host(New nexthop: 192.168.0.1)
^C
```


If you can reach your router, that means your wired or wireless connection is working.

What about other hosts on my network? You can ping all kinds of devices. Not all are guaranteed to respond (some devices drop ICMP packets), but many do. For instance, I can ping my printer:

`$ ping 192.168.0.4 `


## Pinging beyond your network

Beyond establishing that your own network is working as expected, you can also ping out into the wider world beyond your router. Again, not all servers are permitted to receive, much less respond to, ICMP. However, there are some that do, and a vital server to the working of the Internet is a nameserver.

Google's DNS server is relatively easy to remember, and it does respond to pings:

```
$ ping -c 2 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=1 ttl=116 time=53.3 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=53.5 ms
--- 8.8.8.8 ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1000ms
rtt min/avg/max/mdev = 53.304/53.424/53.544/0.120 ms
```


When a site has apparently disappeared, you might be able to probe the worldwide DNS network to find out what its host server's address is, and then ping that server. This at least tells you whether the host is down or whether it's just a web server issue.

For example, say you're trying unsuccessfully to reach example.com. First, find the IP address using the `host`

command:

```
$ host example.com
example.com has address 93.184.216.34
example.com has IPv6 address 2606:2800:220:1:248:1893:25c8:1946
example.com mail is handled by 0
```


And then ping the website's host by IP:

`$ ping 93.184.216.34 -c 1`


## Ping and IPv6

Ping works over IPv4 as well as IPv6. Using only one of them explicitly can be enforced by specifying `-4`

or `-6`

.

## Packet size

You can change the size of the ICMP packets you're sending with the `-s`

option. The default packet size is 56, which translates into 64 ICMP data bytes when combined with the 8-byte header. This command sends 43 bytes:

`$ ping -s 35 -c 5 8.8.8.8`


You can print a timestamp before each ping report in your terminal with the `-D`

option. This provides the UNIX epoch time, plus microseconds:

```
$ ping -D 8.8.8.8
PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
[1634013430.297468] 64 bytes from 8.8.8.8: icmp_seq=1 ttl=116 time=53.3 ms
[1634013431.298738] 64 bytes from 8.8.8.8: icmp_seq=2 ttl=116 time=53.1 ms
```


## Ping time

You can change the time interval between pings using the `-i`

option. This changes the ping interval to two seconds:

`$ ping -i 2 `


You can also stop pinging after some value of time (in seconds) with the `-w`

option:

`$ ping -w 6`


## Variants

There are many implementations of ping. The `iputils`

package provides a `ping`

command, [Busybox ](https://opensource.com/article/21/8/what-busybox)has a `ping`

command, and there's one from BSD and others. There's even a GUI for `ping`

: Gping is available for Linux, macOS, and Windows. You can find more information for `gping`

on [Github](https://github.com/orf/gping).

## Learn to ping

The `ping`

command is simple, but it can be eyes and ears out on the vast expanse that is your network. Next time you have connectivity issues, let `ping`

be the first tool you turn to.

## 1 Comment