网络配置和管理是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 ip addr show ip addr show eth0 ethtool eth0 lspci | grep -i network lsusb | grep -i network
1.2 网络配置文件位置 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 /etc/sysconfig/network-scripts/ifcfg-eth0 /etc/sysconfig/network /etc/resolv.conf /etc/hosts /etc/network/interfaces /etc/netplan/*.yaml /etc/systemd/network/ /etc/NetworkManager/ /etc/hosts /etc/resolv.conf /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 ifconfig route netstat arp ip ss ping traceroute nslookup/dig 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 addr add 192.168.1.100/24 dev eth0 ip addr del 192.168.1.100/24 dev eth0 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 addr flush dev eth0 ip link set eth0 mtu 1500 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 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 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 cat > /etc/sysconfig/network-scripts/ifcfg-eth0 << 'EOF' TYPE=Ethernet BOOTPROTO=dhcp DEFROUTE=yes NAME=eth0 DEVICE=eth0 ONBOOT=yes EOF cat > /etc/netplan/01-dhcp.yaml << 'EOF' network: version: 2 ethernets: eth0: dhcp4: true dhcp6: true EOF cat >> /etc/network/interfaces << 'EOF' auto eth0 iface eth0 inet dhcp EOF dhclient eth0 dhclient -r eth0 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 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 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/interfacescat > /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_tablesip 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 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 chattr +i /etc/resolv.conf systemctl enable systemd-resolved systemctl start systemd-resolved ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.confcat > /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 google.com nslookup google.com 8.8.8.8 nslookup -type =MX google.com nslookup -type =NS google.com dig google.com dig @8.8.8.8 google.com dig google.com MX dig google.com NS dig +trace google.com dig +short google.com dig -x 8.8.8.8 host google.com host -t MX google.com host 8.8.8.8 for i in {1..10}; do time dig +short google.com > /dev/null done systemctl flush-dns sudo systemd-resolve --flush-cachesresolvectl 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 yum install dnsmasq apt install dnsmasq cat > /etc/dnsmasq.conf << 'EOF' interface=eth0 server=8.8.8.8 server=8.8.4.4 address=/local.example.com/192.168.1.100 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 log-queries log-dhcp cache-size=1000 EOF systemctl enable dnsmasq systemctl start dnsmasq 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 -L -n -v iptables -t nat -L -n -v iptables -t mangle -L -n -v 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 -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' 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 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 iptables -A INPUT -p tcp --dport 80 -j ACCEPT iptables -A INPUT -p tcp --dport 443 -j ACCEPT iptables -A INPUT -p udp --dport 53 -j ACCEPT iptables -A INPUT -p tcp --dport 53 -j ACCEPT iptables -A INPUT -p udp --dport 123 -j ACCEPT 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 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 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 -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 -i eth0 iftop -n iftop -P nethogs nethogs eth0 vnstat vnstat -i eth0 vnstat -d vnstat -m vnstat -h sar -n DEV 1 10 sar -n EDEV 1 10 sar -n TCP 1 10 nload nload eth0 cat > network_monitor.sh << 'EOF' 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 -s iperf3 -c server_ip iperf3 -c server_ip -t 30 iperf3 -c server_ip -P 4 iperf3 -c server_ip -u ping -c 10 8.8.8.8 ping -i 0.1 -c 100 8.8.8.8 ping -s 1472 8.8.8.8 mtr google.com mtr --report --report-cycles 100 google.com tcptraceroute google.com 80 cat > latency_test.sh << 'EOF' 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 ip link show ethtool eth0 dmesg | grep -i network ip addr show ip route show cat /etc/resolv.confping 127.0.0.1 ping $(ip route | grep default | awk '{print $3}' ) nslookup google.com dig google.com ping 8.8.8.8 ping google.com telnet google.com 80 nc -zv google.com 80 iptables -L -n firewall-cmd --list-all 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' 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 " echo "1. Network Interfaces:" >> "$REPORT_FILE " ip link show >> "$REPORT_FILE " 2>&1 echo >> "$REPORT_FILE " echo "2. IP Addresses:" >> "$REPORT_FILE " ip addr show >> "$REPORT_FILE " 2>&1 echo >> "$REPORT_FILE " echo "3. Routing Table:" >> "$REPORT_FILE " ip route show >> "$REPORT_FILE " 2>&1 echo >> "$REPORT_FILE " echo "4. DNS Configuration:" >> "$REPORT_FILE " cat /etc/resolv.conf >> "$REPORT_FILE " 2>&1echo >> "$REPORT_FILE " echo "5. Network Connections:" >> "$REPORT_FILE " ss -tuln >> "$REPORT_FILE " 2>&1 echo >> "$REPORT_FILE " 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 " 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 " echo "8. Network Statistics:" >> "$REPORT_FILE " cat /proc/net/dev >> "$REPORT_FILE " 2>&1echo >> "$REPORT_FILE " 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 nmcli connection show nmcli connection reload nmcli connection up eth0 systemctl restart NetworkManager nmcli general reload systemctl restart systemd-resolved resolvectl flush-caches echo "nameserver 8.8.8.8" > /etc/resolv.confip route add default via 192.168.1.1 ip route del 0.0.0.0/0 systemctl stop firewalld iptables -F echo 'net.core.rmem_max = 16777216' >> /etc/sysctl.confecho 'net.core.wmem_max = 16777216' >> /etc/sysctl.confecho 'net.ipv4.tcp_rmem = 4096 87380 16777216' >> /etc/sysctl.confecho 'net.ipv4.tcp_wmem = 4096 65536 16777216' >> /etc/sysctl.confsysctl -p cat > reset_network.sh << 'EOF' INTERFACE="$1 " if [ -z "$INTERFACE " ]; then echo "Usage: $0 <interface>" exit 1 fi echo "Resetting network interface $INTERFACE ..." ip link set "$INTERFACE " down sleep 2ip addr flush dev "$INTERFACE " ip link set "$INTERFACE " up sleep 2if [ -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 systemctl disable telnet systemctl disable rsh systemctl disable rlogin 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 cat > /etc/sysctl.d/99-network-security.conf << 'EOF' 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 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 net.ipv4.icmp_echo_ignore_all = 0 net.ipv4.icmp_echo_ignore_broadcasts = 1 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' net.core.rmem_default = 262144 net.core.rmem_max = 16777216 net.core.wmem_default = 262144 net.core.wmem_max = 16777216 net.ipv4.tcp_window_scaling = 1 net.ipv4.tcp_congestion_control = bbr net.ipv4.tcp_fastopen = 3 net.core.netdev_max_backlog = 5000 net.core.somaxconn = 65535 net.ipv4.tcp_max_syn_backlog = 65535 net.ipv4.tcp_keepalive_time = 600 net.ipv4.tcp_keepalive_intvl = 60 net.ipv4.tcp_keepalive_probes = 3 net.ipv4.tcp_tw_reuse = 1 net.ipv4.tcp_fin_timeout = 30 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' INTERFACE="eth0" CPU_COUNT=$(nproc ) IRQS=$(grep "$INTERFACE " /proc/interrupts | awk -F: '{print $1}' | tr -d ' ' ) 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 网络管理最佳实践
配置管理 :
使用版本控制管理网络配置文件
建立配置变更审批流程
定期备份网络配置
文档化所有网络变更
监控和告警 :
建立网络性能基线
配置关键指标告警
定期检查网络健康状态
建立故障响应流程
安全防护 :
实施网络分段
配置适当的防火墙规则
定期更新安全配置
监控异常网络活动
9.2 故障预防措施
定期维护 :
更新网络驱动程序
检查网络硬件状态
清理网络配置
优化网络参数
容量规划 :
监控网络使用率
预测流量增长
规划网络扩容
优化网络架构
灾难恢复 :
建立网络配置备份
制定故障恢复计划
定期演练恢复流程
准备应急网络方案
9.3 工具推荐
监控工具 :
Nagios/Zabbix - 网络监控
MRTG/Cacti - 流量监控
Wireshark - 包分析
nmap - 网络扫描
性能工具 :
iperf3 - 带宽测试
mtr - 网络诊断
tcpdump - 包捕获
netstat/ss - 连接监控
管理工具 :
Ansible - 配置管理
Terraform - 基础设施即代码
Git - 配置版本控制
IPAM - IP地址管理
通过掌握这些Linux网络配置和管理技能,你将能够有效地管理复杂的网络环境,确保网络的稳定性、安全性和高性能。记住,网络管理是一个持续的过程,需要不断学习新技术和最佳实践。
本文全面介绍了Linux网络配置和管理的各个方面,从基础配置到高级优化,提供了丰富的实战示例和故障排查方法。建议读者根据实际环境需求,选择合适的配置方案,并建立完善的网络管理流程。