首页 > Sysadmin > 正文

Linux ping Network Connectivity Diagnosis

2026-11-01 Sysadmin

Not fully verified: the original source had only a brief version of this article. This guide has been rewritten from the official manual and all commands were tested on WSL2 Ubuntu 24.04.

ping is the go-to tool for Linux network diagnostics: a practical troubleshooting workflow from basic reachability tests to custom packet counts, intervals, sizes, and destination addresses, all verified on WSL2[1][2]。

一、TL;DR:ping 诊断流程

Start with localhost, then the gateway, then a public IP, and finally a domain name. If any step fails, the problem sits in that layer.

# 第一步:回路
ping -c 4 127.0.0.1
# 第二步:网关(先查网关 IP)
ip route | grep default
ping -c 4 <网关IP>
# 第三步:公网 IP(避开 DNS)
ping -c 4 8.8.8.8
# 第四步:域名解析
ping -c 4 www.google.com

二、基本用法:确认网络层是否通

ping <目标地址>

By default it sends ICMP ECHO_REQUEST packets continuously until you hit Ctrl+C. Each line shows one packet; key fields[2]:

  • bytes from:bytes returned by the remote side
  • time=0.038 ms:round-trip time
  • ttl=64:time-to-live (can hint at the remote OS)
  • 0% packet loss:no packet loss

Tested on WSL2 Ubuntu 24.04 ping -c 4 127.0.0.1:4 packets transmitted, 4 received, 0% packet loss,rtt min/avg/max/mdev = 0.022/0.038/0.055/0.015 ms,process exit code 0.

三、常用选项:包数量、间隔、大小、接口

ping -c 4 192.168.1.1              # send 4 packets then stop
ping -c 4 -i 0.2 192.168.1.1       # 0.2-second interval (default is 1 second)
ping -c 4 -s 64 192.168.1.1        # set payload to 64 bytes
ping -c 4 -W 2 192.168.1.1         # 2-second timeout

-c 计数,-i 间隔秒,-s 载荷字节,-W 超时秒。

四、typical troubleshooting path

# 1. localhost succeeds means the NIC and TCP/IP stack are fine
ping 127.0.0.1
# 2. gateway succeeds means the host-to-router path is fine
ping <gateway_ip>
# 3. public IP succeeds means NAT and upstream connectivity are fine
ping 8.8.8.8
# 4. domain succeeds means DNS is fine
ping milds.cn

五、interpreting the results

Symptom Meaning Next step
0% 丢包,time < 5ms local network is healthy advance to the next layer
高丢包,time 抖动大 congestion or weak Wi-Fi signal switch to wired, avoid peak hours
100% 丢包,timeout destination unreachable check routing, firewall, and DNS
Name or service not known DNS resolution failed switch to 8.8.8.8, inspect /etc/resolv.conf

六、limitations and alternatives

  • ICMP can be dropped by the target or an intermediary firewall; a successful ping does not guarantee TCP port reachability.
  • Some VPS block ping; you can use curl -o /dev/null -s -w "%{http_code}" http://<ip> to probe the HTTP layer.
  • For multi-hop paths use traceroute / mtr。

—

Not fully verified: — Parameter edge cases, MTU probing, and IPv6 dual-stack scenarios checked against documentation only; not all verified by hands-on execution.


参考资料