网络运维与故障排查 目录
网络基础配置
网络监控工具
性能测试
故障排查方法
常见问题解决
网络安全
网络优化
1. 网络基础配置 1.1 IP 地址配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 ip addr show ip -brief addr show ifconfig -a ip addr add 192.168.1.100/24 dev eth0 ip addr del 192.168.1.100/24 dev eth0 cat > /etc/netplan/01-netcfg.yaml << EOF network: version: 2 ethernets: eth0: addresses: - 192.168.1.100/24 gateway4: 192.168.1.1 nameservers: addresses: [8.8.8.8, 8.8.4.4] EOF netplan apply nmcli con mod eth0 ipv4.addresses 192.168.1.100/24 nmcli con mod eth0 ipv4.gateway 192.168.1.1 nmcli con mod eth0 ipv4.dns "8.8.8.8 8.8.4.4" nmcli con up eth0
1.2 路由配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ip route show route -n netstat -rn ip route add default via 192.168.1.1 ip route add 10.0.0.0/8 via 192.168.1.254 ip route add 172.16.0.0/16 dev eth1 ip route del 10.0.0.0/8 echo "GATEWAY=192.168.1.1" >> /etc/sysconfig/networkecho "10.0.0.0/8 via 192.168.1.254" >> /etc/sysconfig/network-scripts/route-eth0
1.3 DNS 配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 cat /etc/resolv.confnmcli dev show | grep DNS echo "nameserver 8.8.8.8" > /etc/resolv.confecho "nameserver 8.8.4.4" >> /etc/resolv.confcat > /etc/netplan/01-netcfg.yaml << EOF network: version: 2 ethernets: eth0: nameservers: addresses: [8.8.8.8, 1.1.1.1] EOF netplan apply nmcli con mod eth0 ipv4.dns "8.8.8.8 1.1.1.1" nmcli con up eth0 nslookup example.com dig example.com host example.com
1.4 网络接口管理 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ip link set eth0 up ip link set eth0 down ip link show ethtool eth0 ip -s link show eth0 ethtool -S eth0 ip link set eth0 mtu 9000 ethtool -K eth0 gro on tso on gso on
2. 网络监控工具 2.1 流量监控 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 iftop -i eth0 iftop -n -N nethogs eth0 iptraf-ng vnstat -i eth0 vnstat -h vnstat -d vnstat -m
2.2 连接监控 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ss -tulpn netstat -tulpn lsof -i ss -tan netstat -tan ss -tlnp netstat -tlnp watch -n1 'ss -tan | wc -l'
2.3 带宽测试 1 2 3 4 5 6 7 8 9 10 11 12 iperf3 -s iperf3 -c server_ip iperf3 -c server_ip -P 4 iperf3 -c server_ip -R speedtest-cli speedtest-cli --share
2.4 数据包捕获 1 2 3 4 5 6 7 8 9 10 11 12 13 14 tcpdump -i eth0 tcpdump -i eth0 -n port 80 tcpdump -i eth0 -n host 192.168.1.100 tcpdump -i eth0 -w capture.pcap tcpdump -r capture.pcap tshark -i eth0 tshark -i eth0 -f "port 80" tshark -r capture.pcap wireshark capture.pcap
3. 性能测试 3.1 延迟测试 1 2 3 4 5 6 7 8 9 10 11 ping example.com ping -c 10 example.com ping -i 0.2 example.com mtr example.com mtr -rwc 10 example.com ping -D example.com | awk '{print strftime("%Y-%m-%d %H:%M:%S"), $7}'
3.2 路由追踪 1 2 3 4 5 6 7 8 9 10 traceroute example.com traceroute -I example.com tracepath example.com mtr example.com mtr --report example.com
3.3 DNS 性能测试 1 2 3 4 5 6 7 8 9 dig @8.8.8.8 example.com dig +stats example.com time dig example.comfor i in {1..10}; do dig example.com | grep "Query time" ; done
3.4 HTTP 性能测试 1 2 3 4 5 6 7 8 curl -o /dev/null -s -w "DNS: %{time_namelookup}s\nConnect: %{time_connect}s\nTTFB: %{time_starttransfer}s\nTotal: %{time_total}s\n" https://example.com ab -n 1000 -c 100 https://example.com/ wrk -t12 -c400 -d30s https://example.com/
4. 故障排查方法 4.1 排查流程 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 1. 确认问题范围 - 单台主机还是多台? - 特定服务还是所有服务? - 内网还是外网? 2. 检查物理层 - 网线连接 - 接口状态 - LED 指示灯 3. 检查网络层 - IP 配置 - 路由表 - DNS 解析 4. 检查传输层 - 防火墙规则 - 端口监听 - 连接状态 5. 检查应用层 - 服务配置 - 日志信息 - 资源使用
4.2 诊断命令 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ip addr show ip link show ethtool eth0 ip route show traceroute 8.8.8.8 cat /etc/resolv.confnslookup example.com dig example.com ss -tulpn netstat -tan | grep ESTABLISHED | wc -l iptables -L -n firewall-cmd --list-all ufw status systemctl status service journalctl -u service -f
4.3 日志分析 1 2 3 4 5 6 7 8 9 10 11 12 13 tail -f /var/log/syslogtail -f /var/log/messagesjournalctl -f journalctl -u NetworkManager -f journalctl -u systemd-networkd -f dmesg | grep -i eth tail -f /var/log/nginx/error.logtail -f /var/log/apache2/error.log
5. 常见问题解决 5.1 无法访问外网 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 ip route show ping 192.168.1.1 nslookup www.baidu.com dig www.baidu.com iptables -L -n firewall-cmd --list-all iptables -t nat -L -n echo "nameserver 8.8.8.8" > /etc/resolv.confip route add default via 192.168.1.1
5.2 DNS 解析失败 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 cat /etc/resolv.confnslookup example.com 8.8.8.8 nslookup example.com 114.114.114.114 systemctl status systemd-resolved resolvectl status cat > /etc/resolv.conf << EOF nameserver 8.8.8.8 nameserver 114.114.114.114 EOF systemctl restart systemd-resolved /etc/init.d/nscd restart
5.3 端口无法访问 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 ss -tlnp | grep :80 netstat -tlnp | grep :80 iptables -L -n | grep 80 firewall-cmd --list-ports ufw status firewall-cmd --add-port=80/tcp --permanent firewall-cmd --reload ufw allow 80/tcp iptables -A INPUT -p tcp --dport 80 -j ACCEPT getenforce setenforce 0
5.4 网络连接数过多 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 ss -tan | wc -l netstat -tan | wc -l netstat -tan | awk '{print $6}' | sort | uniq -c netstat -tan | grep TIME_WAIT | wc -l cat >> /etc/sysctl.conf << EOF # 允许重用 TIME_WAIT socket net.ipv4.tcp_tw_reuse = 1 # 缩短 FIN-WAIT-2 超时时间 net.ipv4.tcp_fin_timeout = 30 # 增加本地端口范围 net.ipv4.ip_local_port_range = 1024 65535 # 增加最大文件描述符 fs.file-max = 2097152 # 增加连接队列 net.core.somaxconn = 65535 net.ipv4.tcp_max_syn_backlog = 65535 EOF sysctl -p
5.5 网络延迟高 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 ping -c 100 example.com mtr --report example.com traceroute example.com iftop -n cat >> /etc/sysctl.conf << EOF # 启用 BBR 拥塞控制 net.core.default_qdisc=fq net.ipv4.tcp_congestion_control=bbr # 增加缓冲区 net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 EOF sysctl -p modprobe tcp_bbr echo "net.ipv4.tcp_congestion_control=bbr" >> /etc/sysctl.conf
6. 网络安全 6.1 防火墙配置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 iptables -P INPUT DROP iptables -P FORWARD DROP iptables -P OUTPUT ACCEPT iptables -A INPUT -i lo -j ACCEPT iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT iptables -A INPUT -p tcp --dport 22 -j ACCEPT iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT iptables-save > /etc/iptables/rules.v4 firewall-cmd --permanent --add-service=ssh firewall-cmd --permanent --add-service=http firewall-cmd --permanent --add-service=https firewall-cmd --reload ufw default deny incoming ufw default allow outgoing ufw allow ssh ufw allow http ufw allow https ufw enable
6.2 SSH 安全加固 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Port 2222 Protocol 2 PermitRootLogin no PasswordAuthentication no PubkeyAuthentication yes MaxAuthTries 3 ClientAliveInterval 300 ClientAliveCountMax 2 AllowUsers admin deploy DenyUsers root systemctl restart sshd
6.3 DDoS 防护 1 2 3 4 5 6 7 8 9 10 11 12 13 iptables -A INPUT -p tcp --dport 80 -m limit --limit 25/minute --limit-burst 100 -j ACCEPT iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 4 -j ACCEPT apt-get install fail2ban systemctl enable fail2ban systemctl start fail2ban
7. 网络优化 7.1 内核参数优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 net.core.default_qdisc = fq net.ipv4.tcp_congestion_control = bbr net.core.rmem_max = 16777216 net.core.wmem_max = 16777216 net.ipv4.tcp_rmem = 4096 87380 16777216 net.ipv4.tcp_wmem = 4096 65536 16777216 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 30 net.ipv4.ip_local_port_range = 1024 65535 net.core.somaxconn = 65535 net.ipv4.tcp_max_syn_backlog = 65535 net.ipv4.tcp_max_tw_buckets = 5000 net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_keepalive_intvl = 30 net.ipv4.tcp_keepalive_probes = 10 sysctl -p
7.2 网卡优化 1 2 3 4 5 6 7 8 9 10 11 12 13 14 ethtool eth0 ethtool -K eth0 gro on tso on gso on lro on ethtool -G eth0 rx 4096 tx 4096 cat /proc/irq/*/smp_affinity_listethtool -l eth0
7.3 负载均衡 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 global maxconn 4096 nbthread 4 defaults mode http timeout connect 5s timeout client 50s timeout server 50s frontend http_front bind *:80 default_backend http_back backend http_back balance roundrobin server web1 192.168.1.10:80 check server web2 192.168.1.11:80 check server web3 192.168.1.12:80 check upstream backend { least_conn; server 192.168.1.10:8080; server 192.168.1.11:8080; server 192.168.1.12:8080; } server { location / { proxy_pass http://backend; } }
文档版本 : 1.0最后更新 : 2026-02-27适用系统 : Linux (CentOS/RHEL, Ubuntu/Debian)