当前位置:首页 > CN2资讯 > 正文内容

iptables 进程限速 iptables limit限速

3天前CN2资讯


怎样使用IPTABLES限制IP上传下载速度如何用iptables限速?我们先来看范例:

iptables限制某IP的上传速度为1000KB/秒(8Mbps,流入服务器带宽),即在此IP所在的服务器或VPS上wget的速度

iptables -A FORWARD -m limit -d 208.8.14.53 --limit 700/s --limit-burst 100 -j ACCEPT 
iptables -A FORWARD -d 208.8.14.53 -j DROP

取消iptables限速:

iptables -D FORWARD -m limit -d 208.8.14.53 --limit 700/s --limit-burst 100 -j ACCEPT 
iptables -D FORWARD -d 208.8.14.53 -j DROP

 

限制某IP的上传速度为2000KB/秒(16Mbps,流入服务器带宽),即在此IP所在的服务器或VPS上wget的速度

iptables -A FORWARD -m limit -d 208.8.14.53 --limit 1400/s --limit-burst 100 -j ACCEPT 
iptables -A FORWARD -d 208.8.14.53 -j DROP

取消限制:

iptables -D FORWARD -m limit -d 208.8.14.53 --limit 1400/s --limit-burst 100 -j ACCEPT 
iptables -D FORWARD -d 208.8.14.53 -j DROP

如果要限制某IP下载速度(即网友通过网页下载的带宽/速度)参考

iptables -A FORWARD -s 208.8.14.36 -m limit --limit 700/s -j ACCEPT
iptables -A FORWARD -s 208.8.14.36 -j DROP

双向限制:

iptables -A FORWARD -m limit -d 208.8.14.53 --limit 2400/s --limit-burst 100 -j ACCEPT 
iptables -A FORWARD -d 208.8.14.53 -j DROP
iptables -A FORWARD -m limit -s 208.8.14.53 --limit 2400/s --limit-burst 100 -j ACCEPT 
iptables -A FORWARD -s 208.8.14.53 -j DROP

 

Iptables 的limit匹配ctohome.com大致介绍

限制匹配数据包的频率或速率,看清楚了,它是用来限制匹配的数据包的频率和速率的.这里“limit”这个词经常给别人“限制”的误解, 其实准确说,应该是“按一定速率去匹配”,至于“限制”还是“放行”是后面 -j 动作来实现的,limit 仅仅是个 match 模块,他的功能是匹配,匹配方式是按一定速率.

iptables的limit模块,目标是ACCEPT.当你设置300/s时,它大约每3ms发出一个令牌,获得令牌的包可以发出去,没有获得令牌的包只能等待下一个令牌到来,这样不会造成一些包丢失,更不会造成所谓“断线”的. 

以下2条是对icmp的burst限制

iptables -A INPUT -p icmp -m limit --limit 1/sec --limit-burst 10 -j ACCEPT
iptables -A INPUT -p icmp -j DROP

第一条ipables的意思是限制ping包每一秒钟一个,10个后重新开始.

同时可以限制IP碎片,每秒钟只允许100个碎片,用来防止DoS攻击.

iptables -A INPUT -f -m limit --limit 100/sec --limit-burst 100 -j ACCEPT


下面ctohome.com详细的讲述一下iptables的limit模块的功能:


限制 ping (echo-request) 传入的速度
限制前, 可正常每 0.2 秒 ping 一次
ping your.linux.ip -i 0.2

限制每秒只接受一个 icmp echo-request 封包
iptables -A INPUT -p icmp --icmp-type echo-request -m limit --limit 1/s --limit-burst 1 -j ACCEPT
iptables -A INPUT -p icmp --icmp-type echo-request -j DROP

--limit 1/s 表示每秒一次; 1/m 则为每分钟一次
--limit-burst 表示允许触发 limit 限制的最大次数 (预设 5)

再以每 0.2 秒 ping 一次, 得到的响应是每秒一次
ping your.linux.ip -i 0.2

下面规则亦可达到每秒只接受一个 echo-request 封包
iptables -N pinglimit
iptables -A pinglimit -m limit --limit 1/s --limit-burst 1 -j ACCEPT
iptables -A pinglimit -j DROP
iptables -A INPUT -p icmp --icmp-type echo-request -j pinglimit

iptables限速原理解释:

