ping 指令:Linux ping 選項與 100% packet loss 確認

測試環境RHEL 系列AlmaLinux 9.8iputils 20210202root 帳號Debian 12 · iputils 20221126 一般帳號
網路指令ping位置/usr/bin/ping2026-11-04 確認

在 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.com
PING 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.com
PING 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 -V
ping from iputils 20221126
libcap: yes, IDN: yes, NLS: no, error.h: yes, getrandom(): yes, __fpending(): yes
ping -V
ping from iputils 20210202

2️⃣ ping 次數與間隔選項

  • ↪️ -c, -i, -w, -W, -q

ping 選項中,-c 是傳送次數,-i 是傳送間隔(秒)。預設間隔為 1 秒,加上 -i 0.5 後,傳送四次所花的時間從 3003 ms 縮短為 1502 ms。

ping -c 4 -i 0.5 example.com
PING 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.com
PING 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.com
PING 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 ms

3️⃣ ping 封包大小與 IPv6 選項

  • ↪️ -s, -t, -4, -6

傳送的資料大小用 -s 變更。加上 -s 1000 後,第一行變成 1000(1028) bytes,回應行變成 1008 bytes。

ping -c 2 -s 1000 example.com
PING 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 ms

1028 是 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.com
PING 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.com
PING  (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.com
ping: 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 指令模擬器

在下面的模擬器中,可以自己輸入指令,試試次數與間隔、封包大小,以及傳送到沒有回應的位址的結果。

ping 指令模擬器iputils 20221126
[root@server work]#
只在瀏覽器中運作,不會傳送實際封包。用 ↑↓ 可以叫回之前輸入的指令。

4️⃣ ping 沒有回應

  • ↪️ 100% packet loss 與名稱解析失敗

[100% packet loss] 表示傳送的封包連一個回應都沒有。用 -W 1 對保留給文件使用的位址 192.0.2.1 傳送兩次,沒有回應行,只顯示統計。

ping -c 2 -W 1 192.0.2.1
PING 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
0

ping 只確認 ICMP,所以封鎖 ICMP 的伺服器可能會回應網頁請求,卻不回應 ping。從 Debian 12 伺服器傳送兩次到 amazon.com 時出現 [100% packet loss],但用 curl 對同一個位址發出網頁請求時,回傳了 301 回應。

ping -c 2 -W 1 amazon.com
PING amazon.com (98.82.161.185) 56(84) bytes of data.

--- amazon.com ping statistics ---
2 packets transmitted, 0 received, 100% packet loss, time 1002ms
curl -sI https://amazon.com/ | head -1
HTTP/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.invalid
ping: 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 exceededTTL 在中間路由器變成 0調高 -t 的值或拿掉後重新傳送

🔢 FAQ & 推薦內容

在 Debian 12 伺服器上,用一般帳號執行也不需要 sudo 就收到回應。這台伺服器的 /usr/bin/ping 帶有 cap_net_raw 權限(capability),所以不是 root 也能傳送 ICMP 封包。

在確認的兩台伺服器上,ping 是由 AlmaLinux 9 的 iputils 套件和 Debian 12 的 iputils-ping 套件安裝的指令。所以 RHEL 系列用 [sudo dnf install iputils],Debian 系列用 [sudo apt install iputils-ping] 安裝。

加上 -D 後,每個回應行前面會加上 Unix 時間(秒.微秒)。從 Debian 12 伺服器傳送兩次到 example.com,回應行前面記錄了像 [1793227536.574201] 這樣的時間。

ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。

發佈留言

發佈留言必須填寫的電子郵件地址不會公開。 必填欄位標示為 *

Prove your humanity: 1   +   5   =