80 端口(HTTP)連接數分析是 Web 服務器性能調優、DDoS 攻擊識別、容量規劃的核心指標。本文系統講解 netstat/ss 統計連接數的多維方法、TCP 狀態解讀、攻擊特征識別與自動化監控腳本。
# netstat 寫法
netstat -nat | grep -c ':80 '
# ss 寫法(更快、支持過濾器)
ss -tan 'sport = :80' | wc -l
# 或
ss -tan sport = :80 | grep -c ESTAB
netstat -nat | grep ':80 ' | grep -c ESTABLISHED
ss -tan state established sport = :80 | wc -l
# 一鍵統計所有 TCP 狀態計數
netstat -nat | awk '/^tcp/ {++S[$NF]} END {for (a in S) print a, S[a]}'
# ss 版本(推薦,速度快 10 倍+)
ss -tan state all | awk 'NR>1 {print $1}' | sort | uniq -c | sort -rn
# 典型輸出:
# ESTABLISHED 1597
# TIME_WAIT 1057
# FIN_WAIT2 504
# SYN_RECV 30
# FIN_WAIT1 51
# LAST_ACK 5
| 狀態 | 含義 | 正常範圍 | 異常信號 |
|---|---|---|---|
| ESTABLISHED | 正常數據傳輸 | 業務並發數 | 持續增長超過 MaxClients → 連接洩漏/慢查詢阻塞 |
| SYN_RECV | 收到 SYN,等待 ACK(半連接隊列) | < 100 | 大量 SYN_RECV → SYN Flood 攻擊 |
| TIME_WAIT | 主動關閉方等待 2MSL | 短連接業務較多 | 極多 TIME_WAIT → 端口耗盡,需開啟 tcp_tw_reuse |
| FIN_WAIT1/2 | 關閉握手中間狀態 | 極少 | 大量堆積 → 對端不回 FIN/ACK,檢查應用關閉邏輯 |
| CLOSE_WAIT | 被動關閉方待應用調用 close() | 0 | > 0 即異常 → 應用未調用 close(),代碼洩漏 |
| LAST_ACK | 被動關閉發送 FIN 等待 ACK | 極少 | 堆積 → 網絡丟包/對端異常 |
# 統計 ESTABLISHED 狀態下客戶端 IP 連接數(Top 20)
ss -tan state established sport = :80 | \
awk 'NR>1 {split($5,a,":"); print a[1]}' | \
sort | uniq -c | sort -rn | head -20
# 統計 SYN_RECV 來源(SYN Flood 來源)
ss -tan state syn-recv sport = :80 | \
awk 'NR>1 {split($5,a,":"); print a[1]}' | \
sort | uniq -c | sort -rn | head -20
# Apache prefork 模式:進程數 ≈ 並發處理能力
ps -ef | grep httpd | grep -v grep | wc -l
# 或
pgrep -c httpd
# Nginx worker 進程數
ps -ef | grep 'nginx: worker' | grep -v grep | wc -l
# 對比:連接數 vs 進程數
# 若 ESTABLISHED >> worker_processes * worker_connections → 積壓/慢請求
#!/bin/bash
# /usr/local/bin/monitor_port80.sh
# 每分鐘運行,異常告警
PORT=80
THRESHOLD_ESTAB=2000 # ESTABLISHED 閾值
THRESHOLD_SYN_RECV=100 # SYN_RECV 閾值
THRESHOLD_TIME_WAIT=5000 # TIME_WAIT 閾值
ALERT_EMAIL="ops@example.com"
# 獲取統計
ESTAB=$(ss -tan state established sport = :$PORT | wc -l)
SYN_RECV=$(ss -tan state syn-recv sport = :$PORT | wc -l)
TIME_WAIT=$(ss -tan state time-wait sport = :$PORT | wc -l)
TOTAL=$(ss -tan sport = :$PORT | wc -l)
# 構建報告
REPORT="Port $PORT Stats: ESTAB=$ESTAB SYN_RECV=$SYN_RECV TIME_WAIT=$TIME_WAIT TOTAL=$TOTAL"
# 告警判斷
ALERT=0
if [ "$ESTAB" -gt "$THRESHOLD_ESTAB" ]; then
REPORT="$REPORT | ALERT: High ESTABLISHED"
ALERT=1
fi
if [ "$SYN_RECV" -gt "$THRESHOLD_SYN_RECV" ]; then
REPORT="$REPORT | ALERT: SYN Flood suspected"
ALERT=1
fi
if [ "$TIME_WAIT" -gt "$THRESHOLD_TIME_WAIT" ]; then
REPORT="$REPORT | ALERT: TIME_WAIT exhaustion risk"
ALERT=1
fi
echo "$REPORT" | logger -t port80_monitor
if [ $ALERT -eq 1 ]; then
echo "$REPORT" | mail -s "[ALERT] Port 80 Anomaly on $(hostname)" "$ALERT_EMAIL"
fi
# 記錄歷史(用於趨勢圖)
echo "$(date +%s) $ESTAB $SYN_RECV $TIME_WAIT $TOTAL" >> /var/log/port80_stats.log
chmod +x /usr/local/bin/monitor_port80.sh
echo "* * * * * root /usr/local/bin/monitor_port80.sh" >> /etc/crontab
# /etc/sysctl.d/99-tcp-tuning.conf
# 連接隊列
net.core.somaxconn = 65535
net.ipv4.tcp_max_syn_backlog = 65535
net.core.netdev_max_backlog = 65535
# TIME_WAIT 復用(需內核 4.1+,NAT 環境慎用 tcp_tw_recycle)
net.ipv4.tcp_tw_reuse = 1
net.ipv4.tcp_fin_timeout = 15
# 緩衝區優化
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_keepalive_time = 600
net.ipv4.tcp_keepalive_intvl = 30
net.ipv4.tcp_keepalive_probes = 3
sysctl -p /etc/sysctl.d/99-tcp-tuning.conf
watch -n 1 'ss -tan state all | awk '"'"'NR>1 {print $1}'"'"' | sort | uniq -c | sort -rn'node_netstat_Tcp_CurrEstab、node_sockstat_TCP_tw 等指標繪製儀表盤tcplife、tcpconnect)掌握 80 端口連接數多維統計與 TCP 狀態判讀,是 Linux 運維從「會用命令」進階到「能做容量規劃、能抓攻擊」的關鍵門檻。建議將監控腳本納入基礎設施監控體系,歷史數據留存至少 30 天,支撐趨勢分析與異常基線建模。