1. 首页 > Linux教程 > 正文

Linux教程FG207-网络故障排查工具ping与traceroute

内容简介:本文风哥教程参考Linux官方文档、Red Hat Enterprise Linux官方文档、Ansible Automation Platform官方文档、Docker官方文档、Kubernetes官方文档和Podman官方文档等内容,详细介绍了相关技术的配置和使用方法。

本文档详细介绍ping和traceroute工

风哥提示:

具的使用方法,帮助诊断网络连通性和路由问题。

Part01-ping命令详解

1.1 ping基本使用

# ping命令基本语法
# ping [选项] 目标主机

# 基本ping测试
$ ping 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.521 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.489 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.512 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.498 ms
^C
— 192.168.1.1 ping statistics —
4 packets transmitted, 4 received, 0% packet loss, time 3005ms
rtt min/avg/max/mdev = 0.489/0.505/0.521/0.013 ms

# 指定ping次数
$ ping -c 3 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=117 time=10.5 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=10.2 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=117 time=10.3 ms

— 8.8.8.8 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 10.196/10.342/10.531/0.138 ms

# 指定数据包大小
$ ping -s 1000 -c 3 192.168.1.1
PING 192.学习交流加群风哥QQ113from PG视频:www.itpux.com257174168.1.1 (192.168.1.1) 1000(1028) bytes of data.
1008 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.621 ms
1008 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.589 ms
1008 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.612 ms

— 192.168.1.1 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.589/0.607/0.621/0.013 ms

# 指定时间间隔(秒)
$ ping -i 0.5 -c 5 192.168.1.1
PING 192.更多学习教程公众号风哥教程itpux_com168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.521 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.489 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.512 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.498 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=0.505 ms

— 192.168.1.1 ping statistics —
5 packets transmitted, 5 received, 0% packet loss, time 2001ms
rtt min/avg/max/mdev = 0.489/0.505/0.521/0.013 ms

Part02-ping高级选项

2.1 ping高级参数

# 查看ping帮助
$ ping -h
Usage: ping [OPTION…] HOST …
Send ICMP ECHO_REQUEST to network hosts.

Options valid for all requests:
-c, –count=NUMBER stop after sending NUMBER packets
-i, –interval=NUMBER wait NUMBER seconds between sending each packet
-l, –preload=NUMBER send NUMBER packets as fast as possible
-n, –numeric do not resolve host addresses
-q, –quiet quiet output
-s, –size=NUMBER send NUMBER data octets
-t, –ttl=N specify N as time-to-live
-v, –verbose verbose output
-w, –timeout=N stop after N seconds
-W, –linger=N number of seconds to wait for response

# 设置TTL值
$ ping -t 64 -c 3 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=117 time=10.5 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=10.2 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=117 time=10.3 ms

— 8.8.8.8 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 10.196/10.342/10.531/0.138 ms

# 设置超时时间
$ ping -w 5 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.521 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.489 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.512 ms
64 bytes from 192.168.1.1: icmp_seq=4 ttl=64 time=0.498 ms
64 bytes from 192.168.1.1: icmp_seq=5 ttl=64 time=0.505 ms

— 192.168.1.1 ping statistics —
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 0.489/0.505/0.521/0.013 ms

# 静默模式
$ ping -q -c 5 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.

— 192.168.1.1 ping statistics —
5 packets transmitted, 5 received, 0% packet loss, time 4006ms
rtt min/avg/max/mdev = 0.489/0.505/0.521/0.013 ms

# 指定源IP地址
$ ping -I 192.168.1.100 -c 3 192.168.1.1
PING 192.168.1.1 (192.168.1.1) from 192.168.1.100 : 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.521 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.489 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.512 ms

— 192.168.1.1 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 0.489/0.505/0.521/0.013 ms

Part03-traceroute命令详解

3.1 traceroute基本使用

# 安装traceroute
$ sudo dnf install -y traceroute
Last metadata expiration check: 0:45:23 ago on Thu 03 Apr 2026 18:00:15 AM CST.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
traceroute x86_64 3:2.1.3-1.el10 baseos 45 k
Transaction Summary
================================================================================
Install 1 Packages

