网络故障
网络故障排查的关键是分层排查、逐步缩小范围。本文按照从物理层到应用层的顺序,介绍一套系统性的网络排查方法。
排查思路:从底层到顶层
Section titled “排查思路:从底层到顶层”网络故障排查应该遵循 OSI 模型从底层向上的顺序:
- 物理层 / 链路层 — 网线、网卡、链路状态
- 网络层 — IP 地址、路由、网关
- DNS — 域名解析
- 防火墙 — firewalld / iptables 规则
- 传输层 / 应用层 — 端口、服务监听状态
原则: 不要跳过低层直接排查高层。如果 IP 地址都没配对,排查 DNS 毫无意义。
第一步:检查物理链路
Section titled “第一步:检查物理链路”$ ip link show关注 state UP 还是 state DOWN。如果接口处于 DOWN 状态:
$ sudo ip link set eth0 up$ lspci | grep -i ethernet$ lsmod | grep -i e1000 # 示例:Intel 网卡$ sudo ethtool eth0在输出中查找 Link detected: yes。如果显示 no,说明物理连接有问题(网线、交换机端口、虚拟网络配置等)。
$ ip -s link show eth0关注 errors 和 dropped 计数。大量错误可能意味着硬件问题或双工模式不匹配。
第二步:检查 IP 配置
Section titled “第二步:检查 IP 配置”$ ip addr show$ ip route show确认以下信息:
- 接口有正确的 IP 地址
- 子网掩码正确
- 存在默认路由(
default via x.x.x.x)
$ ping -c 3 $(ip route show default | awk '{print $3}')如果 ping 网关失败,问题在本地网络配置或物理连接。
使用 nmcli 检查和配置网络
Section titled “使用 nmcli 检查和配置网络”$ nmcli connection show$ nmcli connection show "有线连接 1"$ nmcli device status$ sudo nmcli connection down "有线连接 1" && sudo nmcli connection up "有线连接 1"常见 IP 配置问题
Section titled “常见 IP 配置问题”DHCP 未获取到地址:
$ sudo nmcli connection modify "有线连接 1" ipv4.method auto$ sudo nmcli connection up "有线连接 1"$ journalctl -u NetworkManager --since "5 minutes ago" | grep -i dhcp设置静态 IP:
$ sudo nmcli connection modify "有线连接 1" \ ipv4.method manual \ ipv4.addresses "192.168.1.100/24" \ ipv4.gateway "192.168.1.1" \ ipv4.dns "8.8.8.8 8.8.4.4"$ sudo nmcli connection up "有线连接 1"第三步:检查 DNS 解析
Section titled “第三步:检查 DNS 解析”如果 IP 层正常(能 ping 通 IP 地址)但无法访问域名:
$ cat /etc/resolv.conf$ dig google.com$ dig @8.8.8.8 google.com$ dig +short google.com$ dig -x 8.8.8.8常见 DNS 问题
Section titled “常见 DNS 问题”resolv.conf 被覆盖:
NetworkManager 会管理 /etc/resolv.conf。如果手动修改总是被覆盖:
$ sudo nmcli connection modify "有线连接 1" ipv4.dns "8.8.8.8 114.114.114.114"$ sudo nmcli connection up "有线连接 1"DNS 解析慢:
$ time dig google.com如果延迟很高,可能是 DNS 服务器不可达或响应慢。尝试更换 DNS:
$ dig @114.114.114.114 google.com$ dig @223.5.5.5 google.com本地 hosts 文件问题:
$ cat /etc/hosts$ grep hosts /etc/nsswitch.conf正常配置一般为:hosts: files dns myhostname
第四步:检查防火墙
Section titled “第四步:检查防火墙”防火墙是 EL 系统上网络问题的最常见原因之一。服务正常运行但外部无法访问,十有八九是防火墙。
$ sudo firewall-cmd --state$ sudo firewall-cmd --list-all$ sudo firewall-cmd --list-services$ sudo firewall-cmd --list-ports临时放行端口进行测试
Section titled “临时放行端口进行测试”$ sudo firewall-cmd --add-port=8080/tcp$ sudo firewall-cmd --add-service=http$ sudo firewall-cmd --add-port=8080/tcp --permanent$ sudo firewall-cmd --reload$ sudo firewall-cmd --add-service=http --permanent$ sudo firewall-cmd --add-service=https --permanent$ sudo firewall-cmd --reload排查 firewalld 阻止的流量
Section titled “排查 firewalld 阻止的流量”$ sudo firewall-cmd --set-log-denied=all$ sudo journalctl -f | grep REJECT测试完毕后关闭日志:
$ sudo firewall-cmd --set-log-denied=off紧急排除防火墙因素
Section titled “紧急排除防火墙因素”如果需要快速确认是否是防火墙问题:
$ sudo firewall-cmd --zone=trusted --change-interface=eth0警告: 这会完全开放该接口,仅用于临时测试,测试后立即恢复。
$ sudo firewall-cmd --zone=public --change-interface=eth0第五步:检查服务和端口
Section titled “第五步:检查服务和端口”$ sudo ss -tlnp$ sudo ss -ulnp$ sudo ss -tlnp | grep :80如果服务没有监听预期的端口,检查服务状态:
$ systemctl status nginx$ journalctl -u nginx -n 30 --no-pager使用 curl 测试 HTTP 服务
Section titled “使用 curl 测试 HTTP 服务”$ curl -v http://localhost/$ curl -I http://localhost/$ curl -v http://localhost:8080/使用 traceroute 追踪路由
Section titled “使用 traceroute 追踪路由”$ sudo dnf install -y traceroute$ traceroute 8.8.8.8$ sudo traceroute -T -p 80 example.com使用 tcpdump 抓包分析
Section titled “使用 tcpdump 抓包分析”$ sudo tcpdump -i eth0 port 80 -nn -c 20$ sudo tcpdump -i eth0 port 443 -w /tmp/capture.pcap -c 100常见网络故障场景
Section titled “常见网络故障场景”场景一:服务器能 ping 通但 HTTP 不通
Section titled “场景一:服务器能 ping 通但 HTTP 不通”排查顺序:
- 确认 Web 服务在运行:
systemctl status nginx - 确认服务监听正确端口:
ss -tlnp | grep :80 - 确认本地可以访问:
curl http://localhost/ - 检查防火墙:
firewall-cmd --list-all - 检查 SELinux:
ausearch -m avc -ts recent
场景二:DNS 能解析但连接超时
Section titled “场景二:DNS 能解析但连接超时”$ timeout 5 bash -c 'echo > /dev/tcp/目标IP/目标端口' && echo "端口可达" || echo "端口不可达"可能原因:
- 对方防火墙阻挡
- 路由问题(traceroute 排查)
- ISP 或中间网络设备阻断
场景三:NetworkManager 连接反复断开重连
Section titled “场景三:NetworkManager 连接反复断开重连”$ journalctl -u NetworkManager --since "30 minutes ago" | grep -E '(connect|disconnect|fail)'$ dmesg | grep -i 'link'可能原因:
- 网线接触不良
- 网卡驱动问题
- DHCP 租约冲突
场景四:迁移后网络配置丢失
Section titled “场景四:迁移后网络配置丢失”从 CentOS 迁移后,网络接口名称或配置文件可能变化:
$ ls /etc/NetworkManager/system-connections/$ ls /etc/sysconfig/network-scripts/ifcfg-* 2>/dev/null$ sudo nmcli connection add type ethernet con-name "主连接" ifname eth0$ sudo nmcli connection modify "主连接" ipv4.method auto$ sudo nmcli connection up "主连接"网络排查速查表
Section titled “网络排查速查表”| 目标 | 命令 |
|---|---|
| 查看接口状态 | ip link show |
| 查看 IP 地址 | ip addr show |
| 查看路由表 | ip route show |
| 测试连通性 | ping -c 3 目标 |
| DNS 解析 | dig 域名 |
| 查看监听端口 | ss -tlnp |
| 追踪路由 | traceroute 目标 |
| 防火墙规则 | firewall-cmd --list-all |
| 查看连接 | nmcli connection show |
| 抓包分析 | tcpdump -i 接口 port 端口 |
| HTTP 测试 | curl -v URL |
| NetworkManager 日志 | journalctl -u NetworkManager |