Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> 關於 Linux

Linux netstat 查看 80 埠連線數完整指令集:總連線、ESTABLISHED、IP 統計與 Apache 並發分析

編輯:關於 Linux

netstat 配合 grep/awk 可快速統計 80 埠各狀態連線數,識別攻擊源 IP、分析 Apache 並發負載。本文提供六大實用指令管線:總連線數、ESTABLISHED 連線、SYN 洪水偵測、IP 連線數排序、Apache 進程數對比、server-status 自動化監控。

netstat 80埠統計示意圖

核心統計指令

# 1. 80 埠總連線數 (含所有狀態)
netstat -nat | grep -i ":80" | wc -l

# 2. ESTABLISHED 已建立連線數
netstat -nat | grep ":80" | grep ESTABLISHED | wc -l

# 3. SYN_RECV 連線數 (偵測 SYN Flood)
netstat -nat | grep ":80" | grep SYN_RECV | wc -l

# 4. TIME_WAIT 連線數 (大量短連線特徵)
netstat -nat | grep ":80" | grep TIME_WAIT | wc -l

IP 來源統計與封鎖

# 統計 ESTABLISHED 狀態下各 IP 連線數,降序排列
netstat -nat | grep ESTABLISHED | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn

# 統計 SYN_RECV 狀態下各 IP (潛在攻擊源)
netstat -nat | grep SYN_RECV | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn

# 封鎖前 5 名攻擊 IP (範例)
netstat -nat | grep SYN_RECV | awk '{print $5}' | cut -d: -f1 | sort | uniq -c | sort -rn | head -5 | while read count ip; do iptables -A INPUT -s $ip -j DROP; done

Apache 並發分析

# Apache worker 進程數 (每請求一進程,prefork MPM)
ps aux | grep httpd | grep -v grep | wc -l

# 對比 httpd.conf MaxClients 設定
grep MaxClients /etc/httpd/conf/httpd.conf

# 啟用 server-status 即時監控 (需 mod_status)
# httpd.conf: ExtendedStatus On, Location /server-status SetHandler server-status
curl -s https://localhost/server-status?auto
# 輸出: BusyWorkers, IdleWorkers, ReqPerSec, BytesPerSec 等

指令解析對照

指令片段作用
netstat -nat顯示所有 TCP 連線 (數值格式 IP/埠)
grep -i ":80"過濾 80 埠相關 (忽略大小寫)
awk '{print $5}'取第 5 欄 (遠端 IP:埠)
cut -d: -f1以冒號分隔取 IP 部分
sort | uniq -c | sort -rn計數去重並降序排列

持續監控建議

  • 將上述指令封裝為腳本,配合 watch -n 5 或 cron 定期記錄
  • 整合至 Zabbix/Prometheus 監控系統,設定連線數閾值告警
  • Apache 事件 MPM (event) 下進程數不等於並發數,需看 AsyncRequestWorkerFactor 計算
Copyright © Windows教程網 All Rights Reserved