网络故障排查

核心知识点

1. 网络故障类型

1.1 连接故障

┌─────────────────────────────────────────────────────┐
│              连接故障类型                           │
├─────────────────────────────────────────────────────┤
│                                                     │
│   ┌─────────────────────────────────────────┐       │
│   │           物理层故障                   │       │
│   │  - 网线故障                             │       │
│   │  - 网卡故障                             │       │
│   │  - 交换机故障                           │       │
│   │  - 路由器故障                           │       │
│   └─────────────────────────────────────────┘       │
│                      │                              │
│   ┌─────────────────────────────────────────┐       │
│   │           链路层故障                   │       │
│   │  - MAC 地址冲突                         │       │
│   │  - VLAN 配置错误                         │       │
│   │  - 网桥配置错误                         │       │
│   └─────────────────────────────────────────┘       │
│                      │                              │
│   ┌─────────────────────────────────────────┐       │
│   │           网络层故障                   │       │
│   │  - IP 地址冲突                           │       │
│   │  - 子网掩码错误                           │       │
│   │  - 路由配置错误                           │       │
│   │  - DNS 解析失败                          │       │
│   └─────────────────────────────────────────┘       │
│                      │                              │
│   ┌─────────────────────────────────────────┐       │
│   │           传输层故障                   │       │
│   │  - 端口被占用                             │       │
│   │  - 防火墙阻止                             │       │
│   │  - 连接超时                               │       │
│   └─────────────────────────────────────────┘       │
│                                                     │
└─────────────────────────────────────────────────────┘

1.2 性能故障

故障类型 表现 常见原因
延迟高 响应时间长 路由跳数多、网络拥塞
丢包 数据丢失 网络质量差、拥塞
带宽低 传输速度慢 网络限制、设备性能
抖动大 延迟不稳定 网络拥塞、QoS 配置

2. 网络诊断工具

2.1 基础诊断工具

# ping - 测试连通性
ping -c 100 google.com
ping -i 0.2 -c 100 google.com

# traceroute - 路由跟踪
traceroute google.com
traceroute -n google.com

# tracepath - 路径跟踪
tracepath google.com

# mtr - 网络诊断
mtr google.com
mtr -c 100 -r google.com

2.2 网络状态工具

# ifconfig - 网络接口配置
ifconfig
ifconfig eth0

# ip - 网络配置工具
ip addr show
ip link show
ip route show

# netstat - 网络统计
netstat -an
netstat -rn
netstat -s

# ss - 套接字统计
ss -s
ss -tunap
ss -tunlp

2.3 端口扫描工具

# nmap - 端口扫描
nmap -sS <host>
nmap -sU <host>
nmap -p 1-65535 <host>

# nc - 网络调试
nc -zv <host> <port>
nc -l -p <port>

# telnet - 端口测试
telnet <host> <port>

2.4 抓包工具

# tcpdump - 抓包工具
tcpdump -i eth0
tcpdump -i eth0 -w capture.pcap
tcpdump -i eth0 host <host>
tcpdump -i eth0 port <port>

# wireshark - 图形化抓包工具
wireshark
wireshark capture.pcap

# tshark - 命令行抓包工具
tshark -i eth0
tshark -r capture.pcap

3. 连接故障排查

3.1 物理层故障

# 1. 检查网络接口
ip link show
ifconfig

# 2. 检查网卡状态
ethtool eth0
ethtool -i eth0

# 3. 检查连接状态
mii-tool eth0

# 4. 查看网卡统计
ethtool -S eth0

# 5. 重启网络接口
sudo ip link set eth0 down
sudo ip link set eth0 up

# 6. 检查网线
# - 检查网线连接
# - 更换网线
# - 测试网线

3.2 IP 配置故障

# 1. 查看 IP 配置
ip addr show
ifconfig

# 2. 检查 IP 地址冲突
arp -a
arping -D -I eth0 <ip>

# 3. 配置 IP 地址
sudo ip addr add 192.168.1.100/24 dev eth0
sudo ip addr del 192.168.1.100/24 dev eth0

# 4. 配置网关
sudo ip route add default via 192.168.1.1
sudo ip route del default

# 5. 配置 DNS
sudo nano /etc/resolv.conf
# nameserver 8.8.8.8
# nameserver 8.8.4.4

# 6. 测试连通性
ping 8.8.8.8
ping google.com

3.3 路由故障

# 1. 查看路由表
ip route show
netstat -rn

# 2. 添加路由
sudo ip route add 192.168.2.0/24 via 192.168.1.1

