大网数据广告活动
服务器运维问答

Limit packet rate, open connections, and IP addresses

· 52CE 我爱测

On Linux, can I use tools like tc , iptables or others to control/shape network traffic on a network interface, for the following purposes: Control the network packet number rate (or the total number). Control the number of IP ad…

问题原文

原帖链接 · WindChaser · CC BY-SA 3.0

On Linux, can I use tools like tc, iptables or others to control/shape network traffic on a network interface, for the following purposes:

  1. Control the network packet number rate (or the total number).
  2. Control the number of IP addresses connected (sent).
  3. Control the total number of open TCP connections (per second).

If so, how to do that?

已采纳解答

原帖链接 · Luca Gibelli · CC BY-SA 4.0

I don't have an answer for nr. 2 (see below). For 1. and 3. you can use the limit module:

  1. Control the network packet number rate:

    iptables -A OUTPUT -m limit --limit 10/s -j ACCEPT
    
  2. Control the total number of open TCP connections (per second):

    iptables -A INPUT -m state -m tcp -p tcp --dport 80 --state RELATED,ESTABLISHED -m limit --limit 10/second -j ACCEPT
    

Alternatively you can use the hashlimit module:

  1. Control the network packet number rate:

    iptables -A INPUT -m hashlimit -m tcp -p tcp --hashlimit-mode srcip --hashlimit-srcmask 32 --hashlimit-above 10/sec --hashlimit-burst 2 --hashlimit-htable-expire 30000 --hashlimit-name pktlimit -j DROP
    
  2. Control the total number of open TCP connections (per second):

    iptables -I INPUT -m hashlimit -m tcp -p tcp --dport 80 --hashlimit-above 10/sec --hashlimit-mode srcip --hashlimit-name connlimit -m state --state RELATED,ESTABLISHED -j DROP
    

You can monitor how hashlimit is performing for you by looking at:

cat /proc/net/ipt_hashlimit/pktlimit
cat /proc/net/ipt_hashlimit/connlimit

EDIT:

In a world where NAT is dominant, does it really make sense to limit the number of source IP addresses like you want to do in nr. 2? I think it makes more sense to limit the total number of open TCP connections, like this:

iptables -A INPUT -p tcp --syn --dport 80 -m connlimit --connlimit-above 10 --connlimit-mask 32 -j REJECT --reject-with tcp-reset

摘自公开归档,保留原文语言,版式经整理,外部媒体与邮箱地址已移除。

资料来源与适用范围

资料来源
Server Fault
适用范围
以原帖系统、软件版本与发布日期为准(2018-05-01)
内容语言
中文;命令、路径和错误标识保留技术原文
本地收录日期
2026-11-15
授权说明
CC BY-SA 3.0 / CC BY-SA 4.0

正文与所需资源由本站本地存储。查看来源及更新记录 ↗

请按适用版本和实际日志确认原因;本站没有对每一种系统或设备逐项执行文中的操作。