在 CentOS 6 上嘗試加入 connlimit 模組限制單 IP 連接數時,報錯 iptables: Protocol wrong type for socket.。根因是系統預設 iptables 1.4.7 版本過舊,核心模組不相容。本文記錄從源碼編譯升級至 1.4.19 的完整流程。
# iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 100 -j REJECT
iptables: Protocol wrong type for socket.
# 確認版本
# iptables -V
iptables v1.4.7
# rpm -qa | grep iptables
iptables-1.4.7-9.el6.x86_64
iptables-ipv6-1.4.7-9.el6.x86_64
警告: 升級後無法再用 yum 管理 iptables,請確認環境允許。
rpm -e --nodeps iptables-1.4.7-9.el6.x86_64
rpm -e --nodeps iptables-ipv6-1.4.7-9.el6.x86_64
yum groupinstall 'Development Tools'
wget https://www.netfilter.org/projects/iptables/files/iptables-1.4.19.tar.bz2
tar jxf iptables-1.4.19.tar.bz2
cd iptables-1.4.19
LDFLAGS="-L$PWD/libiptc/.libs" ./configure \
--prefix=/usr \
--exec-prefix= \
--bindir=/usr/bin \
--with-xtlibdir=/lib/xtables \
--with-pkgconfigdir=/usr/lib/pkgconfig \
--enable-libipq \
--enable-devel
make && make install
# iptables -V
iptables v1.4.19
# 再次執行原失敗規則
# iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 100 -j REJECT
# iptables -L
Chain INPUT (policy ACCEPT)
REJECT tcp -- anywhere anywhere tcp dpt:http flags:FIN,SYN,RST,ACK/SYN #conn src/32 > 100 reject-with icmp-port-unreachable
規則生效,connlimit 模組正常工作。
recent 模組限制連接頻率(不需 connlimit):iptables -A INPUT -p tcp --dport 80 -m recent --name http_limit --setiptables -A INPUT -p tcp --dport 80 -m recent --name http_limit --update --seconds 60 --hitcount 100 -j DROP/usr/local/src/iptables-build.log 便於日後維護;nftables(Linux 3.13+),為未來標準。版本相容性是防火牆規則生效的基石,遇到模組報錯優先查核版本矩陣。

CentOS 安裝 VNC 後報錯 "Could not start Xvnc" 解決方案
Linux 命令列環境下 Git over HTTPS 認證設定完整指南:.netrc + GPG 加密 + credential helper 實戰
chgrp 變更檔案群組:語法、遞迴、參考檔、符號連結處理、超級用戶權限、與 chown 差異
Linux cp 複製目錄出現 omitting directory 錯誤?加上 -r 遞迴參數即可解決
Linux bunzip2 指令使用教學:.bz2 壓縮檔解壓縮參數完整說明
Linux 系統如何使用 QQ?Pidgin + WebQQ 協議實現 Linux 微信/QQ 互通
Linux 檔案管理核心指令實戰:touch 建立/更新時間、mkdir 建立目錄、cp/mv/rm 複製移動刪除、ln 連結