在CentOS系统中,使用iptables进行流量控制主要涉及到两个模块:ip_conntrack和hashlimit
确保内核支持conntrack和hashlimit模块。你可以通过以下命令检查它们是否已加载:
lsmod | grep conntrack
lsmod | grep hashlimit
如果没有加载,请使用以下命令加载它们:
modprobe ip_conntrack
modprobe nf_conntrack
modprobe nf_conntrack_ftp
modprobe nf_conntrack_ipv4
modprobe nf_conntrack_ipv6
modprobe iptable_hashlimit
创建一个新的iptables规则链,用于流量控制:
iptables -N TRAFFIC_CONTROL
将所有流量重定向到新创建的规则链:
iptables -A INPUT -j TRAFFIC_CONTROL
iptables -A OUTPUT -j TRAFFIC_CONTROL
iptables -A FORWARD -j TRAFFIC_CONTROL
为特定IP地址或子网设置流量限制。例如,限制IP地址192.168.1.100的下载速度为1Mbps:
iptables -A TRAFFIC_CONTROL -s 192.168.1.100 -p tcp --dport 80 -m hashlimit --hashlimit-name http_limit --hashlimit-mode srcip --hashlimit-upto 1mbit/s --hashlimit-burst 15kb -j ACCEPT
iptables -A TRAFFIC_CONTROL -s 192.168.1.100 -p tcp --dport 443 -m hashlimit --hashlimit-name https_limit --hashlimit-mode srcip --hashlimit-upto 1mbit/s --hashlimit-burst 15kb -j ACCEPT
iptables -A TRAFFIC_CONTROL -s 192.168.1.100 -j DROP
这里,我们限制了IP地址192.168.1.100的HTTP和HTTPS下载速度为1Mbps。--hashlimit-upto参数设置限制速度,--hashlimit-burst参数设置允许的突发流量。
保存iptables规则:
service iptables save
重启iptables服务以应用更改:
service iptables restart
这样,你就使用CentOS iptables实现了流量控制。请注意,这只是一个简单的示例,你可以根据需要调整规则以满足你的需求。