Linux网络配置与管理实战指南:从基础到高级的网络运维技能
Orion K Lv6

网络配置和管理是Linux系统管理的重要组成部分。本文将深入探讨Linux网络配置的各个方面,从基础的网络接口配置到高级的网络优化和故障排查,帮助你全面掌握Linux网络管理技能。

一、Linux网络基础概念

1.1 网络接口类型

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 查看网络接口
ip link show
ifconfig -a

# 常见接口类型:
# eth0, ens33 - 以太网接口
# wlan0, wlp2s0 - 无线网络接口
# lo - 回环接口
# br0 - 网桥接口
# tun0, tap0 - 虚拟网络接口
# docker0 - Docker网桥
# virbr0 - 虚拟机网桥

# 查看接口详细信息
ip addr show
ip addr show eth0
ethtool eth0 # 查看网卡硬件信息
lspci | grep -i network # 查看网卡硬件
lsusb | grep -i network # 查看USB网卡

1.2 网络配置文件位置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
# CentOS/RHEL/Fedora
/etc/sysconfig/network-scripts/ifcfg-eth0
/etc/sysconfig/network
/etc/resolv.conf
/etc/hosts

# Ubuntu/Debian
/etc/network/interfaces # 传统配置
/etc/netplan/*.yaml # Ubuntu 18.04+
/etc/systemd/network/ # systemd-networkd
/etc/NetworkManager/ # NetworkManager

# 通用配置文件
/etc/hosts # 主机名解析
/etc/resolv.conf # DNS配置
/etc/nsswitch.conf # 名称解析顺序
/etc/services # 服务端口映射
/proc/net/ # 网络状态信息

1.3 网络工具概览

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
# 传统网络工具 (net-tools)
ifconfig # 接口配置
route # 路由管理
netstat # 网络连接状态
arp # ARP表管理

# 现代网络工具 (iproute2)
ip # 综合网络管理
ss # 套接字统计

# 网络诊断工具
ping # 连通性测试
traceroute # 路由跟踪
nslookup/dig # DNS查询
telnet # 端口测试
nc (netcat) # 网络瑞士军刀

二、网络接口配置

2.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
# 查看网络接口
ip link show
ip addr show

# 启用/禁用网络接口
ip link set eth0 up
ip link set eth0 down

# 配置IP地址
ip addr add 192.168.1.100/24 dev eth0
ip addr del 192.168.1.100/24 dev eth0

# 配置多个IP地址
ip addr add 192.168.1.100/24 dev eth0
ip addr add 192.168.1.101/24 dev eth0

# 查看特定接口的地址
ip addr show eth0

# 清空接口所有IP地址
ip addr flush dev eth0

# 设置接口MTU
ip link set eth0 mtu 1500

# 设置接口MAC地址
ip link set eth0 address 00:11:22:33:44:55

2.2 永久网络配置

CentOS/RHEL配置

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
# 编辑网络配置文件
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << 'EOF'
TYPE=Ethernet
PROXY_METHOD=none
BROWSER_ONLY=no
BOOTPROTO=static
DEFROUTE=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=12345678-1234-1234-1234-123456789abc
DEVICE=eth0
ONBOOT=yes
IPADDR=192.168.1.100
PREFIX=24
GATEWAY=192.168.1.1
DNS1=8.8.8.8
DNS2=8.8.4.4
EOF

# 重启网络服务
systemctl restart network
# 或者
systemctl restart NetworkManager

# 重启特定接口
ifdown eth0 && ifup eth0

Ubuntu配置 (Netplan)

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
# 编辑netplan配置文件
cat > /etc/netplan/01-network-manager-all.yaml << 'EOF'
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: false
addresses:
- 192.168.1.100/24
gateway4: 192.168.1.1
nameservers:
addresses:
- 8.8.8.8
- 8.8.4.4
search:
- example.com
EOF

# 应用配置
netplan apply

# 测试配置
netplan try

# 生成配置
netplan generate

传统Debian/Ubuntu配置

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 编辑interfaces文件
cat > /etc/network/interfaces << 'EOF'
auto lo
iface lo inet loopback

auto eth0
iface eth0 inet static
address 192.168.1.100
netmask 255.255.255.0
gateway 192.168.1.1
dns-nameservers 8.8.8.8 8.8.4.4
dns-search example.com
EOF

# 重启网络服务
systemctl restart networking

# 重启特定接口
ifdown eth0 && ifup eth0

2.3 DHCP配置

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
# CentOS/RHEL DHCP配置
cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << 'EOF'
TYPE=Ethernet
BOOTPROTO=dhcp
DEFROUTE=yes
NAME=eth0
DEVICE=eth0
ONBOOT=yes
EOF

# Ubuntu Netplan DHCP配置
cat > /etc/netplan/01-dhcp.yaml << 'EOF'
network:
version: 2
ethernets:
eth0:
dhcp4: true
dhcp6: true
EOF

# 传统interfaces DHCP配置
cat >> /etc/network/interfaces << 'EOF'
auto eth0
iface eth0 inet dhcp
EOF

# 手动获取DHCP地址
dhclient eth0
dhclient -r eth0 # 释放DHCP地址

# 查看DHCP租约信息
cat /var/lib/dhcp/dhclient.leases

三、路由管理

3.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
# 查看路由表
ip route show
route -n
netstat -rn

# 添加路由
ip route add 10.0.0.0/8 via 192.168.1.1
ip route add default via 192.168.1.1
ip route add 172.16.0.0/16 dev eth1

# 删除路由
ip route del 10.0.0.0/8
ip route del default

# 修改路由
ip route change 10.0.0.0/8 via 192.168.1.2

# 查看特定目标的路由
ip route get 8.8.8.8

# 添加多路径路由
ip route add default \
nexthop via 192.168.1.1 weight 1 \
nexthop via 192.168.1.2 weight 1

3.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# CentOS/RHEL 永久路由
cat > /etc/sysconfig/network-scripts/route-eth0 << 'EOF'
10.0.0.0/8 via 192.168.1.1
172.16.0.0/16 via 192.168.1.1
EOF

# Ubuntu Netplan 路由配置
cat > /etc/netplan/01-routes.yaml << 'EOF'
network:
version: 2
ethernets:
eth0:
addresses:
- 192.168.1.100/24
routes:
- to: 10.0.0.0/8
via: 192.168.1.1
- to: 172.16.0.0/16
via: 192.168.1.1
gateway4: 192.168.1.1
EOF

# 传统方式添加永久路由
echo "10.0.0.0/8 via 192.168.1.1" >> /etc/network/interfaces

# 使用systemd添加路由服务
cat > /etc/systemd/system/custom-routes.service << 'EOF'
[Unit]
Description=Custom Routes
After=network.target

[Service]
Type=oneshot
ExecStart=/sbin/ip route add 10.0.0.0/8 via 192.168.1.1
ExecStart=/sbin/ip route add 172.16.0.0/16 via 192.168.1.1
RemainAfterExit=yes

[Install]
WantedBy=multi-user.target
EOF

systemctl enable custom-routes.service

3.3 策略路由

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# 查看路由表
ip route show table main
ip route show table local
ip rule show

# 创建自定义路由表
echo "200 custom" >> /etc/iproute2/rt_tables

# 添加策略路由规则
ip rule add from 192.168.1.0/24 table custom
ip rule add to 10.0.0.0/8 table custom
ip rule add fwmark 1 table custom

# 在自定义表中添加路由
ip route add default via 192.168.2.1 table custom
ip route add 10.0.0.0/8 via 192.168.2.1 table custom

# 删除策略路由规则
ip rule del from 192.168.1.0/24 table custom

# 刷新路由缓存
ip route flush cache

四、DNS配置和管理

4.1 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
28
29
30
31
# 配置DNS服务器
cat > /etc/resolv.conf << 'EOF'
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 1.1.1.1
search example.com local.domain
options timeout:2 attempts:3
EOF

# 防止resolv.conf被覆盖
chattr +i /etc/resolv.conf

# 使用systemd-resolved
systemctl enable systemd-resolved
systemctl start systemd-resolved
ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf

# 配置systemd-resolved
cat > /etc/systemd/resolved.conf << 'EOF'
[Resolve]
DNS=8.8.8.8 8.8.4.4
FallbackDNS=1.1.1.1 1.0.0.1
Domains=example.com
LLMNR=yes
MulticastDNS=yes
DNSSEC=yes
Cache=yes
DNSStubListener=yes
EOF

systemctl restart systemd-resolved

4.2 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
28
29
# 使用nslookup
nslookup google.com
nslookup google.com 8.8.8.8
nslookup -type=MX google.com
nslookup -type=NS google.com

# 使用dig (更强大)
dig google.com
dig @8.8.8.8 google.com
dig google.com MX
dig google.com NS
dig +trace google.com # 跟踪DNS解析过程
dig +short google.com # 简短输出
dig -x 8.8.8.8 # 反向DNS查询

# 使用host命令
host google.com
host -t MX google.com
host 8.8.8.8

# 测试DNS响应时间
for i in {1..10}; do
time dig +short google.com > /dev/null
done

# DNS缓存管理
systemctl flush-dns # systemd-resolved
sudo systemd-resolve --flush-caches
resolvectl flush-caches

4.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
28
29
30
31
32
33
34
35
36
# 安装dnsmasq
yum install dnsmasq # CentOS/RHEL
apt install dnsmasq # Ubuntu/Debian

# 配置dnsmasq
cat > /etc/dnsmasq.conf << 'EOF'
# 监听接口
interface=eth0

# DNS上游服务器
server=8.8.8.8
server=8.8.4.4

# 本地域名解析
address=/local.example.com/192.168.1.100

# DHCP配置
dhcp-range=192.168.1.50,192.168.1.150,12h
dhcp-option=3,192.168.1.1 # 网关
dhcp-option=6,192.168.1.1 # DNS服务器

# 日志配置
log-queries
log-dhcp

# 缓存大小
cache-size=1000
EOF

# 启动dnsmasq
systemctl enable dnsmasq
systemctl start dnsmasq

# 测试本地DNS
dig @localhost google.com
nslookup google.com localhost

五、防火墙配置

5.1 iptables基础

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
# 查看iptables规则
iptables -L -n -v
iptables -t nat -L -n -v
iptables -t mangle -L -n -v

# 基本规则操作
# 允许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

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

# 允许回环接口
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 删除规则
iptables -D INPUT 1
iptables -F # 清空所有规则
iptables -X # 删除自定义链

5.2 iptables高级配置

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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# 创建完整的防火墙脚本
cat > /usr/local/bin/firewall.sh << 'EOF'
#!/bin/bash
# 防火墙配置脚本

# 清空现有规则
iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X

# 设置默认策略
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT ACCEPT

# 允许回环接口
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# 允许已建立的连接
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT

# 允许SSH (限制连接频率)
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --set
iptables -A INPUT -p tcp --dport 22 -m state --state NEW -m recent --update --seconds 60 --hitcount 4 -j DROP
iptables -A INPUT -p tcp --dport 22 -j ACCEPT

# 允许Web服务
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT

# 允许DNS
iptables -A INPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --dport 53 -j ACCEPT

# 允许NTP
iptables -A INPUT -p udp --dport 123 -j ACCEPT

# 允许ping (限制频率)
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT

# 防止端口扫描
iptables -A INPUT -m state --state NEW -p tcp --tcp-flags ALL ALL -j DROP
iptables -A INPUT -m state --state NEW -p tcp --tcp-flags ALL NONE -j DROP

# 记录被丢弃的包
iptables -A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7

# 保存规则
service iptables save
# 或者
iptables-save > /etc/iptables/rules.v4
EOF

chmod +x /usr/local/bin/firewall.sh

5.3 firewalld配置

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
39
40
41
42
43
44
# 启动firewalld
systemctl enable firewalld
systemctl start firewalld

# 查看状态
firewall-cmd --state
firewall-cmd --get-default-zone
firewall-cmd --get-active-zones
firewall-cmd --list-all

# 区域管理
firewall-cmd --get-zones
firewall-cmd --set-default-zone=public
firewall-cmd --zone=public --list-all

# 服务管理
firewall-cmd --get-services
firewall-cmd --add-service=http
firewall-cmd --add-service=https
firewall-cmd --add-service=ssh
firewall-cmd --remove-service=dhcpv6-client

# 端口管理
firewall-cmd --add-port=8080/tcp
firewall-cmd --add-port=1000-2000/tcp
firewall-cmd --remove-port=8080/tcp

# 永久配置
firewall-cmd --permanent --add-service=http
firewall-cmd --permanent --add-port=8080/tcp
firewall-cmd --reload

# 富规则
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.0/24" accept'
firewall-cmd --add-rich-rule='rule family="ipv4" source address="10.0.0.1" port protocol="tcp" port="22" accept'
firewall-cmd --add-rich-rule='rule family="ipv4" source address="192.168.1.100" reject'

# 端口转发
firewall-cmd --add-forward-port=port=80:proto=tcp:toport=8080
firewall-cmd --add-forward-port=port=80:proto=tcp:toaddr=192.168.1.100

# 伪装(NAT)
firewall-cmd --add-masquerade
firewall-cmd --query-masquerade

六、网络监控和诊断

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
# 查看网络连接
ss -tuln # 监听端口
ss -tulpn # 包含进程信息
ss -an # 所有连接
ss -s # 统计信息

# netstat (传统工具)
netstat -tuln # 监听端口
netstat -tulpn # 包含进程信息
netstat -an # 所有连接
netstat -i # 接口统计

# 查看特定端口
ss -tlnp | grep :80
netstat -tlnp | grep :80
lsof -i :80

# 查看特定进程的网络连接
ss -p | grep nginx
netstat -p | grep nginx
lsof -p $(pgrep nginx)

# 实时监控网络连接
watch -n 1 'ss -tuln'
watch -n 1 'netstat -i'

6.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
# 使用iftop监控实时流量
iftop
iftop -i eth0
iftop -n # 不解析主机名
iftop -P # 显示端口

# 使用nethogs按进程监控流量
nethogs
nethogs eth0

# 使用vnstat统计流量
vnstat # 显示统计信息
vnstat -i eth0 # 特定接口
vnstat -d # 按天统计
vnstat -m # 按月统计
vnstat -h # 按小时统计

# 使用sar监控网络
sar -n DEV 1 10 # 网络接口统计
sar -n EDEV 1 10 # 网络错误统计
sar -n TCP 1 10 # TCP统计

# 使用nload监控带宽
nload
nload eth0

# 简单的流量监控脚本
cat > network_monitor.sh << 'EOF'
#!/bin/bash
# 网络流量监控脚本

INTERFACE="eth0"
INTERVAL=1

while true; do
RX_BYTES=$(cat /sys/class/net/$INTERFACE/statistics/rx_bytes)
TX_BYTES=$(cat /sys/class/net/$INTERFACE/statistics/tx_bytes)

sleep $INTERVAL

RX_BYTES_NEW=$(cat /sys/class/net/$INTERFACE/statistics/rx_bytes)
TX_BYTES_NEW=$(cat /sys/class/net/$INTERFACE/statistics/tx_bytes)

RX_RATE=$(( (RX_BYTES_NEW - RX_BYTES) / INTERVAL ))
TX_RATE=$(( (TX_BYTES_NEW - TX_BYTES) / INTERVAL ))

echo "$(date): RX: $(( RX_RATE / 1024 )) KB/s, TX: $(( TX_RATE / 1024 )) KB/s"
done
EOF

chmod +x network_monitor.sh

6.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
35
36
37
38
39
40
41
# 使用iperf3测试带宽
# 服务器端
iperf3 -s

# 客户端
iperf3 -c server_ip
iperf3 -c server_ip -t 30 # 测试30秒
iperf3 -c server_ip -P 4 # 4个并行连接
iperf3 -c server_ip -u # UDP测试

# 使用ping测试延迟
ping -c 10 8.8.8.8
ping -i 0.1 -c 100 8.8.8.8 # 高频ping
ping -s 1472 8.8.8.8 # 大包ping

# 使用mtr进行网络诊断
mtr google.com
mtr --report --report-cycles 100 google.com

# 使用tcptraceroute
tcptraceroute google.com 80

# 网络延迟测试脚本
cat > latency_test.sh << 'EOF'
#!/bin/bash
# 网络延迟测试脚本

HOSTS=("8.8.8.8" "1.1.1.1" "google.com" "github.com")
COUNT=10

echo "Network Latency Test - $(date)"
echo "======================================"

for host in "${HOSTS[@]}"; do
echo "Testing $host:"
ping -c $COUNT -q $host | tail -1 | awk -F'/' '{print " Average: " $5 " ms"}'
echo
done
EOF

chmod +x latency_test.sh

七、网络故障排查

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
25
26
27
28
29
30
31
32
33
34
35
# 网络故障排查步骤

# 1. 检查物理连接
ip link show
ethtool eth0
dmesg | grep -i network

# 2. 检查IP配置
ip addr show
ip route show
cat /etc/resolv.conf

# 3. 测试本地连通性
ping 127.0.0.1 # 回环测试
ping $(ip route | grep default | awk '{print $3}') # 网关测试

# 4. 测试DNS解析
nslookup google.com
dig google.com

# 5. 测试外部连通性
ping 8.8.8.8 # 公网IP
ping google.com # 域名解析

# 6. 检查端口连通性
telnet google.com 80
nc -zv google.com 80

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

# 8. 检查服务状态
systemctl status NetworkManager
systemctl status network

7.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
# 创建网络诊断脚本
cat > network_diagnosis.sh << 'EOF'
#!/bin/bash
# 网络故障诊断脚本

REPORT_FILE="/tmp/network_diagnosis_$(date +%Y%m%d_%H%M%S).txt"
DATE=$(date '+%Y-%m-%d %H:%M:%S')

echo "=== Network Diagnosis Report ===" > "$REPORT_FILE"
echo "Date: $DATE" >> "$REPORT_FILE"
echo "Hostname: $(hostname)" >> "$REPORT_FILE"
echo >> "$REPORT_FILE"

# 1. 网络接口信息
echo "1. Network Interfaces:" >> "$REPORT_FILE"
ip link show >> "$REPORT_FILE" 2>&1
echo >> "$REPORT_FILE"

# 2. IP地址配置
echo "2. IP Addresses:" >> "$REPORT_FILE"
ip addr show >> "$REPORT_FILE" 2>&1
echo >> "$REPORT_FILE"

# 3. 路由表
echo "3. Routing Table:" >> "$REPORT_FILE"
ip route show >> "$REPORT_FILE" 2>&1
echo >> "$REPORT_FILE"

# 4. DNS配置
echo "4. DNS Configuration:" >> "$REPORT_FILE"
cat /etc/resolv.conf >> "$REPORT_FILE" 2>&1
echo >> "$REPORT_FILE"

# 5. 网络连接
echo "5. Network Connections:" >> "$REPORT_FILE"
ss -tuln >> "$REPORT_FILE" 2>&1
echo >> "$REPORT_FILE"

# 6. 防火墙状态
echo "6. Firewall Status:" >> "$REPORT_FILE"
if command -v firewall-cmd &> /dev/null; then
firewall-cmd --list-all >> "$REPORT_FILE" 2>&1
elif command -v iptables &> /dev/null; then
iptables -L -n >> "$REPORT_FILE" 2>&1
fi
echo >> "$REPORT_FILE"

# 7. 连通性测试
echo "7. Connectivity Tests:" >> "$REPORT_FILE"
echo "Localhost ping:" >> "$REPORT_FILE"
ping -c 3 127.0.0.1 >> "$REPORT_FILE" 2>&1

GATEWAY=$(ip route | grep default | awk '{print $3}' | head -1)
if [ -n "$GATEWAY" ]; then
echo "Gateway ping ($GATEWAY):" >> "$REPORT_FILE"
ping -c 3 "$GATEWAY" >> "$REPORT_FILE" 2>&1
fi

echo "External ping (8.8.8.8):" >> "$REPORT_FILE"
ping -c 3 8.8.8.8 >> "$REPORT_FILE" 2>&1

echo "DNS resolution test:" >> "$REPORT_FILE"
nslookup google.com >> "$REPORT_FILE" 2>&1
echo >> "$REPORT_FILE"

# 8. 网络统计
echo "8. Network Statistics:" >> "$REPORT_FILE"
cat /proc/net/dev >> "$REPORT_FILE" 2>&1
echo >> "$REPORT_FILE"

# 9. 系统日志相关网络错误
echo "9. Recent Network Errors:" >> "$REPORT_FILE"
journalctl -u NetworkManager --since "1 hour ago" --no-pager >> "$REPORT_FILE" 2>&1
dmesg | grep -i "network\|eth\|link" | tail -20 >> "$REPORT_FILE" 2>&1
echo >> "$REPORT_FILE"

echo "=== End of Report ===" >> "$REPORT_FILE"

# 显示报告
cat "$REPORT_FILE"

# 提供修复建议
echo
echo "=== Troubleshooting Suggestions ==="
echo "1. Check physical cable connections"
echo "2. Restart network service: systemctl restart NetworkManager"
echo "3. Reset network interface: ip link set eth0 down && ip link set eth0 up"
echo "4. Check DHCP: dhclient -r eth0 && dhclient eth0"
echo "5. Flush DNS cache: systemctl restart systemd-resolved"
echo "6. Check firewall rules: firewall-cmd --list-all"
echo "7. Review system logs: journalctl -u NetworkManager"
EOF

chmod +x network_diagnosis.sh

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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
# 1. 网络接口无法启动
# 检查配置文件语法
nmcli connection show
nmcli connection reload
nmcli connection up eth0

# 重置NetworkManager
systemctl restart NetworkManager
nmcli general reload

# 2. DNS解析问题
# 刷新DNS缓存
systemctl restart systemd-resolved
resolvectl flush-caches

# 临时修改DNS
echo "nameserver 8.8.8.8" > /etc/resolv.conf

# 3. 路由问题
# 添加默认路由
ip route add default via 192.168.1.1

# 删除错误路由
ip route del 0.0.0.0/0

# 4. 防火墙阻塞
# 临时关闭防火墙
systemctl stop firewalld
iptables -F

# 5. 网络性能问题
# 调整网络参数
echo 'net.core.rmem_max = 16777216' >> /etc/sysctl.conf
echo 'net.core.wmem_max = 16777216' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_rmem = 4096 87380 16777216' >> /etc/sysctl.conf
echo 'net.ipv4.tcp_wmem = 4096 65536 16777216' >> /etc/sysctl.conf
sysctl -p

# 6. 网络接口重置脚本
cat > reset_network.sh << 'EOF'
#!/bin/bash
# 网络接口重置脚本

INTERFACE="$1"

if [ -z "$INTERFACE" ]; then
echo "Usage: $0 <interface>"
exit 1
fi

echo "Resetting network interface $INTERFACE..."

# 关闭接口
ip link set "$INTERFACE" down
sleep 2

# 清除IP地址
ip addr flush dev "$INTERFACE"

# 启动接口
ip link set "$INTERFACE" up
sleep 2

# 重新获取DHCP
if [ -f "/etc/sysconfig/network-scripts/ifcfg-$INTERFACE" ]; then
ifup "$INTERFACE"
else
dhclient "$INTERFACE"
fi

echo "Network interface $INTERFACE reset completed"
ip addr show "$INTERFACE"
EOF

chmod +x reset_network.sh

八、网络安全和优化

8.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
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
# 1. 禁用不必要的网络服务
systemctl disable telnet
systemctl disable rsh
systemctl disable rlogin

# 2. 配置TCP Wrappers
cat > /etc/hosts.allow << 'EOF'
sshd: 192.168.1.0/24
sshd: 10.0.0.0/8
ALL: localhost
EOF

cat > /etc/hosts.deny << 'EOF'
ALL: ALL
EOF

# 3. 网络参数安全加固
cat > /etc/sysctl.d/99-network-security.conf << 'EOF'
# IP转发控制
net.ipv4.ip_forward = 0
net.ipv6.conf.all.forwarding = 0

# 源路由保护
net.ipv4.conf.all.accept_source_route = 0
net.ipv4.conf.default.accept_source_route = 0
net.ipv6.conf.all.accept_source_route = 0

# ICMP重定向保护
net.ipv4.conf.all.accept_redirects = 0
net.ipv4.conf.default.accept_redirects = 0
net.ipv6.conf.all.accept_redirects = 0
net.ipv4.conf.all.send_redirects = 0
net.ipv4.conf.default.send_redirects = 0

# 反向路径过滤
net.ipv4.conf.all.rp_filter = 1
net.ipv4.conf.default.rp_filter = 1

# 忽略ping
net.ipv4.icmp_echo_ignore_all = 0
net.ipv4.icmp_echo_ignore_broadcasts = 1

# SYN flood保护
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_max_syn_backlog = 2048
net.ipv4.tcp_synack_retries = 2
net.ipv4.tcp_syn_retries = 5

# 记录可疑包
net.ipv4.conf.all.log_martians = 1
net.ipv4.conf.default.log_martians = 1

# 时间戳保护
net.ipv4.tcp_timestamps = 0
EOF

sysctl -p /etc/sysctl.d/99-network-security.conf

8.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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
# 网络性能优化配置
cat > /etc/sysctl.d/99-network-performance.conf << 'EOF'
# TCP缓冲区大小
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216

# TCP窗口缩放
net.ipv4.tcp_window_scaling = 1

# TCP拥塞控制
net.ipv4.tcp_congestion_control = bbr

# TCP快速打开
net.ipv4.tcp_fastopen = 3

# 网络队列长度
net.core.netdev_max_backlog = 5000

# TCP连接队列
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535

# TCP keepalive
net.ipv4.tcp_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 60
net.ipv4.tcp_keepalive_probes = 3

# TCP重用和回收
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 30

# UDP缓冲区
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
EOF

sysctl -p /etc/sysctl.d/99-network-performance.conf

# 网卡队列优化
ethtool -G eth0 rx 4096 tx 4096
ethtool -K eth0 gro on
ethtool -K eth0 gso on
ethtool -K eth0 tso on

# 中断亲和性优化
cat > optimize_irq.sh << 'EOF'
#!/bin/bash
# 网卡中断优化脚本

INTERFACE="eth0"
CPU_COUNT=$(nproc)

# 获取网卡中断号
IRQS=$(grep "$INTERFACE" /proc/interrupts | awk -F: '{print $1}' | tr -d ' ')

# 分配中断到不同CPU
i=0
for irq in $IRQS; do
cpu=$((i % CPU_COUNT))
echo $((1 << cpu)) > /proc/irq/$irq/smp_affinity
echo "IRQ $irq assigned to CPU $cpu"
((i++))
done
EOF

chmod +x optimize_irq.sh

九、总结和最佳实践

9.1 网络管理最佳实践

  1. 配置管理

    • 使用版本控制管理网络配置文件
    • 建立配置变更审批流程
    • 定期备份网络配置
    • 文档化所有网络变更
  2. 监控和告警

    • 建立网络性能基线
    • 配置关键指标告警
    • 定期检查网络健康状态
    • 建立故障响应流程
  3. 安全防护

    • 实施网络分段
    • 配置适当的防火墙规则
    • 定期更新安全配置
    • 监控异常网络活动

9.2 故障预防措施

  1. 定期维护

    • 更新网络驱动程序
    • 检查网络硬件状态
    • 清理网络配置
    • 优化网络参数
  2. 容量规划

    • 监控网络使用率
    • 预测流量增长
    • 规划网络扩容
    • 优化网络架构
  3. 灾难恢复

    • 建立网络配置备份
    • 制定故障恢复计划
    • 定期演练恢复流程
    • 准备应急网络方案

9.3 工具推荐

  1. 监控工具

    • Nagios/Zabbix - 网络监控
    • MRTG/Cacti - 流量监控
    • Wireshark - 包分析
    • nmap - 网络扫描
  2. 性能工具

    • iperf3 - 带宽测试
    • mtr - 网络诊断
    • tcpdump - 包捕获
    • netstat/ss - 连接监控
  3. 管理工具

    • Ansible - 配置管理
    • Terraform - 基础设施即代码
    • Git - 配置版本控制
    • IPAM - IP地址管理

通过掌握这些Linux网络配置和管理技能,你将能够有效地管理复杂的网络环境,确保网络的稳定性、安全性和高性能。记住,网络管理是一个持续的过程,需要不断学习新技术和最佳实践。


本文全面介绍了Linux网络配置和管理的各个方面,从基础配置到高级优化,提供了丰富的实战示例和故障排查方法。建议读者根据实际环境需求,选择合适的配置方案,并建立完善的网络管理流程。

本站由 提供部署服务