Linux 网络排查与故障诊断实战 2026

🔌 网络问题是运维最常见的挑战之一。 从简单的连接失败到复杂的 TCP 丢包,网络故障千变万化。掌握一套系统化的排查方法和工具链,是每个 Linux 管理员的必备技能。本文带你从基础到进阶,全面掌握网络故障诊断。
本文将带你系统掌握:
- ✅ 网络排查方法论与思路
- ✅ ping / traceroute / mtr 基础工具
- ✅ netstat / ss 连接状态分析
- ✅ tcpdump / wireshark 抓包分析
- ✅ DNS 故障排查
- ✅ 防火墙与 SELinux 问题
- ✅ TCP 连接问题分析
- ✅ 常见问题处理与案例
一、网络排查方法论
1.1 分层排查法
按照 OSI 模型从下往上排查:
第1层 物理层 → 网线、网卡、端口
第2层 数据链路层 → ARP、MAC 地址、VLAN
第3层 网络层 → IP 地址、路由、ICMP
第4层 传输层 → TCP/UDP、端口、连接状态
第5-7层 应用层 → DNS、HTTP、应用协议排查顺序:
1. 检查本地网络配置
2. 检查物理连接
3. 检查路由表
4. 检查防火墙
5. 检查 DNS
6. 检查应用服务
7. 抓包分析1.2 系统化排查流程
目标:www.example.com 无法访问
步骤:
1. ping 127.0.0.1 → 检查本地回环
2. ping 本机IP → 检查网卡
3. ping 网关 → 检查局域网连接
4. ping 8.8.8.8 → 检查外网连接
5. nslookup example.com → 检查 DNS
6. curl http://example.com → 检查应用层二、基础工具
2.1 ping
# 基本用法
ping 8.8.8.8
ping www.example.com
# 指定次数
ping -c 4 8.8.8.8
# 指定间隔(秒)
ping -i 0.5 8.8.8.8
# 指定数据包大小
ping -s 1500 8.8.8.8
# 不解析域名(更快)
ping -n 8.8.8.8
# 输出时间戳
ping -D 8.8.8.8结果分析:
| 结果 | 含义 |
|---|---|
| 100% 丢包 | 网络不通 |
| 高延迟 | 网络拥塞或路由问题 |
| 不稳定延迟 | 网络抖动 |
| Destination Host Unreachable | 目标不可达 |
| Unknown host | DNS 解析失败 |
2.2 traceroute / tracepath
# 追踪路由
traceroute www.example.com
tracepath www.example.com
# IPv6
traceroute6 www.example.com
# 使用 ICMP
traceroute -I www.example.com
# 使用 TCP
traceroute -T -p 80 www.example.com
# 使用 UDP(默认)
traceroute www.example.com结果分析:
1 192.168.1.1 (192.168.1.1) 0.5ms
2 10.0.0.1 (10.0.0.1) 2ms
3 * * * # 超时,可能被防火墙拦截
4 202.96.128.1 (202.96.128.1) 10ms
...2.3 mtr(结合 ping 和 traceroute)
# 安装
apt install mtr
yum install mtr
# 使用
mtr www.example.com
# 生成报告
mtr --report www.example.com
# 指定时间
mtr --report --report-cycles 100 www.example.com
# IPv6
mtr6 www.example.com结果分析:
| 列 | 含义 |
|---|---|
| Loss% | 丢包率 |
| Snt | 发送数据包数 |
| Last | 最后延迟 |
| Avg | 平均延迟 |
| Best | 最小延迟 |
| Wrst | 最大延迟 |
| StDev | 标准差 |
三、连接状态分析
3.1 netstat
# 所有连接
netstat -a
# TCP 连接
netstat -at
# UDP 连接
netstat -au
# 监听端口
netstat -tnlp
# 查看进程名
netstat -tnlp | grep 80
# 统计连接数
netstat -tn | grep ESTABLISHED | wc -l
# 按状态统计
netstat -tn | awk '{print $6}' | sort | uniq -c3.2 ss(netstat 替代品)
# 所有连接
ss -a
# TCP 连接
ss -t
# UDP 连接
ss -u
# 监听端口
ss -tlnp
# 查看进程名
ss -tlnp | grep nginx
# 统计连接数
ss -tn state established | wc -l
# 按状态统计
ss -tn | awk '{print $1}' | sort | uniq -c
# 查看某个端口的连接
ss -tn sport = :80
# 查看连接到某个IP的
ss -tn dst 192.168.1.100
# 查看连接状态详情
ss -tn state established '( dport = :80 or sport = :80 )'3.3 TCP 连接状态
LISTEN → 等待连接请求
SYN_SENT → 发送 SYN,等待对方确认
SYN_RECV → 收到 SYN,等待 ACK
ESTABLISHED → 连接已建立
FIN_WAIT1 → 主动关闭,发送 FIN
FIN_WAIT2 → 等待对方 FIN
TIME_WAIT → 等待足够时间确保对方收到 ACK
CLOSE_WAIT → 被动关闭,收到 FIN
LAST_ACK → 发送 FIN,等待 ACK
CLOSED → 连接关闭常见问题:
| 状态 | 问题 |
|---|---|
| 大量 SYN_RECV | SYN 攻击或后端服务慢 |
| 大量 TIME_WAIT | 短连接过多,可优化 |
| 大量 CLOSE_WAIT | 应用未正确关闭连接 |
四、抓包分析
4.1 tcpdump
# 安装
apt install tcpdump
yum install tcpdump
# 抓所有包
tcpdump
# 指定网卡
tcpdump -i eth0
# 指定协议
tcpdump -i eth0 tcp
tcpdump -i eth0 udp
tcpdump -i eth0 icmp
# 指定端口
tcpdump -i eth0 port 80
tcpdump -i eth0 port 80 or port 443
# 指定主机
tcpdump -i eth0 host 192.168.1.100
tcpdump -i eth0 src 192.168.1.100
tcpdump -i eth0 dst 192.168.1.100
# 指定长度(显示完整包)
tcpdump -i eth0 -s 0
# 保存到文件
tcpdump -i eth0 -w capture.pcap
# 读取文件
tcpdump -r capture.pcap
# 显示详细信息
tcpdump -i eth0 -vvv
# 显示 ASCII
tcpdump -i eth0 -A
# 显示十六进制
tcpdump -i eth0 -X
# 过滤 HTTP 请求
tcpdump -i eth0 tcp port 80 -A | grep "GET\|POST"
# 过滤 DNS
tcpdump -i eth0 udp port 53 -vvv
# 过滤 SYN 包
tcpdump -i eth0 'tcp[tcpflags] & tcp-syn != 0'
# 过滤 RST 包
tcpdump -i eth0 'tcp[tcpflags] & tcp-rst != 0'4.2 常见过滤表达式
# TCP 三次握手
tcpdump -i eth0 'tcp[tcpflags] & (tcp-syn|tcp-ack) != 0'
# TCP 四次挥手
tcpdump -i eth0 'tcp[tcpflags] & (tcp-fin|tcp-rst) != 0'
# 丢包检测
tcpdump -i eth0 'tcp[tcpflags] & tcp-dupack != 0'
# 重传检测
tcpdump -i eth0 'tcp[tcpflags] & tcp-retrans != 0'
# 特定协议
tcpdump -i eth0 'tcp port 443 and ssl.handshake.type == 1'4.3 Wireshark(图形界面)
# 安装
apt install wireshark
yum install wireshark
# 查看抓包文件
wireshark capture.pcap常用过滤器:
| 过滤器 | 含义 |
|---|---|
http | HTTP 协议 |
https | HTTPS 协议 |
dns | DNS 协议 |
tcp.port == 80 | 端口 80 |
ip.addr == 192.168.1.1 | 指定 IP |
tcp.flags.syn == 1 | SYN 包 |
tcp.analysis.retransmission | 重传包 |
五、DNS 故障排查
5.1 检查 DNS 配置
# 查看 /etc/resolv.conf
cat /etc/resolv.conf
# nameserver 8.8.8.8
# nameserver 8.8.4.4
# 查看 DNS 服务状态(systemd-resolved)
systemctl status systemd-resolved
# 查看 DNS 缓存
systemd-resolve --statistics5.2 nslookup / dig / host
# nslookup
nslookup www.example.com
nslookup www.example.com 8.8.8.8
# dig(更详细)
dig www.example.com
dig www.example.com @8.8.8.8
dig www.example.com +short
dig www.example.com MX # 邮件记录
dig www.example.com TXT # 文本记录
dig -x 8.8.8.8 # 反向解析
# host
host www.example.com
host 8.8.8.85.3 DNS 缓存问题
# 清空 systemd-resolved 缓存
systemd-resolve --flush-caches
# 清空 dnsmasq 缓存
pkill -HUP dnsmasq
# 测试不同 DNS
dig www.example.com @8.8.8.8
dig www.example.com @1.1.1.1
dig www.example.com @223.5.5.55.4 DNS 常见问题
| 问题 | 原因 | 解决 |
|---|---|---|
| 域名解析慢 | DNS 服务器响应慢 | 更换 DNS |
| 解析错误 | DNS 缓存过期 | 清空缓存 |
| 无法解析 | DNS 服务器不可达 | 检查网络 |
| 解析到错误 IP | DNS 污染 | 使用加密 DNS |
六、防火墙与安全组
6.1 iptables
# 查看规则
iptables -L
iptables -L -n
iptables -L -n -v
# 查看 NAT 规则
iptables -t nat -L
# 查看详细规则
iptables -S
# 允许端口
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 允许特定 IP
iptables -A INPUT -s 192.168.1.0/24 -j ACCEPT
# 拒绝特定 IP
iptables -A INPUT -s 10.0.0.1 -j DROP
# 保存规则
iptables-save > /etc/iptables/rules.v4
# 加载规则
iptables-restore < /etc/iptables/rules.v46.2 firewalld(CentOS/RHEL)
# 查看状态
systemctl status firewalld
# 查看规则
firewall-cmd --list-all
firewall-cmd --list-ports
# 开放端口
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --add-port=443/tcp --permanent
# 开放服务
firewall-cmd --add-service=http --permanent
firewall-cmd --add-service=https --permanent
# 开放特定 IP
firewall-cmd --add-source=192.168.1.0/24 --permanent
# 重新加载
firewall-cmd --reload6.3 ufw(Ubuntu/Debian)
# 查看状态
ufw status
# 启用
ufw enable
# 禁用
ufw disable
# 允许端口
ufw allow 80/tcp
ufw allow 443/tcp
# 允许特定 IP
ufw allow from 192.168.1.0/24
# 拒绝
ufw deny from 10.0.0.1
# 删除规则
ufw delete allow 80/tcp6.4 SELinux
# 查看状态
sestatus
getenforce
# 临时关闭(测试)
setenforce 0
# 永久关闭(不推荐)
sed -i 's/^SELINUX=.*/SELINUX=disabled/' /etc/selinux/config
# 查看 SELinux 策略
ls -Z
# 设置 SELinux 上下文
chcon -R -t httpd_sys_content_t /var/www/html
# 查看 SELinux 日志
grep "SELinux is preventing" /var/log/audit/audit.log七、路由与网关
7.1 查看路由表
# 查看路由表
route -n
ip route show
# 查看网关
ip route show default
# 查看接口配置
ip addr show
ifconfig
# 查看接口状态
ip link show
# 查看网络统计
ip -s link show eth07.2 添加路由
# 添加默认路由
ip route add default via 192.168.1.1
# 添加静态路由
ip route add 10.0.0.0/8 via 192.168.1.100
# 添加策略路由
ip rule add from 192.168.2.0/24 table 100
ip route add default via 192.168.1.200 table 100
# 删除路由
ip route del 10.0.0.0/8
# 保存路由
ip route save > /etc/iproute2/rt_tables7.3 常见路由问题
| 问题 | 原因 | 解决 |
|---|---|---|
| 无法访问外网 | 默认网关错误 | 检查并修复 |
| 只能访问局域网 | 缺少默认路由 | 添加默认路由 |
| 特定网段无法访问 | 缺少静态路由 | 添加静态路由 |
八、TCP 连接问题
8.1 连接拒绝
# 服务未启动
ss -tlnp | grep 80
# 端口被占用
lsof -i :80
fuser 80/tcp
# 防火墙阻止
iptables -L -n | grep 80
firewall-cmd --list-ports | grep 80
# SELinux 阻止
grep "denied" /var/log/audit/audit.log | grep httpd8.2 连接超时
# 检查网络连通性
ping target_ip
mtr target_ip
# 检查端口是否开放
telnet target_ip 80
nc -zv target_ip 80
# 检查防火墙规则
iptables -L -n | grep DROP
# 检查网络 ACL
iptables -L FORWARD -n8.3 慢连接
# 检查延迟
ping target_ip
# 检查带宽
iperf3 -c server_ip
# 检查 TCP 参数
cat /proc/sys/net/ipv4/tcp_congestion_control
# 检查网络拥塞
ss -tn state established | wc -l8.4 TCP 参数调优
# /etc/sysctl.conf
# TCP 超时时间
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 300
# 最大连接数
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
# 内存参数
net.core.rmem_max = 16777216
net.core.wmem_max = 16777216
# 缓冲区
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# 复用 TIME_WAIT
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_tw_recycle = 0
# 应用修改
sysctl -p九、常见问题案例
9.1 案例一:网站无法访问
症状: 访问网站超时
排查步骤:
# 1. 检查服务是否运行
ss -tlnp | grep 80
systemctl status nginx
# 2. 检查本地网络
ping 127.0.0.1
ping 本机IP
# 3. 检查防火墙
iptables -L -n | grep 80
ufw status
# 4. 检查端口连通性
telnet localhost 80
# 5. 检查外部连接
curl http://localhost
# 6. 检查 DNS
nslookup www.example.com
# 7. 检查路由
ip route show default
# 8. 抓包分析
tcpdump -i eth0 port 80 -w capture.pcap9.2 案例二:SSH 连接慢
症状: SSH 连接需要等待很长时间才能输入密码
原因: DNS 反向解析慢
解决:
# /etc/ssh/sshd_config
UseDNS no
GSSAPIAuthentication no
systemctl restart sshd9.3 案例三:TCP 连接数过多
症状: 服务器响应慢,大量连接堆积
排查:
# 查看连接状态
ss -tn | awk '{print $1}' | sort | uniq -c
# 查看连接数最多的 IP
ss -tn | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr
# 限制连接数(iptables)
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 100 -j DROP9.4 案例四:DNS 解析失败
症状: 域名无法解析,但 IP 可以访问
排查:
# 1. 检查 DNS 配置
cat /etc/resolv.conf
# 2. 测试 DNS
nslookup www.example.com
dig www.example.com
# 3. 测试不同 DNS 服务器
dig www.example.com @8.8.8.8
dig www.example.com @1.1.1.1
# 4. 清空缓存
systemd-resolve --flush-caches
# 5. 更换 DNS
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 1.1.1.1" >> /etc/resolv.conf十、常用工具速查
# ===== 基础检查 =====
ping # 连通性测试
traceroute / tracepath # 路由追踪
mtr # 连续 ping + 路由追踪
# ===== 连接状态 =====
netstat # 传统连接查看
ss # 现代连接查看(推荐)
lsof # 打开文件/端口查看
# ===== 抓包 =====
tcpdump # 命令行抓包
wireshark # 图形界面抓包
# ===== DNS =====
nslookup # DNS 查询
dig # DNS 查询(更详细)
host # DNS 查询
# ===== 网络配置 =====
ip addr # IP 地址
ip route # 路由表
ip link # 网络接口
ifconfig # 传统接口查看
# ===== 防火墙 =====
iptables # 传统防火墙
ufw # Ubuntu 简化防火墙
firewalld # CentOS 防火墙十一、总结
网络排查的关键在于系统化和耐心:
- 分层排查:从物理层到应用层逐步排查
- 基础工具:ping、traceroute、mtr 是起点
- 连接分析:ss 查看连接状态,识别异常
- 抓包分析:tcpdump 是终极武器
- DNS 问题:nslookup/dig 快速定位
- 防火墙:检查 iptables/firewalld/ufw
- TCP 参数:适当调优提升性能
🔌 网络问题不可怕,掌握方法就能快速定位。
相关文章推荐: