運維必備:大廠Linux網路管理技術實戰指南
一、引言
二、網路基礎架構與規劃
2.1 大廠網路架構設計
┌─────────────────────────────────────────────────────────┐
│ 核心層 (Core Layer) │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Core-1 │──────────────│ Core-2 │ │
│ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────┐
│ 匯聚層 (Aggregation Layer) │
│ ┌─────────────┐ ┌─────────────┐ │
│ │ Agg-1 │──────────────│ Agg-2 │ │
│ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────┘
│
┌─────────────────────────────────────────────────────────┐
│ 接入層 (Access Layer) │
│ ┌─────────────┐ ┌─────────────┐ ┌─────────────┐ │
│ │ TOR-1 │ │ TOR-2 │ │ TOR-3 │ │
│ └─────────────┘ └─────────────┘ └─────────────┘ │
└─────────────────────────────────────────────────────────┘
# 管理網路
VLAN 100: 192.168.100.0/24 # 伺服器管理介面
VLAN 101: 192.168.101.0/24 # 網路裝置管理
# 業務網路
VLAN 200: 10.10.200.0/24 # Web前端服務
VLAN 201: 10.10.201.0/24 # 應用服務層
VLAN 202: 10.10.202.0/24 # 資料庫層
# 儲存網路
VLAN 300: 10.10.300.0/24 # 分散式儲存
VLAN 301: 10.10.301.0/24 # 備份網路
2.2 網路介面配置與管理
# /etc/sysconfig/network-scripts/ifcfg-eth0
TYPE=Ethernet
BOOTPROTO=static
DEFROUTE=yes
PEERDNS=yes
PEERROUTES=yes
IPV4_FAILURE_FATAL=no
IPV6INIT=yes
IPV6_AUTOCONF=yes
IPV6_DEFROUTE=yes
IPV6_PEERDNS=yes
IPV6_PEERROUTES=yes
IPV6_FAILURE_FATAL=no
NAME=eth0
UUID=12345678-1234-1234-1234-123456789abc
DEVICE=eth0
ONBOOT=yes
IPADDR=10.10.200.100
NETMASK=255.255.255.0
GATEWAY=10.10.200.1
DNS1=8.8.8.8
DNS2=8.8.4.4
# /etc/netplan/00-installer-config.yaml
network:
version:2
renderer:networkd
ethernets:
eth0:
addresses:
-10.10.200.100/24
gateway4:10.10.200.1
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
eth1:
addresses:
-10.10.201.100/24
# /etc/sysconfig/network-scripts/ifcfg-bond0
DEVICE=bond0
TYPE=Bond
BONDING_MASTER=yes
BOOTPROTO=static
ONBOOT=yes
IPADDR=10.10.200.100
NETMASK=255.255.255.0
GATEWAY=10.10.200.1
BONDING_OPTS="mode=802.3ad miimon=100 lacp_rate=fast"
# /etc/sysconfig/network-scripts/ifcfg-eth0
DEVICE=eth0
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
# /etc/sysconfig/network-scripts/ifcfg-eth1
DEVICE=eth1
TYPE=Ethernet
BOOTPROTO=none
ONBOOT=yes
MASTER=bond0
SLAVE=yes
三、網路效能監控與調優
3.1 網路效能監控工具
#!/bin/bash
# 網路效能監控指令碼
INTERFACE="eth0"
INTERVAL=5
echo"網路介面: $INTERFACE"
echo"監控間隔: $INTERVAL 秒"
echo"時間戳 接收(MB/s) 傳送(MB/s) 丟包率(%)"
echo"=================================================="
whiletrue; do
# 獲取網路統計資訊
RX1=$(cat /sys/class/net/$INTERFACE/statistics/rx_bytes)
TX1=$(cat /sys/class/net/$INTERFACE/statistics/tx_bytes)
RX_DROPPED1=$(cat /sys/class/net/$INTERFACE/statistics/rx_dropped)
TX_DROPPED1=$(cat /sys/class/net/$INTERFACE/statistics/tx_dropped)
RX_PACKETS1=$(cat /sys/class/net/$INTERFACE/statistics/rx_packets)
TX_PACKETS1=$(cat /sys/class/net/$INTERFACE/statistics/tx_packets)
sleep$INTERVAL
RX2=$(cat /sys/class/net/$INTERFACE/statistics/rx_bytes)
TX2=$(cat /sys/class/net/$INTERFACE/statistics/tx_bytes)
RX_DROPPED2=$(cat /sys/class/net/$INTERFACE/statistics/rx_dropped)
TX_DROPPED2=$(cat /sys/class/net/$INTERFACE/statistics/tx_dropped)
RX_PACKETS2=$(cat /sys/class/net/$INTERFACE/statistics/rx_packets)
TX_PACKETS2=$(cat /sys/class/net/$INTERFACE/statistics/tx_packets)
# 計算速率
RX_RATE=$(echo"scale=2; ($RX2 - $RX1) / 1024 / 1024 / $INTERVAL" | bc)
TX_RATE=$(echo"scale=2; ($TX2 - $TX1) / 1024 / 1024 / $INTERVAL" | bc)
# 計算丟包率
TOTAL_PACKETS=$((RX_PACKETS2 - RX_PACKETS1 + TX_PACKETS2 - TX_PACKETS1))
DROPPED_PACKETS=$((RX_DROPPED2 - RX_DROPPED1 + TX_DROPPED2 - TX_DROPPED1))
if [ $TOTAL_PACKETS -gt 0 ]; then
DROP_RATE=$(echo"scale=2; $DROPPED_PACKETS * 100 / $TOTAL_PACKETS" | bc)
else
DROP_RATE=0
fi
printf"%-15s %10s %10s %10s\n" \
"$(date '+%H:%M:%S')" \
"$RX_RATE" \
"$TX_RATE" \
"$DROP_RATE"
done
# iftop - 即時網路流量監控
iftop -i eth0 -P -n
# nethogs - 按程序顯示網路使用情況
nethogs eth0
# ss - 檢視網路連線狀態
ss -tuln | grep :80
ss -i # 顯示詳細連線資訊
# nload - 即時網路流量圖形化顯示
nload -m eth0
# tcpdump - 網路包捕獲分析
tcpdump -i eth0 -n -c 100 'port 80 or port 443'
3.2 網路引數調優
# /etc/sysctl.conf
# TCP緩衝區最佳化
net.core.rmem_default = 262144
net.core.rmem_max = 16777216
net.core.wmem_default = 262144
net.core.wmem_max = 16777216
net.ipv4.tcp_rmem = 4096 87380 16777216
net.ipv4.tcp_wmem = 4096 65536 16777216
# TCP連線最佳化
net.ipv4.tcp_fin_timeout = 30
net.ipv4.tcp_keepalive_time = 1200
net.ipv4.tcp_max_syn_backlog = 8192
net.ipv4.tcp_max_tw_buckets = 5000
net.ipv4.tcp_syncookies = 1
net.ipv4.tcp_tw_recycle = 1
net.ipv4.tcp_tw_reuse = 1
# 網路佇列最佳化
net.core.netdev_max_backlog = 5000
net.core.netdev_budget = 600
# 應用最佳化
sysctl -p
#!/bin/bash
# 網路介面多佇列配置
INTERFACE="eth0"
CPU_CORES=$(nproc)
# 啟用多佇列
ethtool -L $INTERFACE combined $CPU_CORES
# 設定中斷親和性
for ((i=0; i<$CPU_CORES; i++)); do
IRQ=$(grep "$INTERFACE-TxRx-$i" /proc/interrupts | awk '{print $1}' | tr -d ':')
if [ -n "$IRQ" ]; then
echo $((1 << i)) > /proc/irq/$IRQ/smp_affinity
fi
done
# 最佳化網路介面引數
ethtool -G $INTERFACE rx 4096 tx 4096
ethtool -C $INTERFACE adaptive-rx on adaptive-tx on
四、網路安全與防護
4.1 iptables防火牆配置
#!/bin/bash
# 企業級iptables配置指令碼
# 清空現有規則
iptables -F
iptables -X
iptables -Z
# 設定預設策略
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訪問控制(僅允許特定IP)
iptables -A INPUT -p tcp --dport 22 -s 192.168.1.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -s 10.0.0.0/8 -j ACCEPT
# Web服務埠
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
# 資料庫訪問控制
iptables -A INPUT -p tcp --dport 3306 -s 10.10.201.0/24 -j ACCEPT
iptables -A INPUT -p tcp --dport 5432 -s 10.10.201.0/24 -j ACCEPT
# 防止SYN洪水攻擊
iptables -A INPUT -p tcp --syn -m limit --limit 1/s --limit-burst 3 -j ACCEPT
iptables -A INPUT -p tcp --syn -j DROP
# 防止埠掃描
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
# 限制ICMP
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s -j ACCEPT
# 儲存規則
iptables-save > /etc/iptables/rules.v4
4.2 網路入侵檢測
#!/bin/bash
# 網路入侵檢測指令碼
LOG_FILE="/var/log/secure"
ALERT_EMAIL="[email protected]"
THRESHOLD=10
# 檢測SSH暴力破解
check_ssh_brute_force() {
local failed_attempts=$(grep "Failed password"$LOG_FILE | \
grep "$(date '+%b %d')" | \
awk '{print $11}' | sort | uniq -c | \
awk -v threshold=$THRESHOLD'$1 > threshold {print $2, $1}')
if [ -n "$failed_attempts" ]; then
echo"SSH暴力破解檢測到:"
echo"$failed_attempts"
# 自動封禁IP
echo"$failed_attempts" | whileread ip count; do
iptables -A INPUT -s $ip -j DROP
echo"已封禁IP: $ip (失敗次數: $count)"
done
fi
}
# 檢測埠掃描
check_port_scan() {
local scan_attempts=$(netstat -an | grep SYN_RECV | \
awk '{print $5}' | cut -d: -f1 | sort | uniq -c | \
awk -v threshold=50 '$1 > threshold {print $2, $1}')
if [ -n "$scan_attempts" ]; then
echo"埠掃描檢測到:"
echo"$scan_attempts"
fi
}
# 執行檢測
check_ssh_brute_force
check_port_scan
五、高可用網路架構
5.1 負載均衡配置
# /etc/haproxy/haproxy.cfg
global
daemon
maxconn 4096
user haproxy
group haproxy
defaults
mode http
timeout connect 5000ms
timeout client 50000ms
timeout server 50000ms
option httplog
option dontlognull
option redispatch
retries 3
frontend web_frontend
bind *:80
bind *:443 ssl crt /etc/ssl/certs/server.pem
redirect scheme https if !{ ssl_fc }
default_backend web_servers
backend web_servers
balance roundrobin
option httpchk GET /health
server web1 10.10.200.10:80 check
server web2 10.10.200.11:80 check
server web3 10.10.200.12:80 check
listen stats
bind *:8080
stats enable
stats uri /stats
stats refresh 30s
# /etc/keepalived/keepalived.conf
vrrp_script chk_haproxy {
script "/bin/curl -f http://localhost:80/health || exit 1"
interval 2
weight -2
fall 3
rise 2
}
vrrp_instance VI_1 {
state MASTER
interface eth0
virtual_router_id 51
priority 100
advert_int 1
authentication {
auth_type PASS
auth_pass mypassword
}
virtual_ipaddress {
10.10.200.100/24
}
track_script {
chk_haproxy
}
}
5.2 網路故障切換
#!/bin/bash
# 網路故障自動切換指令碼
PRIMARY_GATEWAY="10.10.200.1"
BACKUP_GATEWAY="10.10.200.2"
TEST_HOST="8.8.8.8"
INTERFACE="eth0"
check_connectivity() {
local gateway=$1
ping -c 3 -W 2 $gateway > /dev/null 2>&1
return $?
}
switch_gateway() {
local new_gateway=$1
ip route del default
ip route add default via $new_gateway dev $INTERFACE
echo"已切換到閘道器: $new_gateway"
}
# 主迴圈
whiletrue; do
current_gateway=$(ip route show default | awk '{print $3}')
if [ "$current_gateway" = "$PRIMARY_GATEWAY" ]; then
if ! check_connectivity $PRIMARY_GATEWAY; then
echo"主閘道器故障,切換到備用閘道器"
switch_gateway $BACKUP_GATEWAY
fi
else
if check_connectivity $PRIMARY_GATEWAY; then
echo"主閘道器恢復,切換回主閘道器"
switch_gateway $PRIMARY_GATEWAY
fi
fi
sleep 30
done
六、網路故障排查與診斷
6.1 網路連通性診斷
#!/bin/bash
# 網路故障診斷指令碼
TARGET_HOST="$1"
TARGET_PORT="$2"
if [ -z "$TARGET_HOST" ]; then
echo"用法: $0 <目標主機> [埠]"
exit 1
fi
echo"=== 網路診斷報告 ==="
echo"目標主機: $TARGET_HOST"
echo"目標埠: ${TARGET_PORT:-N/A}"
echo"診斷時間: $(date)"
echo
# 1. 基礎連通性測試
echo"1. PING測試:"
if ping -c 4 $TARGET_HOST > /tmp/ping_result 2>&1; then
echo" ✓ PING成功"
grep "rtt" /tmp/ping_result
else
echo" ✗ PING失敗"
cat /tmp/ping_result
fi
echo
# 2. 路由跟蹤
echo"2. 路由跟蹤:"
traceroute $TARGET_HOST 2>/dev/null | head -10
echo
# 3. DNS解析測試
echo"3. DNS解析:"
if nslookup $TARGET_HOST > /tmp/dns_result 2>&1; then
echo" ✓ DNS解析成功"
grep "Address" /tmp/dns_result | tail -1
else
echo" ✗ DNS解析失敗"
fi
echo
# 4. 埠連通性測試
if [ -n "$TARGET_PORT" ]; then
echo"4. 埠連通性:"
if nc -zv $TARGET_HOST$TARGET_PORT 2>&1 | grep -q "succeeded"; then
echo" ✓ 埠 $TARGET_PORT 開放"
else
echo" ✗ 埠 $TARGET_PORT 不可達"
fi
echo
fi
# 5. 網路介面狀態
echo"5. 本地網路介面狀態:"
ip addr show | grep -E "inet|state"
echo
# 6. 路由表
echo"6. 路由表:"
ip route show
echo
# 7. 防火牆狀態
echo"7. 防火牆狀態:"
iptables -L -n | head -20
6.2 網路效能分析
#!/bin/bash
# 網路效能測試指令碼
SERVER_IP="$1"
TEST_DURATION=30
if [ -z "$SERVER_IP" ]; then
echo"用法: $0 <伺服器IP>"
exit 1
fi
echo"=== 網路效能測試 ==="
echo"伺服器: $SERVER_IP"
echo"測試時長: $TEST_DURATION 秒"
echo
# TCP頻寬測試
echo"1. TCP頻寬測試:"
ifcommand -v iperf3 &> /dev/null; then
iperf3 -c $SERVER_IP -t $TEST_DURATION
else
echo" iperf3 未安裝,跳過頻寬測試"
fi
echo
# 延遲測試
echo"2. 網路延遲測試:"
ping -c 10 $SERVER_IP | tail -1
echo
# 併發連線測試
echo"3. 併發連線測試:"
for i in {1..10}; do
(time nc -zv $SERVER_IP 80 2>&1) 2>&1 | grep real &
done
wait
echo
# 丟包率測試
echo"4. 丟包率測試:"
ping -c 100 $SERVER_IP | grep "packet loss"
七、容器網路管理
7.1 Docker網路配置
#!/bin/bash
# Docker網路配置指令碼
# 建立自定義網路
docker network create --driver bridge \
--subnet=172.20.0.0/16 \
--ip-range=172.20.240.0/20 \
--gateway=172.20.0.1 \
custom_network
# 建立macvlan網路
docker network create -d macvlan \
--subnet=192.168.1.0/24 \
--gateway=192.168.1.1 \
-o parent=eth0 \
macvlan_network
# 容器網路監控
monitor_container_network() {
echo"容器網路使用情況:"
docker stats --no-stream --format "table {{.Container}}\t{{.NetIO}}"
echo -e "\n容器網路詳情:"
docker network ls
echo -e "\n網路介面統計:"
for container in $(docker ps -q); do
name=$(docker inspect --format='{{.Name}}'$container | sed 's/\///')
echo"容器: $name"
docker exec$containercat /proc/net/dev | grep -v "lo:" | tail -n +3
echo
done
}
monitor_container_network
7.2 Kubernetes網路管理
#!/bin/bash
# K8s網路故障排查指令碼
# 檢查Pod網路連通性
check_pod_connectivity() {
local pod_name=$1
local namespace=${2:-default}
echo"檢查Pod: $pod_name (namespace: $namespace)"
# 獲取Pod IP
pod_ip=$(kubectl get pod $pod_name -n $namespace -o jsonpath='{.status.podIP}')
echo"Pod IP: $pod_ip"
# 檢查Pod網路介面
kubectl exec$pod_name -n $namespace -- ip addr show
# 檢查Pod路由
kubectl exec$pod_name -n $namespace -- ip route show
# 測試DNS解析
kubectl exec$pod_name -n $namespace -- nslookup kubernetes.default.svc.cluster.local
}
# 檢查Service網路
check_service_network() {
local service_name=$1
local namespace=${2:-default}
echo"檢查Service: $service_name"
# 獲取Service資訊
kubectl get svc $service_name -n $namespace -o wide
# 檢查Endpoints
kubectl get endpoints $service_name -n $namespace
# 檢查Service的iptables規則
iptables -t nat -L | grep $service_name
}
# 網路策略檢查
check_network_policies() {
echo"當前網路策略:"
kubectl get networkpolicies --all-namespaces
echo -e "\n網路策略詳情:"
kubectl get networkpolicies --all-namespaces -o yaml
}
# 使用示例
# check_pod_connectivity "my-pod" "default"
# check_service_network "my-service" "default"
# check_network_policies
八、網路自動化管理
8.1 Ansible網路自動化
# network_config.yml
---
-name:網路配置自動化
hosts:servers
become:yes
vars:
network_interfaces:
-name:eth0
ip:"{{ ansible_default_ipv4.address }}"
netmask:"255.255.255.0"
gateway:"{{ ansible_default_ipv4.gateway }}"
-name:eth1
ip:"10.10.201.{{ ansible_host.split('.')[3] }}"
netmask:"255.255.255.0"
tasks:
-name:配置網路介面
template:
src:ifcfg-interface.j2
dest:"/etc/sysconfig/network-scripts/ifcfg-{{ item.name }}"
loop:"{{ network_interfaces }}"
notify:restartnetwork
-name:配置防火牆規則
iptables:
chain:INPUT
protocol:tcp
destination_port:"{{ item }}"
jump:ACCEPT
loop:
-22
-80
-443
-name:最佳化網路引數
sysctl:
name:"{{ item.name }}"
value:"{{ item.value }}"
state:present
reload:yes
loop:
- { name:"net.ipv4.tcp_fin_timeout", value:"30" }
- { name:"net.ipv4.tcp_keepalive_time", value:"1200" }
- { name:"net.core.rmem_max", value:"16777216" }
- { name:"net.core.wmem_max", value:"16777216" }
-name:安裝網路監控工具
package:
name:"{{ item }}"
state:present
loop:
-iftop
-nethogs
-tcpdump
-nmap
handlers:
-name:restartnetwork
service:
name:network
state:restarted
8.2 網路監控自動化
# prometheus.yml
global:
scrape_interval:15s
evaluation_interval:15s
rule_files:
-"network_rules.yml"
scrape_configs:
-job_name:'node-exporter'
static_configs:
-targets: ['localhost:9100']
scrape_interval:5s
metrics_path:/metrics
-job_name:'snmp-network'
static_configs:
-targets:
-192.168.1.1# 路由器
-192.168.1.2# 交換機
metrics_path:/snmp
params:
module: [if_mib]
relabel_configs:
-source_labels: [__address__]
target_label:__param_target
-source_labels: [__param_target]
target_label:instance
-target_label:__address__
replacement:127.0.0.1:9116# SNMP exporter
# network_rules.yml
groups:
-name:network_alerts
rules:
-alert:HighNetworkTraffic
expr:rate(node_network_receive_bytes_total[5m])>100000000
for:2m
labels:
severity:warning
annotations:
summary:"高網路流量告警"
description:"{{ $labels.instance }} 網路接收流量超過100MB/s"
-alert:NetworkInterfaceDown
expr:node_network_up==0
for:1m
labels:
severity:critical
annotations:
summary:"網路介面故障"
description:"{{ $labels.instance }} 網路介面 {{ $labels.device }} 已斷開"
-alert:HighPacketLoss
expr:rate(node_network_receive_drop_total[5m])>1000
for:2m
labels:
severity:warning
annotations:
summary:"網路丟包告警"
description:"{{ $labels.instance }} 網路丟包率過高"
九、網路安全最佳實踐
9.1 網路安全加固
#!/bin/bash
# 網路安全檢查指令碼
echo"=== 網路安全檢查報告 ==="
echo"檢查時間: $(date)"
echo
# 1. 開放埠檢查
echo"1. 開放埠檢查:"
netstat -tuln | grep LISTEN | whileread line; do
port=$(echo$line | awk '{print $4}' | cut -d: -f2)
protocol=$(echo$line | awk '{print $1}')
echo" 開放埠: $port ($protocol)"
done
echo
# 2. 防火牆狀態檢查
echo"2. 防火牆狀態:"
if systemctl is-active --quiet iptables; then
echo" ✓ iptables 服務執行中"
rule_count=$(iptables -L | grep -c "^Chain")
echo" 防火牆規則鏈數: $rule_count"
else
echo" ✗ iptables 服務未執行"
fi
echo
# 3. SSH配置檢查
echo"3. SSH安全配置檢查:"
ssh_config="/etc/ssh/sshd_config"
if [ -f "$ssh_config" ]; then
# 檢查關鍵配置項
root_login=$(grep "^PermitRootLogin"$ssh_config | awk '{print $2}')
password_auth=$(grep "^PasswordAuthentication"$ssh_config | awk '{print $2}')
port=$(grep "^Port"$ssh_config | awk '{print $2}')
echo" SSH埠: ${port:-22}"
echo" Root登入: ${root_login:-yes}"
echo" 密碼認證: ${password_auth:-yes}"
if [ "$root_login" = "no" ]; then
echo" ✓ 已停用Root登入"
else
echo" ✗ 建議停用Root登入"
fi
fi
echo
# 4. 網路連線檢查
echo"4. 可疑網路連線檢查:"
netstat -an | grep ESTABLISHED | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -nr | head -10
echo
# 5. 失敗登入嘗試檢查
echo"5. 失敗登入嘗試:"
if [ -f "/var/log/secure" ]; then
grep "Failed password" /var/log/secure | tail -5
else
echo" 無法訪問登入日誌"
fi
9.2 DDoS防護
#!/bin/bash
# DDoS防護指令碼
# 限制連線數
iptables -A INPUT -p tcp --dport 80 -m connlimit --connlimit-above 20 -j REJECT
# 限制新連線速率
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --set
iptables -A INPUT -p tcp --dport 80 -m state --state NEW -m recent --update --seconds 60 --hitcount 10 -j DROP
# SYN Flood防護
echo 1 > /proc/sys/net/ipv4/tcp_syncookies
echo 2048 > /proc/sys/net/ipv4/tcp_max_syn_backlog
echo 3 > /proc/sys/net/ipv4/tcp_synack_retries
# 配置連線跟蹤
echo 65536 > /proc/sys/net/netfilter/nf_conntrack_max
echo 300 > /proc/sys/net/netfilter/nf_conntrack_tcp_timeout_established
# 自動封禁攻擊IP
monitor_ddos() {
whiletrue; do
# 檢測異常連線
netstat -an | grep :80 | grep SYN_RECV | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | whileread count ip; do
if [ $count -gt 50 ]; then
echo"檢測到DDoS攻擊,封禁IP: $ip"
iptables -A INPUT -s $ip -j DROP
fi
done
sleep 10
done
}
# 啟動監控
monitor_ddos &
十、總結與展望
10.1 運維最佳實踐總結
-
1. 標準化配置管理:使用配置管理工具(Ansible、Puppet)實現網路配置的標準化和自動化。 -
2. 全面監控體系:建立從基礎設施到應用層的全棧網路監控,及時發現和處理網路問題。 -
3. 安全防護策略:實施多層次的網路安全防護,包括防火牆、入侵檢測、訪問控制等。 -
4. 故障快速響應:建立完善的故障處理流程和自動化故障切換機制。 -
5. 效能持續最佳化:定期進行網路效能評估和調優,確保網路基礎設施能夠支撐業務發展。
10.2 技術發展趨勢
-
• 軟體定義網路(SDN):透過軟體控制網路行為,提高網路的靈活性和可管理性。 -
• 網路功能虛擬化(NFV):將網路功能從專用硬體中解耦,實現更靈活的網路服務部署。 -
• 邊緣計算網路:隨著邊緣計算的發展,網路架構將更加分散式和智慧化。 -
• AI驅動的網路運維:利用人工智慧技術實現網路的自動化運維和智慧故障診斷。
結語