iptables limit 参数
· 限制特定封包传入速度
· 限制特定端口口连入频率
· iptables Log 记录参数备忘
· 自定 Chain 使用备忘
· 防治 SYN-Flood 碎片攻击
限制 ping (echo-request) 传入的速度
限制前, 可正常每 0.2 秒 ping 一次
ping your.linux.ip -i 0.2
限制每秒只接受一个 icmp echo-request 封包
iptables -A INPUT -p icmp –icmp-type echo-request -m limit –limit 1/s –limit-burst 1 -j ACCEPT
iptables -A INPUT -p icmp –icmp-type echo-request -j DROP
–limit 1/s 表示每秒一次; 1/m 则为每分钟一次
–limit-burst 表示允许触发 limit 限制的最大次数 (预设 5)
再以每 0.2 秒 ping 一次, 得到的响应是每秒一次
ping your.linux.ip -i 0.2

限制 ssh 连入频率
建立自订 Chain, 限制 tcp 联机每分钟一次, 超过者触发 Log 记录 (记录在 /var/log/messages)
iptables -N ratelimit
iptables -A ratelimit -p tcp -m state –state ESTABLISHED,RELATED -j ACCEPT  
iptables -A ratelimit -p tcp –syn -m limit –limit 1/m –limit-burst 1 -j ACCEPT
iptables -A ratelimit -p tcp -j LOG –log-level “NOTICE” –log-prefix “[RATELIMIT]”
iptables -A ratelimit -p tcp -j DROP
引用自订 Chain, 限制 ssh (tcp port 22) 连入频率
iptables -A INPUT -p tcp –dport 22 -s 192.168.0.0/16 -j ACCEPT (特定 IP 来源不受限制)
iptables -A INPUT -p tcp –dport 22 -j ratelimit

sshd_config 设定备忘:
· LoginGraceTime 30 密码输入时限为 30 秒
· MaxAuthTries 2 最多只能输入 3 次密码
同理可证
iptables -N pinglimit
iptables -A pinglimit -m limit –limit 1/s –limit-burst 1 -j ACCEPT
iptables -A pinglimit -j DROP
iptables -A INPUT -p icmp –icmp-type echo-request -j pinglimit
亦可达到每秒只接受一个 echo-request 封包
补充: 清除自订 Chain
iptables -L -n –line-number
iptables -D INPUT n
iptables -F ratelimit
iptables -X ratelimit