# 3. 删除路由
sudo ip route del 192.168.2.0/24 via 192.168.1.1

# 4. 测试路由
traceroute google.com
mtr google.com

# 5. 查看网关
ip route | grep default

# 6. 检查网关连通性
ping 192.168.1.1

3.4 DNS 故障

# 1. 查看 DNS 配置
cat /etc/resolv.conf

# 2. 测试 DNS 解析
nslookup google.com
dig google.com
host google.com

# 3. 测试 DNS 服务器
nslookup google.com 8.8.8.8
dig @8.8.8.8 google.com

# 4. 清除 DNS 缓存
sudo systemd-resolve --flush-caches
sudo service dnsmasq restart

# 5. 配置 DNS
sudo nano /etc/resolv.conf
# nameserver 8.8.8.8
# nameserver 8.8.4.4

# 6. 测试 DNS 响应时间
time nslookup google.com
time dig google.com

4. 端口故障排查

4.1 端口占用排查

# 1. 查看端口占用
netstat -tunlp | grep <port>
ss -tunlp | grep <port>
lsof -i :<port>

# 2. 查看进程端口
netstat -anp | grep <pid>
ss -anp | grep <pid>

# 3. 查看端口状态
netstat -an | grep <port>
ss -an | grep <port>

# 4. 测试端口连接
nc -zv <host> <port>
telnet <host> <port>

# 5. 查看端口监听
netstat -tlnp
ss -tlnp

4.2 防火墙排查

# 1. 查看 iptables 规则
sudo iptables -L -n
sudo iptables -L -n -v

# 2. 查看 ufw 状态
sudo ufw status
sudo ufw status numbered

# 3. 查看 firewalld 状态
sudo firewall-cmd --state
sudo firewall-cmd --list-all

# 4. 添加防火墙规则
sudo iptables -A INPUT -p tcp --dport <port> -j ACCEPT
sudo ufw allow <port>/tcp
sudo firewall-cmd --add-port=<port>/tcp --permanent

# 5. 删除防火墙规则
sudo iptables -D INPUT -p tcp --dport <port> -j ACCEPT
sudo ufw delete allow <port>/tcp
sudo firewall-cmd --remove-port=<port>/tcp --permanent

# 6. 重载防火墙
sudo iptables-restore < /etc/iptables.rules
sudo ufw reload
sudo firewall-cmd --reload

4.3 连接超时排查

# 1. 测试连接
timeout 10 nc -zv <host> <port>
timeout 10 telnet <host> <port>

# 2. 查看连接状态
netstat -an | grep <port>
ss -an | grep <port>

# 3. 查看连接超时设置
cat /proc/sys/net/ipv4/tcp_fin_timeout
cat /proc/sys/net/ipv4/tcp_keepalive_time

# 4. 调整超时设置
echo 30 > /proc/sys/net/ipv4/tcp_fin_timeout
echo 600 > /proc/sys/net/ipv4/tcp_keepalive_time

# 5. 查看连接队列
cat /proc/sys/net/core/somaxconn
cat /proc/sys/net/ipv4/tcp_max_syn_backlog

# 6. 增加连接队列
echo 4096 > /proc/sys/net/core/somaxconn
echo 4096 > /proc/sys/net/ipv4/tcp_max_syn_backlog

5. 性能故障排查

5.1 延迟排查

# 1. 测试延迟
ping -c 100 <host>
mtr -c 100 <host>

# 2. 查看路由跳数
traceroute <host>
tracepath <host>

# 3. 分析延迟原因
# - 检查网络拓扑
# - 检查网络拥塞
# - 检查路由配置

# 4. 优化路由
# - 选择更短路径
# - 使用 BGP 优化
# - 使用 CDN

# 5. 优化 TCP 参数
echo 1 > /proc/sys/net/ipv4/tcp_low_latency

# 6. 验证优化效果
ping -c 100 <host>

5.2 丢包排查

# 1. 测试丢包
ping -c 1000 <host> | grep "packet loss"
mtr -c 100 -r <host>

# 2. 查看网络统计
netstat -s | grep -i error
ip -s link show eth0

# 3. 查看网卡统计
ethtool -S eth0

# 4. 检查网络设备
# - 检查交换机
# - 检查路由器
# - 检查网线

# 5. 优化网络配置
# - 调整 MTU
# - 启用流控
# - 优化队列

# 6. 验证优化效果
ping -c 1000 <host>

5.3 带宽排查

