温馨提示×

ubuntu iptables如何限速ip

小樊
47
2026-10-15 15:46:03
栏目: 智能运维

在 Ubuntu 上使用 iptables 对某个 IP 进行限速,通常借助 hashlimit 模块(推荐)或 tc(流量控制)。下面按常见场景说明。


一、使用 iptables + hashlimit 限制 IP 速率(最常用)

1️⃣ 限制某个 IP 的入站速率

例如限制 192.168.1.100 每秒最多 100 个包:

iptables -A INPUT -s 192.168.1.100 \
  -m hashlimit \
  --hashlimit-name limit_ip \
  --hashlimit-mode srcip \
  --hashlimit-above 100/sec \
  -j DROP

超过 100 包/秒的流量将被丢弃


2️⃣ 限制某个 IP 的出站速率

iptables -A OUTPUT -d 192.168.1.100 \
  -m hashlimit \
  --hashlimit-name limit_ip_out \
  --hashlimit-mode dstip \
  --hashlimit-above 100/sec \
  -j DROP

3️⃣ 限制带宽(按字节)

例如限制每秒 1MB(约 8Mbps):

iptables -A INPUT -s 192.168.1.100 \
  -m hashlimit \
  --hashlimit-name bw_limit \
  --hashlimit-mode srcip \
  --hashlimit-above 1mb/s \
  -j DROP

支持单位:

  • kb/s
  • mb/s
  • kbit/s
  • mbit/s

4️⃣ 限制单个 IP 的总连接速率(防刷)

iptables -A INPUT -s 192.168.1.100 \
  -m hashlimit \
  --hashlimit-name conn_limit \
  --hashlimit-mode srcip \
  --hashlimit-above 20/minute \
  -j DROP

二、限制某个 IP 的并发连接数

(不是速率,但常用于防护)

iptables -A INPUT -s 192.168.1.100 \
  -m connlimit --connlimit-above 10 \
  -j DROP

超过 10 个并发连接直接拒绝


三、查看 & 删除规则

查看规则

iptables -L -n -v

删除规则

iptables -D INPUT -s 192.168.1.100 -m hashlimit ...

或按编号删除:

iptables -L INPUT --line-numbers
iptables -D INPUT 3

四、更精确的限速方案(推荐高级用户)

如果你需要真正限速而不是丢包,建议使用:

tc + htb + iptables MARK

示例(简化):

tc qdisc add dev eth0 root htb
tc class add dev eth0 parent 1: classid 1:10 htb rate 1mbit
iptables -t mangle -A PREROUTING -s 192.168.1.100 -j MARK --set-mark 10
tc filter add dev eth0 protocol ip parent 1:0 handle 10 fw flowid 1:10

五、常见问题

✅ Ubuntu 默认已支持 hashlimit
❌ iptables 限速是“丢包式”,不是平滑限速
✅ Docker / 云服务器注意网卡名称(如 eth0, ens33)


如果你能告诉我:

  • 限速方向(进/出)
  • 限速目标(IP / 端口 / 协议)
  • 想要“丢包”还是“平滑限速”

我可以给你更精确的命令。

0 踩