ping 指令:Linux ping 選項與 100% packet loss 確認
在 Linux 伺服器上,封包能否到達對方伺服器以及往返時間,可以用 ping 指令確認。從 AlmaLinux 9 和 Debian 12 伺服器用 -c 指定次數傳送到 example.com,往返時間為 1~3 ms;而封鎖 ICMP 的伺服器明明會回應網頁請求,ping 結果卻是 100% packet loss。
下面的輸出是在 AlmaLinux 9.8 伺服器(root 帳號)和 Debian 12 伺服器(一般帳號)上執行的結果。所有 ping 都用 -c 指定傳送次數執行。中間路由器的私有 IP 位址已換成範例值,從 example.com 這類公開網域解析出的位址則保留原樣。
1️⃣ ping 指令用法
ping 會向對方伺服器傳送 ICMP 回應要求,並逐行輸出收到回應所花的時間。Linux 的 ping 如果沒有用 -c 指定次數,會一直傳送到按下 Ctrl+C 為止(依官方手冊),所以一開始就加上次數執行,例如 -c 4。
ping -c 4 example.comPING example.com (172.66.147.243) 56(84) bytes of data.
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=1 ttl=58 time=1.46 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=2 ttl=58 time=1.30 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=3 ttl=58 time=1.28 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=4 ttl=58 time=1.30 ms
--- example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 1.279/1.336/1.464/0.074 ms從 Debian 12 伺服器傳送四次到 example.com,四次都收到回應,往返時間為 1.28~1.46 ms。每一行要看的內容如下。
| 輸出 | 查看的資訊 |
|---|---|
| PING example.com (172.66.147.243) 56(84) bytes of data. | 解析名稱得到的位址與傳送大小 — 56 位元組資料加上 20 位元組 IP 標頭和 8 位元組 ICMP 標頭,共 84 位元組 |
| 64 bytes from … | 收到的回應大小(56 位元組資料 + 8 位元組 ICMP 標頭) |
| icmp_seq=1 | 傳送的序號 |
| ttl=58 | 回應封包中剩下的 TTL — 每經過一台路由器就減 1 |
| time=1.46 ms | 該封包的往返時間 |
| 4 packets transmitted, 4 received, 0% packet loss | 傳送數、接收數,以及沒有收到回應的比例 |
| time 3003ms | 從傳送第一個封包到結束所花的時間 |
| rtt min/avg/max/mdev | 往返時間的最小值、平均值、最大值與離散程度(mdev) |
mdev 表示往返時間偏離平均值多少,官方手冊說明這個值越大,往返時間就越不平均。四次都在 1.5 ms 以內的這次結果,mdev 為 0.074 ms。
在 AlmaLinux 9 伺服器上執行同一個指令時,ping 傳送到 example.com 的 IPv6 位址,第一行沒有 84,只顯示 56 data bytes。
ping -c 4 example.comPING example.com(2606:4700:10::ac42:93f3 (2606:4700:10::ac42:93f3)) 56 data bytes
64 bytes from 2606:4700:10::ac42:93f3 (2606:4700:10::ac42:93f3): icmp_seq=1 ttl=51 time=2.20 ms
64 bytes from 2606:4700:10::ac42:93f3 (2606:4700:10::ac42:93f3): icmp_seq=2 ttl=54 time=2.30 ms
64 bytes from 2606:4700:10::ac42:93f3 (2606:4700:10::ac42:93f3): icmp_seq=3 ttl=51 time=2.55 ms
64 bytes from 2606:4700:10::ac42:93f3 (2606:4700:10::ac42:93f3): icmp_seq=4 ttl=54 time=2.26 ms
--- example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3004ms
rtt min/avg/max/mdev = 2.203/2.326/2.547/0.131 ms已安裝的 ping 版本用 -V 確認。Debian 12 伺服器是 iputils 20221126,AlmaLinux 9 伺服器是 iputils 20210202。
ping -Vping from iputils 20221126
libcap: yes, IDN: yes, NLS: no, error.h: yes, getrandom(): yes, __fpending(): yesping -Vping from iputils 202102022️⃣ ping 次數與間隔選項
ping 選項中,-c 是傳送次數,-i 是傳送間隔(秒)。預設間隔為 1 秒,加上 -i 0.5 後,傳送四次所花的時間從 3003 ms 縮短為 1502 ms。
ping -c 4 -i 0.5 example.comPING example.com (172.66.147.243) 56(84) bytes of data.
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=1 ttl=58 time=1.26 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=2 ttl=58 time=1.31 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=3 ttl=58 time=1.37 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=4 ttl=58 time=1.44 ms
--- example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 1502ms
rtt min/avg/max/mdev = 1.264/1.343/1.436/0.065 ms依 AlmaLinux 9 伺服器的 iputils 20210202 手冊,把間隔縮到 0.2 秒以下只有 root 能做;Debian 12 伺服器的 iputils 20221126 手冊則把這個標準寫成 2 ms。間隔太短可能會對對方伺服器和網路造成負擔。
-w 是不論已傳送幾次、讓整個 ping 結束的時間(秒)。與 -c 一起使用時,收到指定次數的回應,或超過 -w 時間時就會結束。以 -c 10 -w 3 執行時,過了 3 秒只傳送三次就結束了。
ping -c 10 -w 3 example.comPING example.com (172.66.147.243) 56(84) bytes of data.
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=1 ttl=58 time=1.34 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=2 ttl=58 time=1.27 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=3 ttl=58 time=1.34 ms
--- example.com ping statistics ---
3 packets transmitted, 3 received, 0% packet loss, time 2002ms
rtt min/avg/max/mdev = 1.272/1.318/1.342/0.032 ms三次都收到回應,但沒有達到指定的 10 次,所以這時 ping 的結束代碼是 1。官方手冊也說明,同時使用 -c 和 -w 時,若沒有收到指定次數的回應,就會以 1 結束。
-W 是等待一個回應的時間(秒)。依官方手冊,這個值只在完全沒有回應時才會套用;對會回應的目標,則等待往返時間的兩倍。
只想看摘要、不要結果行時,加上 -q。傳送四次後,只剩第一行和最後的統計行。
ping -c 4 -q example.comPING example.com (104.20.23.154) 56(84) bytes of data.
--- example.com ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 1.551/1.649/1.715/0.060 ms3️⃣ ping 封包大小與 IPv6 選項
傳送的資料大小用 -s 變更。加上 -s 1000 後,第一行變成 1000(1028) bytes,回應行變成 1008 bytes。
ping -c 2 -s 1000 example.comPING example.com (172.66.147.243) 1000(1028) bytes of data.
1008 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=1 ttl=58 time=1.21 ms
1008 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=2 ttl=58 time=1.31 ms
--- example.com ping statistics ---
2 packets transmitted, 2 received, 0% packet loss, time 1001ms
rtt min/avg/max/mdev = 1.214/1.262/1.311/0.048 ms1028 是 1000 位元組資料加上 20 位元組 IP 標頭和 8 位元組 ICMP 標頭的大小,1008 則是資料只加上 ICMP 標頭的大小。往返時間為 1.21~1.31 ms,與預設大小(56 位元組)傳送時差不多。
-t 設定傳送封包的 TTL。TTL 每經過一台路由器就減 1,變成 0 時,該路由器會丟棄封包並回傳 [Time to live exceeded]。
ping -c 2 -t 3 example.comPING example.com (172.66.147.243) 56(84) bytes of data.
From 192.0.2.103 (192.0.2.103) icmp_seq=1 Time to live exceeded
From 192.0.2.103 (192.0.2.103) icmp_seq=2 Time to live exceeded
--- example.com ping statistics ---
2 packets transmitted, 0 received, +2 errors, 100% packet loss, time 1001ms把 TTL 降到 3 時,第三台路由器兩次都回傳 [Time to live exceeded]。從 1 開始逐一調高 TTL,example.com 在 8 時第一次回應。
-4 和 -6 把傳送的位址類型限定為 IPv4 或 IPv6 其中一種。AlmaLinux 9 伺服器即使不加選項也會傳送到 example.com 的 IPv6 位址,所以要傳送到 IPv4 位址時加上了 -4。
ping -4 -c 4 example.comPING (172.66.147.243) 56(84) bytes of data.
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=1 ttl=54 time=2.64 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=2 ttl=54 time=2.55 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=3 ttl=54 time=2.57 ms
64 bytes from 172.66.147.243 (172.66.147.243): icmp_seq=4 ttl=54 time=2.58 ms
--- ping statistics ---
4 packets transmitted, 4 received, 0% packet loss, time 3003ms
rtt min/avg/max/mdev = 2.553/2.585/2.642/0.033 ms在 AlmaLinux 9 伺服器的 iputils 20210202 上加上 -4 後,第一行和統計行的名稱欄位變成空白。在 Debian 12 伺服器的 iputils 20221126 上執行同一個指令時,example.com 照常顯示。
在沒有 IPv6 預設路由的 Debian 12 伺服器上加上 -6 後,沒有回應行,直接以 [connect: Network is unreachable] 結束。
ping -6 -c 2 example.comping: connect: Network is unreachable🔲 ping 指令選項
📌 主要選項:本文實際執行的結果
| 選項 | 作用 | 本文看到的結果 | |
|---|---|---|---|
| -c 次數 | 指定傳送次數,傳送完就結束 | 在 Debian 12 伺服器上用 -c 4 傳送四次,四次都有回應,往返 1.28~1.46 ms | |
| -i 間隔 | 傳送封包的間隔(秒),預設 1 秒 | 用 -i 0.5 傳送四次所花的時間從 3003 ms 縮短為 1502 ms | |
| -w 時間 | 不論已傳送幾次,讓整個 ping 結束的時間(秒) | -c 10 -w 3 在 3 秒後只傳送三次,並以結束代碼 1 結束 | |
| -W 時間 | 完全沒有回應時,等待一個回應的時間(秒) | 用 -W 1 傳送兩次到 192.0.2.1,[100% packet loss],結束代碼 1 | |
| -q | 不輸出回應行,只輸出第一行和最後的統計 | 傳送四次後只剩第一行和統計行 | |
| -s 大小 | 傳送的資料大小(位元組),預設 56 | 用 -s 1000 時,第一行為 1000(1028) bytes,回應行為 1008 bytes | |
| -t TTL | 傳送封包的 TTL | 用 -t 3 時,第三台路由器兩次都回傳 [Time to live exceeded] | |
| -4 | 只傳送到 IPv4 位址 | 在 AlmaLinux 9 伺服器上改傳送到 IPv4 位址而非 IPv6,名稱欄位顯示為空白 | |
| -6 | 只傳送到 IPv6 位址 | 在沒有 IPv6 預設路由的 Debian 12 伺服器上 [connect: Network is unreachable] | |
| -D | 在每個回應行前面加上 Unix 時間(秒.微秒) | 回應行前面加上像 [1793227536.574201] 這樣的時間 | |
| -V | 輸出版本資訊 | Debian 12 是 iputils 20221126,AlmaLinux 9 是 iputils 20210202 |
📎 其他選項:依官方手冊
| 選項 | 作用 | |
|---|---|---|
| -n | 不把位址解析成名稱,只以數字輸出 | |
| -I 介面 | 指定要傳送的介面或來源位址 | |
| -A | 依往返時間自動調整傳送間隔,非 root 時最小間隔為 200 ms | |
| -f | 不等待回應、快速傳送的洪水 ping,間隔 0 只限 root | |
| -l 數量 | 不等待回應就先傳送的封包數,超過 3 個只限 root | |
| -M do | 指定 Path MTU Discovery 方式,do 會禁止分段(也接受 want、dont) | |
| -p 模式 | 填入傳送封包的位元組模式,最多 16 位元組 | |
| -O | 在傳送下一個封包前通知沒有收到回應的要求,與 -D 一起記錄時使用 | |
| -R | 記錄經過的路徑(僅限 IPv4,很多主機會忽略這個選項) | |
| -b | 允許傳送到廣播位址 | |
| -a | 每次收到回應時發出聲音 |
🔲 ping 指令模擬器
在下面的模擬器中,可以自己輸入指令,試試次數與間隔、封包大小,以及傳送到沒有回應的位址的結果。
4️⃣ ping 沒有回應
[100% packet loss] 表示傳送的封包連一個回應都沒有。用 -W 1 對保留給文件使用的位址 192.0.2.1 傳送兩次,沒有回應行,只顯示統計。
ping -c 2 -W 1 192.0.2.1PING 192.0.2.1 (192.0.2.1) 56(84) bytes of data.
--- 192.0.2.1 ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1024ms完全沒有回應時,ping 的結束代碼是 1,收到回應時則是 0。在腳本中確認對方伺服器是否回應時,就使用這個值。
ping -c 2 -W 1 192.0.2.1 > /dev/null; echo $?; ping -c 1 example.com > /dev/null; echo $?1
0ping 只確認 ICMP,所以封鎖 ICMP 的伺服器可能會回應網頁請求,卻不回應 ping。從 Debian 12 伺服器傳送兩次到 amazon.com 時出現 [100% packet loss],但用 curl 對同一個位址發出網頁請求時,回傳了 301 回應。
ping -c 2 -W 1 amazon.comPING amazon.com (98.82.161.185) 56(84) bytes of data.
--- amazon.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1002mscurl -sI https://amazon.com/ | head -1HTTP/1.1 301 Moved Permanently封鎖 ICMP 的伺服器,就像拆掉門鈴的店家。
雲端伺服器有時要在防火牆規則中允許 ICMP,才會回應 ping。 Oracle Cloud 的 VM 執行個體在公用子網路的安全清單中把 ICMP 類型改成 8 之後,才開始回應 ping。
無法把名稱解析成 IP 位址時,不會出現第一行,直接以 [Name or service not known] 結束,結束代碼為 2。.invalid 是保留成在任何地方都無法解析的名稱,所以用來確認這個錯誤。
ping -c 1 no-such-host.invalidping: no-such-host.invalid: Name or service not known如果實際網域出現這個錯誤,請用 nslookup 指令確認 DNS 伺服器是否會回傳該名稱的位址。沒有回應時出現的四種輸出,以及接下來要確認的項目如下。
| 輸出 | 說明 | 接下來要確認的項目 |
|---|---|---|
| 100% packet loss | 傳送的封包連一個回應都沒有 | 網頁服務用 curl,防火牆看 ICMP 允許規則 |
| Name or service not known | 無法把名稱解析成 IP 位址 | 用 nslookup 看 DNS 回應 |
| connect: Network is unreachable | 沒有該位址類型的對外路由 | 用 -4 傳送到 IPv4 位址 |
| Time to live exceeded | TTL 在中間路由器變成 0 | 調高 -t 的值或拿掉後重新傳送 |
🔢 FAQ & 推薦內容
ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。