在Ubuntu系统中,使用iptables限制连接数的方法如下:
sudo apt-get update
sudo apt-get install iptables
sudo iptables -L
LIMITED_CONNECTIONS的链:sudo iptables -N LIMITED_CONNECTIONS
LIMITED_CONNECTIONS链:sudo iptables -A INPUT -j LIMITED_CONNECTIONS
LIMITED_CONNECTIONS链中,我们将限制每个IP地址的连接数。例如,限制每个IP地址每分钟最多允许5个连接:sudo iptables -A LIMITED_CONNECTIONS -m connlimit --connlimit-above 5 -j DROP
sudo iptables -A LIMITED_CONNECTIONS -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
sudo iptables-save > /etc/iptables/rules.v4
现在,iptables已经限制了每个IP地址的连接数。请注意,这些规则在系统重启后将丢失。要使规则永久生效,请安装iptables-persistent:
sudo apt-get install iptables-persistent
在安装过程中,选择“是”以保存当前规则。