Total download size: 45 k
Installed size: 89 k
Complete!

# 基本traceroute测试
$ traceroute 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 gateway (192.168.1.1) 0.521 ms 0.489 ms 0.512 ms
2 10.0.0.1 (10.0.0.1) 1.234 ms 1.123 ms 1.345 ms
3 172.16.0.1 (172.16.0.1) 5.678 ms 5.789 ms 5.890 ms
4 * * *
5 dns.google (8.8.8.8) 10.521 ms 10.489 ms 10.512 ms

# 使用ICMP协议
$ traceroute -I 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 gateway (192.168.1.1) 0.521 ms 0.489 ms 0.512 ms
2 10.0.0.1 (10.0.0.1) 1.234 ms 1.123 ms 1.345 ms
3 172.16.0.1 (172.16.0.1) 5.678 ms 5.789 ms 5.890 ms
4 * * *
5 dns.google (8.8.8.8) 10.521 ms 10.489 ms 10.512 ms

# 使用TCP协议
$ traceroute -T -p 80 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 gateway (192.168.1.1) 0.521 ms 0.489 ms 0.512 ms
2 10.0.0.1 (10.0.0.1) 1.234 ms 1.123 ms 1.345 ms
3 172.16.0.1 (172.16.0.1) 5.678 ms 5.789 ms 5.890 ms
4 * * *
5 dns.google (8.8.8.8) 10.521 ms 10.489 ms 10.512 ms

# 指定最大跳数
$ traceroute -m 15 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 15 hops max, 60 byte packets
1 gateway (192.168.1.1) 0.521 ms 0.489 ms 0.512 ms
2 10.0.0.1 (10.0.0.1) 1.234 ms 1.123 ms 1.345 ms
3 172.16.0.1 (172.16.0.1) 5.678 ms 5.789 ms 5.890 ms
4 * * *
5 dns.google (8.8.8.8) 10.521 ms 10.489 ms 10.512 ms

Part04-traceroute高级选项

4.1 traceroute高级参数

# 查看traceroute帮助
$ traceroute –help 2>&1 | head -30
Usage: traceroute [OPTION…] HOST
Print the route packets trace to network host.

-4, –ipv4 use IPv4
-6, –ipv6 use IPv6
-I, –icmp use ICMP ECHO for tracerouting
-T, –tcp use TCP SYN for tracerouting
-U, –udp use UDP to particular port for tracerouting
-d, –debug enable socket level debugging
-F, –dont-fragment do not fragment packets
-f, –first=FIRST_TTL start from the FIRST_TTL hop
-g, –gateways=GATES route packets through the specified gateway
-i, –interface=IFACE use interface as the source address
-m, –max-hops=MAX_HOPS set the max number of hops
-n, –numeric print hop addresses numerically
-p, –port=PORT use destination PORT port
-q, –tries=QUERIES set the number of probes per hop
-r, –resolve-hostnames resolve hostnames

# 不解析主机名
$ traceroute -n 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 192.168.1.1 0.521 ms 0.489 ms 0.512 ms
2 10.0.0.1 1.234 ms 1.123 ms 1.345 ms
3 172.16.0.1 5.678 ms 5.789 ms 5.890 ms
4 * * *
5 8.8.8.8 10.521 ms 10.489 ms 10.512 ms

# 指定源接口
$ traceroute -i eth0 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 gateway (192.168.1.1) 0.521 ms 0.489 ms 0.512 ms
2 10.0.0.1 (10.0.0.1) 1.234 ms 1.123 ms 1.345 ms
3 172.16.0.1 (172.16.0.1) 5.678 ms 5.789 ms 5.890 ms
4 * * *
5 dns.google (8.8.8.8) 10.521 ms 10.489 ms 10.512 ms

# 设置探测次数
$ traceroute -q 1 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 gateway (192.168.1.1) 0.521 ms
2 10.0.0.1 (10.0.0.1) 1.234 ms
3 172.16.0.1 (172.16.0.1) 5.678 ms
4 *
5 dns.google (8.8.8.8) 10.521 ms