防治 SYN-Flood 碎片攻击
iptables -N syn-flood
iptables -A syn-flood -m limit –limit 100/s –limit-burst 150 -j RETURN
iptables -A syn-flood -j DROP
iptables -I INPUT -j syn-flood
模拟攻击
wget http:///tools/200102/naptha-1.1.tgz
wget ftp://rpmfind.net/linux/freshrpms/redhat/7.0/libnet/libnet-1.0.1b-1.src.rpm
tar -zxf naptha-1.1.tgz
rpmbuild –recompile libnet-1.0.1b-1.src.rpm
cp -r /var/tmp/libnet-buildroot/usr/* /usr/local/
cd naptha-1.1
make
./synsend your.linux.host.ip 80 local.host.eth0.ip 0.1
若成功抵挡, 不久后会出现 Can’t send packet!: Operation not permitted 的讯息

 

参考文章:http://www.ctohome.com/FuWuQi/b4/682.html

 

限速实例:



做限速 30M


[root@ip# iptables -nvL Chain INPUT (policy ACCEPT 68254 packets, 5601K bytes) pkts bytes target prot opt in out source destination 10.248.0.0/16 0.0.0.0/0 limit: avg 500/sec burst 100 11727 626K DROP all -- * * 10.248.0.0/16 0.0.0.0/0 Chain FORWARD (policy ACCEPT 0 packets, 0 bytes) pkts bytes target prot opt in out source destination Chain OUTPUT (policy ACCEPT 71217 packets, 273M bytes) pkts bytes target prot opt in out source destination all -- * * 0.0.0.0/0 10.248.0.0/16 limit: avg 500/sec burst 100 28013 96M DROP all -- * * 0.0.0.0/0 10.248.0.0/16 iptables -A INPUT -s 10.248.0.0/16 -m limit --limit 500/s --limit-burst 100 -j ACCEPT iptables -A INPUT -s 10.248.0.0/16 -j DROP iptables -A OUTPUT -d 10.248.0.0/16 -m limit --limit 500/s --limit-burst 100 -j ACCEPT iptables -A OUTPUT -d 10.248.0.0/16 -j DROP



 



做测试 



 

iperf3 -u -c 10.248.1.6 -b 30M -t 60 Connecting to host 10.248.1.6, port 5201 [ 4] local 10.248.1.5 port 36025 connected to 10.248.1.6 port 5201 [ ID] Interval Transfer Bandwidth [ 4] 0.00-60.00 sec 225 MBytes 31.4 Mbits/sec [ ID] Interval Transfer Bandwidth Jitter Lost/Total Datagrams 31.4 Mbits/sec 0.158 ms 44/28784 (0.15%) [ 4] Sent 28784 datagrams iperf3 -u -c 10.248.1.6 -b 35M -t 60 Connecting to host 10.248.1.6, port 5201 [ 4] local 10.248.1.5 port 46370 connected to 10.248.1.6 port 5201 iperf3: error - unable to write to stream socket: Operation not permitted iperf3 -c 10.248.1.6 -t 60 Connecting to host 10.248.1.6, port 5201 [ 4] local 10.248.1.5 port 47891 connected to 10.248.1.6 port 5201 [ ID] Interval Transfer Bandwidth Retransmits [ 4] 0.00-60.48 sec 110 MBytes 15.3 Mbits/sec 20 [ ID] Interval Transfer Bandwidth Retransmits Sent c 110 MBytes 15.3 Mbits/sec 20 Received [ 4] 0.00-60.48 sec 109 MBytes 15.2 Mbits/sec


 



 

    你可能想看:

    扫描二维码推送至手机访问。

    版权声明:本文由皇冠云发布,如需转载请注明出处。

    本文链接:https://www.idchg.com/info/19397.html

    分享给朋友:

    “iptables 进程限速 iptables limit限速” 的相关文章

    印度尼西亚VPS服务商推荐:如何选择最佳服务提升业务效率

    1.1 地理位置优势如何影响VPS服务? 印度尼西亚位于东南亚的核心位置,这一地理位置为其VPS服务商带来了显著的优势。与中国大陆的网络直连使得ping值稳定在50-80ms之间,这对于需要低延迟连接的用户来说是一个巨大的优势。无论是进行在线游戏、视频流媒体还是其他需要快速响应的应用,这种低延迟都能...

    APT是什么?高级持续性威胁的定义与防御策略

    APT是指高级持续性威胁(Advanced Persistent Threat),它代表了一种针对特定目标进行的长期和有计划的网络攻击。这种攻击的高端特征在于,攻击者会在施加攻击之前,详细调查并了解攻击对象的业务流程和系统架构。换句话说,APT并不是一种简单随机的攻击,而是通过深入分析和细致的侦查工...

    AS7473在网络数据传输中的重要性与应用探究

    AS7473简介 AS7473是一个重要的ASN编号,主要与网络数据传输和路由相关。它在信息技术领域中扮演着至关重要的角色,连接着不同的网络节点,确保数据能够顺利传输。想象一下,在这个数字化时代,数据的传输速度和准确性直接影响着我们的工作效率与信息交流。因此,AS7473的定义与重要性绝不容小觑。...

    Virmach虚拟主机评测:高性价比VPS服务推荐

    大家好,今天我想和你聊一聊Virmach,这是一家我非常推荐的虚拟主机提供商。Virmach专注于提供VPS(虚拟专用服务器)服务,近年来逐渐在行业中赢得了一席之地。它的价格相对亲民,而服务质量与稳定性也让人感到满意。很多人选择它,主要是因为它不仅适合个人用户,也非常受中小企业欢迎。 Virmach...

    深入了解M247 VPS:价格、性能与适用场景全分析

    M247 VPS概述 在如今数字化时代,云计算的需求不断上升,各种VPS(虚拟专用服务器)服务也层出不穷。今天我想和大家分享的是M247 VPS,它是一家相对年轻但却在行业内逐渐崭露头角的服务商。M247成立于2012年,隶属于M24Seven Group旗下,提供多种服务,包括VPS、虚拟主机、服...

    BBR加速开启:提升网络性能的最佳实践

    什么是BBR? 在网络领域,BBR(Bottleneck Bandwidth and Round-trip propagation time)是一个备受关注的TCP拥塞控制算法,由Google开发的这一技术,旨在提升网络连接的传输速率和稳定性。BBR独特之处在于,它通过实时监测数据包的传输时延与丢包...