环境:
[root@node11 ~]# uname -a
Linux node112.6.32-696.el6.x86_64 #1 SMP Tue Mar 2119:29:05 UTC 2017 x86_64 x86_64 x86_64GNU/Linux
[root@node11 ~]# cat/etc/redhat-release
CentOS release 6.9 (Final)
[root@node11 ~]# iptables -V
iptables v1.4.7
设置单IP连接本机的80端口 允许1秒内50个连接。多于50个会DROP掉
[root@localhost ~]# vim/etc/sysconfig/iptables //删除原来的内容,输入如下内容保存
#Generated by iptables-save v1.3.5 onSun jun 12 23:55:59 2019
#Design by Rsc(www.linuxmysql.com)
*filter
:INPUTDROP [0:0]
:FORWARDACCEPT [0:0]
:OUTPUTACCEPT [0:0]
-AINPUT -i lo -j ACCEPT
-AINPUT -m state --stateRELATED,ESTABLISHED -j ACCEPT
-AINPUT -p icmp -j ACCEPT
-AINPUT -s 127.0.0.1 -j ACCEPT
-AINPUT -p tcp -m tcp --dport 80 -s10.1.1.9 -m state --state NEW -m recent --set--name WEB --rsource
-AINPUT -p tcp -m tcp --dport 80 -s10.1.1.9 -m state --state NEW -m recent--update --seconds 1 --hitcount 50--rttl --name WEB --rsource -j DROP
-AINPUT -p tcp -m multiport --ports21,22,80 -j ACCEPT
-AINPUT -p tcp -m tcp --tcp-flagsSYN,RST,ACK SYN -m ttl --ttl-eq 117 -j DROP
-AINPUT -p tcp -m tcp --tcp-flagsSYN,RST,ACK SYN -m length --length 0:40 -j DROP
-AINPUT -p tcp -m tcp ! --tcp-flagsSYN,RST,ACK SYN -m state --state NEW -j DROP
COMMIT
#Completed on Sun Dec 12 23:55:59 2017
#-A INPUT -j REJECT--reject-withicmp-host-prohibited //注意这两行需要注释掉!否则设置的策略无效!
#-A FORWARD -j REJECT--reject-withicmp-host-prohibited
以上配置,说明此设定仅对外开放21(FTP),22(SSH),80(http网站)三个TCP端口。
[root@localhost ~]#/etc/init.d/iptables restart
[root@localhost ~] # serviceiptables reload
附上: WEB日志每秒并发连接数统计脚本:
[root@node11~]# cat concurrency.sh
#!/bin/bash
whiletrue
do
a=$(cat/usr/local/nginx/logs/access.log | wc -l)
sleep1
b=$(cat/usr/local/nginx/logs/access.log | wc -l)
echo$(expr $b - $a)
sleep1
done