# 设置起始TTL
$ traceroute -f 3 8.学习交流加群风哥微信: itpux-com8.8.8
traceroute to 8.8.更多视频教程www.fgedu.net.cn8.8 (8.8.8.8), 30 hops max, 60 byte packets
3 172.16.0.1 (172.16.0.1) 5.678 ms 5.789 ms 5.890 ms
4 * * *
5 dns.google (8.8.8.8) 10.521 ms 10.489 ms 10.512 ms

Part05-故障排查实战

5.1 网络故障诊断流程

# 1. 检查本地网络接口
$ ip addr show
1: lo: mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
link/loopback 00:00:00:00:00:00 brd 00:00:00:00:00:00
inet 127.0.0.1/8 scope host lo
2: eth0: mtu 1500 qdisc mq state UP group default qlen 1000
link/ether 08:00:27:12:34:56 brd ff:ff:ff:ff:ff:ff
inet 192.168.1.100/24 brd 192.168.1.255 scope global noprefixroute eth0

# 2. 测试本地回环
$ ping -c 3 127.0.0.1
PING 127.0.0.1 (127.0.0.1) 56(84) bytes of data.
64 bytes from 127.0.0.1: icmp_seq=1 ttl=64 time=0.021 ms
64 bytes from 127.0.0.1: icmp_seq=2 ttl=64 time=0.019 ms
64 bytes from 127.0.0.1: icmp_seq=3 ttl=64 time=0.018 ms

— 127.0.0.1 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2049ms
rtt min/avg/max/mdev = 0.018/0.019/0.021/0.001 ms

# 3. 测试网关连通性
$ ping -c 3 192.168.1.1
PING 192.168.1.1 (192.168.1.1) 56(84) bytes of data.
64 bytes from 192.168.1.1: icmp_seq=1 ttl=64 time=0.521 ms
64 bytes from 192.168.1.1: icmp_seq=2 ttl=64 time=0.489 ms
64 bytes from 192.168.1.1: icmp_seq=3 ttl=64 time=0.512 ms

— 192.168.1.1 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2049ms
rtt min/avg/max/mdev = 0.489/0.505/0.521/0.013 ms

# 4. 测试外网连通性
$ ping -c 3 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=117 time=10.5 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=117 time=10.2 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=117 time=10.3 ms

— 8.8.8.8 ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 10.196/10.342/10.531/0.138 ms

# 5. 测试DNS解析
$ ping -c 3 www.google.com
PING www.google.com (142.250.185.68) 56(84) bytes of data.
64 bytes from www.google.com (142.250.185.68): icmp_seq=1 ttl=117 time=15.5 ms
64 bytes from www.google.com (142.250.185.68): icmp_seq=2 ttl=117 time=15.2 ms
64 bytes from www.google.com (142.250.185.68): icmp_seq=3 ttl=117 time=15.3 ms

— www.google.com ping statistics —
3 packets transmitted, 3 received, 0% packet loss, time 2003ms
rtt min/avg/max/mdev = 15.196/15.342/15.531/0.138 ms

# 6. 追踪路由
$ traceroute -n 8.8.8.8
traceroute to 8.8.8.8 (8.8.8.8), 30 hops max, 60 byte packets
1 192.168.1.1 0.521 ms 0.489 ms 0.512 ms
2 10.0.0.1 1.234 ms 1.123 ms 1.345 ms
3 172.16.0.1 5.678 ms 5.789 ms 5.890 ms
4 * * *
5 8.8.8.8 10.521 ms 10.489 ms 10.512 ms

风哥针对排查建议:
1. 按照从本地到外网的顺序排查
2. 使用ping测试基本连通性
3. 使用traceroute定位路由问题
4. 注意TTL值和延迟变化
5. 结合其他工具综合诊断

本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html

联系我们

在线咨询:点击这里给我发消息

微信号:itpux-com

工作日:9:30-18:30,节假日休息