萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 作業系統 >  >> Linux 教學

Linux iptables 報錯 "Protocol wrong type for socket" 解決:升級 iptables 版本

在 CentOS 6.4 上使用 connlimit 模組限制連線數時,出現 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 管理)

# 1. 移除舊版
rpm -e --nodeps iptables-1.4.7-9.el6.x86_64
rpm -e --nodeps iptables-ipv6-1.4.7-9.el6.x86_64

# 2. 安裝編譯工具
yum groupinstall 'Development Tools'

# 3. 下載源碼
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

# 4. 配置編譯
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

# 5. 編譯安裝
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

注意事項

  • 升級後無法使用 yum update iptables,後續更新需重新編譯。
  • 生產環境建議先在測試機驗證,確認模組相容性。
  • CentOS 7+ 已內建較新版本,通常不會遇到此問題。
copyright © 萬盛學電腦網 all rights reserved