# 1. 测试带宽
iperf3 -s
iperf3 -c <server-ip> -t 60

# 2. 查看网络流量
iftop
nload
bmon

# 3. 查看网卡速率
ethtool eth0

# 4. 检查网络限制
# - 检查 ISP 带宽
# - 检查设备带宽
# - 检查流量控制

# 5. 优化带宽使用
# - 启用压缩
# - 使用缓存
# - 优化协议

# 6. 验证优化效果
iperf3 -c <server-ip> -t 60

6. 网络抓包分析

6.1 tcpdump 抓包

# 1. 抓取所有数据包
sudo tcpdump -i eth0

# 2. 抓取特定主机数据包
sudo tcpdump -i eth0 host <host>

# 3. 抓取特定端口数据包
sudo tcpdump -i eth0 port <port>

# 4. 保存到文件
sudo tcpdump -i eth0 -w capture.pcap

# 5. 读取抓包文件
sudo tcpdump -r capture.pcap

# 6. 显示详细信息
sudo tcpdump -i eth0 -v
sudo tcpdump -i eth0 -vv

6.2 wireshark 分析

# 1. 打开抓包文件
wireshark capture.pcap

# 2. 过滤数据包
# - 过滤 IP:ip.addr == <ip>
# - 过滤端口:tcp.port == <port>
# - 过滤协议:http

# 3. 分析数据包
# - 查看数据包详情
# - 查看数据包内容
# - 统计数据包

# 4. 导出数据包
# - 导出为 JSON
# - 导出为 CSV
# - 导出为 XML

# 5. 时间线分析
# - 查看时间线
# - 分析延迟
# - 分析抖动

实用案例分析

案例 1:无法连接到远程服务器

场景描述

无法通过 SSH 连接到远程服务器。

排查步骤

# 1. 测试网络连通性
ping <server-ip>

# 2. 测试端口连通性
nc -zv <server-ip> 22
telnet <server-ip> 22

# 3. 检查本地网络
ip addr show
ip route show

# 4. 检查防火墙
sudo iptables -L -n
sudo ufw status

# 5. 检查 SSH 服务
systemctl status sshd
systemctl status ssh

# 6. 查看 SSH 日志
tail -f /var/log/auth.log
tail -f /var/log/secure

# 7. 测试 SSH 连接
ssh -v <user>@<server-ip>

# 8. 解决问题
# - 检查防火墙规则
# - 检查 SSH 配置
# - 检查网络连接

案例 2:网络延迟高

场景描述

访问远程服务延迟很高。

排查步骤

# 1. 测试延迟
ping -c 100 <server-ip>

# 2. 查看路由
traceroute <server-ip>
mtr -c 100 <server-ip>

# 3. 分析延迟原因
# - 检查网络拓扑
# - 检查网络拥塞
# - 检查路由配置

# 4. 优化路由
# - 选择更短路径
# - 使用 BGP 优化
# - 使用 CDN

# 5. 优化 TCP 参数
echo 1 > /proc/sys/net/ipv4/tcp_low_latency

# 6. 验证优化效果
ping -c 100 <server-ip>

案例 3:端口无法访问

场景描述

Web 服务端口无法从外部访问。

排查步骤

# 1. 测试本地端口
curl http://localhost:80

# 2. 检查端口监听
netstat -tlnp | grep :80
ss -tlnp | grep :80

# 3. 检查防火墙
sudo iptables -L -n
sudo ufw status

# 4. 测试外部访问
curl http://<server-ip>:80

# 5. 抓包分析
sudo tcpdump -i eth0 port 80

# 6. 解决问题
# - 添加防火墙规则
# - 检查服务配置
# - 检查网络配置

# 7. 验证效果
curl http://<server-ip>:80

最佳实践

  1. 使用分层排查法:从物理层到应用层逐层排查。

  2. 记录排查过程:便于后续分析和总结。

  3. 使用多种工具:从不同角度分析问题。

  4. 定期监控网络:及时发现网络问题。

  5. 建立网络拓扑:了解网络结构和连接关系。

  6. 配置告警机制:及时发现网络故障。

  7. 定期测试网络:验证网络性能。

  8. 文档记录:记录问题和解决方案。

总结

本教程详细介绍了 Linux 系统网络故障的排查方法和实践。通过实际案例,我们学习了如何排查连接故障、端口故障和性能故障,以及如何使用网络诊断工具和抓包工具。掌握这些知识后,可以快速定位和解决网络问题。

« 上一篇 系统故障排查 下一篇 » 应用故障排查