网络运维与故障排查

目录

  1. 网络基础配置
  2. 网络监控工具
  3. 性能测试
  4. 故障排查方法
  5. 常见问题解决
  6. 网络安全
  7. 网络优化

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 地址(临时)
ip addr add 192.168.1.100/24 dev eth0
ip addr del 192.168.1.100/24 dev eth0

# 配置 IP 地址(永久 - Ubuntu/Netplan)
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

# 配置 IP 地址(永久 - CentOS/NetworkManager)
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

# 永久路由(CentOS)
echo "GATEWAY=192.168.1.1" >> /etc/sysconfig/network
echo "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
# 查看 DNS
cat /etc/resolv.conf
nmcli dev show | grep DNS

# 配置 DNS(临时)
echo "nameserver 8.8.8.8" > /etc/resolv.conf
echo "nameserver 8.8.4.4" >> /etc/resolv.conf

# 配置 DNS(永久 - Ubuntu)
cat > /etc/netplan/01-netcfg.yaml << EOF
network:
version: 2
ethernets:
eth0:
nameservers:
addresses: [8.8.8.8, 1.1.1.1]
EOF
netplan apply

# 配置 DNS(永久 - CentOS)
nmcli con mod eth0 ipv4.dns "8.8.8.8 1.1.1.1"
nmcli con up eth0

# 测试 DNS 解析
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

# 修改 MTU
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 - 实时流量监控
iftop -i eth0
iftop -n -N # 不解析端口和服务名

# nethogs - 按进程监控
nethogs eth0

# iptraf - 交互式流量监控
iptraf-ng

# vnstat - 流量统计
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

# 查看 TCP 连接
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 - 网络性能测试
# 服务端
iperf3 -s

# 客户端
iperf3 -c server_ip
iperf3 -c server_ip -P 4 # 4 个并行连接
iperf3 -c server_ip -R # 反向测试(下载)

# speedtest-cli - 网速测试
speedtest-cli
speedtest-cli --share

2.4 数据包捕获

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# tcpdump - 抓包工具
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 - Wireshark 命令行版
tshark -i eth0
tshark -i eth0 -f "port 80"
tshark -r capture.pcap

# wireshark - 图形界面
wireshark capture.pcap

3. 性能测试

3.1 延迟测试

1
2
3
4
5
6
7
8
9
10
11
# ping - 基础延迟测试
ping example.com
ping -c 10 example.com
ping -i 0.2 example.com # 快速 ping

# mtr - 路由追踪 + ping
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 - 路由追踪
traceroute example.com
traceroute -I example.com # 使用 ICMP

# tracepath - 无需 root
tracepath example.com

# mtr - 动态路由追踪
mtr example.com
mtr --report example.com

3.3 DNS 性能测试

1
2
3
4
5
6
7
8
9
# dig 测试
dig @8.8.8.8 example.com
dig +stats example.com

# DNS 查询时间
time dig example.com

# 批量 DNS 测试
for i in {1..10}; do dig example.com | grep "Query time"; done

3.4 HTTP 性能测试

1
2
3
4
5
6
7
8
# curl 测试
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 - Apache Benchmark
ab -n 1000 -c 100 https://example.com/

# wrk - 现代 HTTP 基准测试
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
# 1. 检查网络接口
ip addr show
ip link show
ethtool eth0

# 2. 检查路由
ip route show
traceroute 8.8.8.8

# 3. 检查 DNS
cat /etc/resolv.conf
nslookup example.com
dig example.com

# 4. 检查连接
ss -tulpn
netstat -tan | grep ESTABLISHED | wc -l

# 5. 检查防火墙
iptables -L -n
firewall-cmd --list-all
ufw status

# 6. 检查服务
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/syslog
tail -f /var/log/messages
journalctl -f

# 网络相关日志
journalctl -u NetworkManager -f
journalctl -u systemd-networkd -f
dmesg | grep -i eth

# 应用日志
tail -f /var/log/nginx/error.log
tail -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
# 1. 检查网关
ip route show
ping 192.168.1.1 # 网关

# 2. 检查 DNS
nslookup www.baidu.com
dig www.baidu.com

# 3. 检查防火墙
iptables -L -n
firewall-cmd --list-all

# 4. 检查 NAT
iptables -t nat -L -n

# 5. 临时解决方案
echo "nameserver 8.8.8.8" > /etc/resolv.conf
ip 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
# 1. 检查 resolv.conf
cat /etc/resolv.conf

# 2. 测试 DNS 服务器
nslookup example.com 8.8.8.8
nslookup example.com 114.114.114.114

# 3. 检查 systemd-resolved
systemctl status systemd-resolved
resolvectl status

# 4. 修复方案
cat > /etc/resolv.conf << EOF
nameserver 8.8.8.8
nameserver 114.114.114.114
EOF

# 5. 清除 DNS 缓存
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
# 1. 检查服务是否监听
ss -tlnp | grep :80
netstat -tlnp | grep :80

# 2. 检查防火墙
iptables -L -n | grep 80
firewall-cmd --list-ports
ufw status

# 3. 开放端口
# firewall-cmd
firewall-cmd --add-port=80/tcp --permanent
firewall-cmd --reload

# ufw
ufw allow 80/tcp

# iptables
iptables -A INPUT -p tcp --dport 80 -j ACCEPT

# 4. 检查 SELinux
getenforce
setenforce 0 # 临时禁用测试

# 5. 检查云服务商安全组
# 需要在云平台控制台配置

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
# 1. 查看连接数
ss -tan | wc -l
netstat -tan | wc -l

# 2. 查看各状态连接数
netstat -tan | awk '{print $6}' | sort | uniq -c

# 3. 查看 TIME_WAIT 连接
netstat -tan | grep TIME_WAIT | wc -l

# 4. 优化内核参数
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
# 1. 测试延迟
ping -c 100 example.com
mtr --report example.com

# 2. 检查路由
traceroute example.com

# 3. 检查带宽
iftop -n

# 4. 优化 TCP 参数
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

# 5. 启用 BBR
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 基础配置
# 默认策略
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

# 允许 SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许 HTTP/HTTPS
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允许 ICMP
iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT

# 保存规则
iptables-save > /etc/iptables/rules.v4

# firewalld 配置
firewall-cmd --permanent --add-service=ssh
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-service=https
firewall-cmd --reload

# ufw 配置
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
# /etc/ssh/sshd_config
Port 2222
Protocol 2
PermitRootLogin no
PasswordAuthentication no
PubkeyAuthentication yes
MaxAuthTries 3
ClientAliveInterval 300
ClientAliveCountMax 2
AllowUsers admin deploy
DenyUsers root

# 重启 SSH
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

# 防止 SYN 洪水
iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT

# 防止 Ping 洪水
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 4 -j ACCEPT

# 安装 fail2ban
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
# /etc/sysctl.conf
# 网络性能优化
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_list

# 多队列优化
ethtool -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
# HAProxy 配置
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

# Nginx 负载均衡
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)