今天在回答一个网友关于使用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 <jengelh@computergmbh.de>
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
iptables使用ipt_connlimit限制连接数
原创wenxue_813 博主文章分类:操作系统 ©著作权
©著作权归作者所有:来自51CTO博客作者wenxue_813的原创作品,请联系作者获取转载授权,否则将追究法律责任
上一篇:DRBD简单配置
下一篇:mysql用户和权限的基本管理
提问和评论都可以,用心的回复会被更多人看到
评论
发布评论
相关文章
-
Iptables扩展模块-connlimit 限制并发连接数
之前也学习过Nginx的限制,只不过它的限制是应用层面的限制,它还要将所有的请求经过网络
linux 连接数 html 应用层