歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux基礎 >> Linux教程 >> iptables使用ipt_connlimit限制連接數

iptables使用ipt_connlimit限制連接數

日期:2017/2/28 15:29:03   编辑:Linux教程

今天在回答一個網友關於使用iptables限制連接數的問題時,使用到了iptables的connlimit參數。這個參數對應的iptables模塊是ipt_connlimit。這個模塊對應linux5.X版本沒有靜態的編譯進內核,需要手動的執行modprobe ipt_connlimit命令把模板加入內核中去,然後在執行iptables命令。我測試的系統是CentOS 5.8 64位,iptables是1.3.5版本。

在沒有執行modprobe ipt_connlimit之前:

[root@vmhost ~]# lsmod |grep ipt
ipt_REJECT 38849 1
iptable_filter 36161 1
ip_tables 55457 1 iptable_filter
x_tables 50505 5 ipt_REJECT,xt_state,xt_tcpudp,ip_tables,ip6_tables
執行完modprobe ipt_connlimit之後:
[root@vmhost ~]# modprobe ipt_connlimit
[root@vmhost ~]# lsmod |grep ipt
ipt_REJECT 38849 1
iptable_filter 36161 1
ip_tables 55457 1 iptable_filter
x_tables 50505 6 xt_connlimit,ipt_REJECT,xt_state,xt_tcpudp,ip_tables,ip6_tables
可以看出執行後比執行前多了個xt_connlimit模塊,其實這個模塊和ipt_connlimit是同一個東西。
[root@vmhost ~]# modinfo xt_connlimit
filename: /lib/modules/2.6.18-274.el5/kernel/net/netfilter/xt_connlimit.ko
alias: ipt_connlimit
license: GPL
description: netfilter xt_connlimit match module
author: Jan Engelhardt <[email protected]>
srcversion: 808A466072E9117C99ACBEF
depends: x_tables,ip_conntrack
vermagic: 2.6.18-274.el5 SMP mod_unload gcc-4.1
module_sig: 883e3504e29417bec40b13698f675161125380098e920ae5f2d2b121c8dfad2928687811244b309f733a4e6727239b66982acb699ba57835f785f3a
然後再執行iptables測試,iptables -A INPUT -p tcp --dport 22 -m connlimit --connlimit-above 2 -j REJECT ##一個IP地址允許有兩個連接連到服務器的22號端口。
這樣配置就可以了。

對於linux6.x版本這個模塊已經不需要手動的添加到內核了。

iptables的man手冊裡面有詳細的說明,還有使用的例子。
connlimit
Allows you to restrict the number of parallel TCP connections to a server per client IP address (or address
block).

[!] --connlimit-above n
match if the number of existing tcp connections is (not) above n

--connlimit-mask bits
group hosts using mask

Examples:

# allow 2 telnet connections per client host
iptables -p tcp --syn --dport 23 -m connlimit --connlimit-above 2 -j REJECT

# you can also match the other way around:
iptables -p tcp --syn --dport 23 -m connlimit ! --connlimit-above 2 -j ACCEPT

# limit the nr of parallel http requests to 16 per class C sized network (24 bit netmask)
iptables -p tcp --syn --dport 80 -m connlimit --connlimit-above 16 --connlimit-mask 24 -j REJECT

Copyright © Linux教程網 All Rights Reserved