网络连接问题
网络连接问题是Linux运维中的常见问题,可能影响服务可用性和用户体验。本文介绍常见的网络故障及其解决方法。
🚨 问题现象
常见错误信息
# 连接被拒绝
Connection refused
# 网络不可达
Network is unreachable
# 连接超时
Connection timed out
# 域名解析失败
Name or service not known
# 主机不可达
No route to host
系统表现
- 无法访问外部网络
- 服务间无法通信
- 网页无法打开
- SSH连接失败
- 文件传输中断
🔍 问题诊断
1. 检查网络接口状态
# 查看网络接口信息
ip addr show
ifconfig
# 查看接口统计信息
ip -s link show
cat /proc/net/dev
# 检查接口是否启用
ip link show
nmcli device status
2. 检查路由配置
# 查看路由表
ip route show
route -n
# 查看默认网关
ip route | grep default
netstat -rn | grep UG
# 测试到网关的连通性
ping $(ip route | grep default | awk '{print $3}')
3. 检查DNS解析
# 查看DNS配置
cat /etc/resolv.conf
# 测试DNS解析
nslookup google.com
dig google.com
host google.com
# 测试不同DNS服务器
nslookup google.com 8.8.8.8
dig @1.1.1.1 google.com
4. 网络连通性测试
# 测试基本连通性
ping -c 4 8.8.8.8
ping -c 4 google.com
# 追踪路由
traceroute google.com
tracepath google.com
# 测试端口连通性
telnet google.com 80
nc -zv google.com 80
5. 检查防火墙状态
# 查看iptables规则
iptables -L -n
iptables -t nat -L -n
# 查看firewalld状态
firewall-cmd --state
firewall-cmd --list-all
# 查看SELinux状态
getenforce
sestatus
🛠️ 解决方案
网络接口问题
1. 重启网络接口
# 使用ip命令
sudo ip link set eth0 down
sudo ip link set eth0 up
# 使用ifconfig命令
sudo ifconfig eth0 down
sudo ifconfig eth0 up
# 使用NetworkManager
sudo nmcli connection down eth0
sudo nmcli connection up eth0
# 重启网络服务
sudo systemctl restart network
sudo systemctl restart NetworkManager
2. 配置静态IP
# CentOS/RHEL - 编辑网络配置文件
sudo vim /etc/sysconfig/network-scripts/ifcfg-eth0
# 配置内容
TYPE=Ethernet
BOOTPROTO=static
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
NETMASK=255.255.255.0
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
# Ubuntu/Debian - 使用netplan
sudo vim /etc/netplan/01-netcfg.yaml
# 配置内容
network:
version: 2
ethernets:
eth0:
dhcp4: no
addresses: [192.168.1.100/24]
gateway4: 192.168.1.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
# 应用配置
sudo netplan apply
3. 使用NetworkManager配置
# 查看连接
nmcli connection show
# 修改连接
nmcli connection modify eth0 ipv4.addresses 192.168.1.100/24
nmcli connection modify eth0 ipv4.gateway 192.168.1.1
nmcli connection modify eth0 ipv4.dns "8.8.8.8,8.8.4.4"
nmcli connection modify eth0 ipv4.method manual
# 激活连接
nmcli connection up eth0
路由问题解决
1. 添加路由
# 添加默认路由
sudo ip route add default via 192.168.1.1
# 添加特定网络路由
sudo ip route add 10.0.0.0/8 via 192.168.1.1
# 永久添加路由(CentOS/RHEL)
echo "10.0.0.0/8 via 192.168.1.1" | sudo tee -a /etc/sysconfig/network-scripts/route-eth0
2. 删除错误路由
# 删除默认路由
sudo ip route del default
# 删除特定路由
sudo ip route del 10.0.0.0/8 via 192.168.1.1
DNS问题解决
1. 修复DNS配置
# 编辑DNS配置
sudo vim /etc/resolv.conf
# 配置内容
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1
# 防止被覆盖
sudo chattr +i /etc/resolv.conf
2. 清理DNS缓存
# 清理systemd-resolved缓存
sudo systemd-resolve --flush-caches
# 重启DNS服务
sudo systemctl restart systemd-resolved
# 清理nscd缓存
sudo systemctl restart nscd
防火墙问题解决
1. 临时禁用防火墙
# 停止iptables
sudo systemctl stop iptables
# 停止firewalld
sudo systemctl stop firewalld
# 清空iptables规则
sudo iptables -F
sudo iptables -X
sudo iptables -t nat -F
sudo iptables -t nat -X
2. 添加防火墙规则
# 允许特定端口
sudo firewall-cmd --add-port=80/tcp --permanent
sudo firewall-cmd --add-port=443/tcp --permanent
sudo firewall-cmd --reload
# 使用iptables
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
🔧 网络故障排查脚本
自动化网络诊断脚本
#!/bin/bash
# network_diagnosis.sh
echo "======== 网络故障诊断报告 ========"
echo "诊断时间: $(date)"
echo
# 1. 检查网络接口
echo "=== 网络接口状态 ==="
ip link show | grep -E "^[0-9]+|state"
echo
# 2. 检查IP配置
echo "=== IP地址配置 ==="
ip addr show | grep -E "inet |UP"
echo
# 3. 检查路由
echo "=== 路由表 ==="
ip route show
echo
# 4. 检查DNS
echo "=== DNS配置 ==="
cat /etc/resolv.conf | grep nameserver
echo
# 5. 测试网络连通性
echo "=== 连通性测试 ==="
ping -c 1 -W 2 8.8.8.8 &>/dev/null && echo "✓ Internet connectivity: OK" || echo "✗ Internet connectivity: FAILED"
ping -c 1 -W 2 google.com &>/dev/null && echo "✓ DNS resolution: OK" || echo "✗ DNS resolution: FAILED"
# 6. 检查网关
GATEWAY=$(ip route | grep default | awk '{print $3}' | head -1)
if [ -n "$GATEWAY" ]; then
ping -c 1 -W 2 $GATEWAY &>/dev/null && echo "✓ Gateway ($GATEWAY): OK" || echo "✗ Gateway ($GATEWAY): FAILED"
fi
echo
echo "=== 建议操作 ==="
if ! ping -c 1 -W 2 8.8.8.8 &>/dev/null; then
echo "1. 检查网络线缆连接"
echo "2. 重启网络接口: sudo ifdown eth0 && sudo ifup eth0"
echo "3. 检查网络配置文件"
echo "4. 联系网络管理员"
fi
if ! ping -c 1 -W 2 google.com &>/dev/null; then
echo "1. 检查/etc/resolv.conf配置"
echo "2. 尝试使用不同的DNS服务器"
echo "3. 清理DNS缓存"
fi
网络重置脚本
#!/bin/bash
# network_reset.sh
echo "重置网络配置..."
# 停止网络服务
sudo systemctl stop NetworkManager
sudo systemctl stop network
# 重启网络接口
for iface in $(ip link show | grep -E "^[0-9]+: (eth|ens|enp)" | cut -d: -f2 | tr -d ' '); do
echo "重启接口: $iface"
sudo ip link set $iface down
sleep 2
sudo ip link set $iface up
done
# 重启网络服务
sudo systemctl start NetworkManager
sudo systemctl start network
# 清理DNS缓存
sudo systemd-resolve --flush-caches
# 重新获取IP
sudo dhclient -r
sudo dhclient
echo "网络重置完成"
echo "当前网络状态:"
ip addr show | grep -E "inet |UP"
📊 网络性能监控
1. 监控网络流量
# 实时监控接口流量
watch -n 1 'cat /proc/net/dev'
# 使用iftop监控流量
sudo iftop -i eth0
# 使用nload监控
nload eth0
# 使用vnstat统计
vnstat -i eth0
2. 监控网络连接
# 查看当前连接
ss -tuln
netstat -tuln
# 监控连接数
watch -n 1 'ss -s'
# 查看网络连接状态统计
netstat -s
🚨 应急处理步骤
网络完全中断时
检查物理连接
# 检查网卡是否被识别 lspci | grep -i ethernet dmesg | grep -i ethernet
重启网络服务
sudo systemctl restart NetworkManager sudo systemctl restart network
重置网络配置
sudo nmcli connection reload sudo nmcli device disconnect eth0 sudo nmcli device connect eth0
使用备用网络
# 配置USB网卡或WiFi sudo nmcli device wifi list sudo nmcli device wifi connect SSID password PASSWORD
📚 常用网络工具
- ping - 测试连通性
- traceroute - 路由追踪
- nmap - 网络扫描
- tcpdump - 数据包捕获
- wireshark - 数据包分析
- iperf3 - 网络性能测试
- mtr - 网络诊断工具
通过系统化的诊断方法和合适的工具,大多数网络问题都可以快速定位和解决。
网络故障排查是运维工作的重要技能,通过系统性的诊断方法可以快速定位和解决网络问题。