我是靠谱客的博主 欢喜丝袜,这篇文章主要介绍使用 hashlimit 限制 新建连接 速率新建连接速率控制 后台指令变量解释源 ip 新建连接速率控制 例子目的 ip 新建连接速率控制 例子测试用例hashlimit 参考ref,现在分享给大家,希望可以做个参考。
新建连接速率控制 后台指令
作用:
使用 hashlimit 限制 新建连接 速率。
源 ip 新建连接速率控制 iptables 指令:
iptables -A FORWARD -s <src_ip>/<src_ip_mask> -m state --state NEW -m hashlimit --hashlimit-name <limit_name> --hashlimit-above <limit_rate>/<limit_unit> --hashlimit-burst <burst> --hashlimit-mode srcip --hashlimit-srcmask <src_ip_mask> -j DROP
目的 ip 新建连接速率控制 iptables 指令:
iptables -A FORWARD -d <dst_ip>/<src_ip_mask> -m state --state NEW -m hashlimit --hashlimit-name <limit_name> --hashlimit-above <limit_rate>/<limit_unit> --hashlimit-burst <burst> --hashlimit-mode dstip --hashlimit-dstmask <dst_ip_mask> -j DROP
注意:
<burst>
的 数值最好 略大于 <limit_rate>
,可以保证流量限制又一定的余量。
如果 <burst>
的数组接近于 0
,会非常容易丢包。
变量解释
<src_ip>
- 描述:源 ip
- 值域:0.0.0.0~255.255.255.255
<dst_ip>
- 描述:目的 ip
- 值域:0.0.0.0~255.255.255.255
<src_ip_mask>
- 描述:源 ip 掩码长度:
- 值域:0~32
<dst_ip_mask>
- 描述:目的 ip 掩码长度:
- 值域:0~32
<limit_name>
- 描述:哈希表名称
- 值域:英文字符串 (注意:不可以用中文)
<limit_rate>
- 描述:最大连接速率
- 值域:1~65535
<limit_unit>
- 描述:时间单位
- 值域:{ sec | min | hour | day }
<burst>
- 描述:并发连接数
- 值域:1~65535
源 ip 新建连接速率控制 例子
设置参数:
名称 : ping # <limit_name> = "ping";
源 ip/ip 组 : 192.168.111.1/32 # <src_ip> = 192.168.111.1; <src_ip_mask> = 32;
最大连接速率 : 30 / 分钟 # <limit_rate> = 30; <limit_unit> = "min"
并发连接数 : 5 # <burst> = 5
后台指令:
iptables -A FORWARD -s 192.168.111.1/32 -m state --state NEW -m hashlimit --hashlimit-name ping --hashlimit-above 30/min --hashlimit-burst 5 --hashlimit-mode srcip --hashlimit-srcmask 32 -j DROP
目的 ip 新建连接速率控制 例子
设置参数:
名称 : visitor_limited # <limit_name> = "visitor_limited";
源 ip/ip 组 : 192.168.222.0/24 # <src_ip> = 192.168.222.0; <src_ip_mask> = 24;
最大连接速率 : 600 / 秒 # <limit_rate> = 600; <limit_unit> = "sec"
并发连接数 : 300 # <burst> = 300
后台指令:
iptables -A FORWARD -d 192.168.222.0/24 -m state --state NEW -m hashlimit --hashlimit-name visitor_limited --hashlimit-above 600/sec --hashlimit-burst 300 --hashlimit-mode dstip --hashlimit-dstmask 24 -j DROP
测试用例
源 ip 新建连接速率控制例子。
测试拓扑
192.168.111.1 192.168.111.254 192.168.222.254 192.168.222.1
[pc1]-------------------------------[firewall]--------------------------------[pc2]
测试要求
pc1 和 pc2 两者 按照速率 1新建连接/sec 进行互 ping。
限制 pc1 到 pc2,一分钟只能 有 30个 新建连接。
pc2 到 pc1 不限制。
pc 测试用脚本
脚本功能:
脚本每一秒中,新建一个 ping 连接。
- ping 通,返回 PASS。
- ping 不通,返回 FAIL。
使用方法:
# <peer_ip> 是 对端 ip 地址
sh test.sh <peer_ip>
文件名称:
test.sh
文件内容:
#!/bin/bash
i=0
ip=$1
while [[ 1 ]];
do
timestamp=`date`
ping "${ip}" -c 1 > /dev/null
result=$?
if [ "${result}" -eq "0" ]; then
echo "[${i}] ----- PASS @ ${timestamp}"
else
echo "[${i}] ------------ FAIL @ ${timestamp}"
fi
sleep 1
let i+=1
done
firewall 后台输入指令
# 限制 pc1(192.168.111.1) 到 pc2(192.168.222.1),一分钟只能 有 30个 新建连接。
# 并发数 为 5。
iptables -A FORWARD -s 192.168.111.1/32 -m state --state NEW -m hashlimit --hashlimit-name ping --hashlimit-above 30/min --hashlimit-burst 5 --hashlimit-mode srcip --hashlimit-srcmask 32 -j DROP
pc1 后台输入指令
# ping: 192.168.111.1 --> 192.168.222.1,速率: 1新建连接/sec。
sh test.sh 192.168.222.1
pc2 后台输入指令
# ping: 192.168.222.1 --> 192.168.111.1,速率: 1新建连接/sec。
sh test.sh 192.168.111.1
pc1 后台输出
[229] ------------ FAIL @ Tue May 7 16:56:03 CST 2019 # 一分钟开始计时
[230] ----- PASS @ Tue May 7 16:56:14 CST 2019
[231] ----- PASS @ Tue May 7 16:56:15 CST 2019
[232] ----- PASS @ Tue May 7 16:56:16 CST 2019
[233] ----- PASS @ Tue May 7 16:56:17 CST 2019
[234] ----- PASS @ Tue May 7 16:56:18 CST 2019
[235] ----- PASS @ Tue May 7 16:56:19 CST 2019
[236] ----- PASS @ Tue May 7 16:56:20 CST 2019
[237] ----- PASS @ Tue May 7 16:56:21 CST 2019
[238] ----- PASS @ Tue May 7 16:56:22 CST 2019
[239] ------------ FAIL @ Tue May 7 16:56:23 CST 2019
[240] ----- PASS @ Tue May 7 16:56:34 CST 2019
[241] ----- PASS @ Tue May 7 16:56:35 CST 2019
[242] ----- PASS @ Tue May 7 16:56:36 CST 2019
[243] ----- PASS @ Tue May 7 16:56:37 CST 2019
[244] ----- PASS @ Tue May 7 16:56:38 CST 2019
[245] ----- PASS @ Tue May 7 16:56:39 CST 2019
[246] ----- PASS @ Tue May 7 16:56:40 CST 2019
[247] ----- PASS @ Tue May 7 16:56:41 CST 2019
[248] ----- PASS @ Tue May 7 16:56:42 CST 2019
[249] ------------ FAIL @ Tue May 7 16:56:43 CST 2019
[250] ----- PASS @ Tue May 7 16:56:54 CST 2019
[251] ----- PASS @ Tue May 7 16:56:55 CST 2019
[252] ----- PASS @ Tue May 7 16:56:56 CST 2019
[253] ----- PASS @ Tue May 7 16:56:57 CST 2019
[254] ----- PASS @ Tue May 7 16:56:58 CST 2019
[255] ----- PASS @ Tue May 7 16:56:59 CST 2019
[256] ----- PASS @ Tue May 7 16:57:00 CST 2019
[257] ----- PASS @ Tue May 7 16:57:01 CST 2019
[258] ----- PASS @ Tue May 7 16:57:02 CST 2019
[259] ------------ FAIL @ Tue May 7 16:57:03 CST 2019 # 一分钟计时结束,最多 30 (259-229)个 新建连接 通过。
[260] ----- PASS @ Tue May 7 16:57:14 CST 2019
[261] ----- PASS @ Tue May 7 16:57:15 CST 2019
[262] ----- PASS @ Tue May 7 16:57:16 CST 2019
[263] ----- PASS @ Tue May 7 16:57:17 CST 2019
[264] ----- PASS @ Tue May 7 16:57:18 CST 2019
[265] ----- PASS @ Tue May 7 16:57:19 CST 2019
[266] ----- PASS @ Tue May 7 16:57:20 CST 2019
[267] ----- PASS @ Tue May 7 16:57:21 CST 2019
[268] ----- PASS @ Tue May 7 16:57:22 CST 2019
pc2 后台输出
# 不限速,全部都是 PASS
...
[64334] ----- PASS @ Wed May 8 10:44:55 CST 2019
[64335] ----- PASS @ Wed May 8 10:44:56 CST 2019
[64336] ----- PASS @ Wed May 8 10:44:57 CST 2019
[64337] ----- PASS @ Wed May 8 10:44:58 CST 2019
[64338] ----- PASS @ Wed May 8 10:44:59 CST 2019
[64339] ----- PASS @ Wed May 8 10:45:00 CST 2019
[64340] ----- PASS @ Wed May 8 10:45:01 CST 2019
[64341] ----- PASS @ Wed May 8 10:45:02 CST 2019
...
firewall 后台 查看 hashlimit 状态
# 观察 hashlimit `ping`
watch -n 1 cat /proc/net/ipt_hashlimit/ping
hashlimit 参考
hashlimit match options:
--hashlimit-upto <avg> max average match rate
[Packets per second unless followed by
/sec /min /hour /day postfixes]
--hashlimit-above <avg> min average match rate
--hashlimit-mode <mode> mode is a comma-separated list of
dstip,srcip,dstport,srcport (or none)
--hashlimit-srcmask <length> source address grouping prefix length
--hashlimit-dstmask <length> destination address grouping prefix length
--hashlimit-name <name> name for /proc/net/ipt_hashlimit
--hashlimit-burst <num> number to match in a burst, default 5
--hashlimit-htable-size <num> number of hashtable buckets
--hashlimit-htable-max <num> number of hashtable entries
--hashlimit-htable-gcinterval interval between garbage collection runs
--hashlimit-htable-expire after which time are idle entries expired?
ref
Understanding iptable’s hashlimit module
最后
以上就是欢喜丝袜最近收集整理的关于使用 hashlimit 限制 新建连接 速率新建连接速率控制 后台指令变量解释源 ip 新建连接速率控制 例子目的 ip 新建连接速率控制 例子测试用例hashlimit 参考ref的全部内容,更多相关使用内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复