我正在尝试为我的 VDS 构建一些基本的 iptables 规则:
iptables -A INPUT -p tcp --dport ssh -j ACCEPT
iptables -A INPUT -p tcp --dport 80 -j ACCEPT
iptables -A INPUT -p tcp --dport 443 -j ACCEPT
iptables -P INPUT DROP
我想阻止除 SSH 和 WEB 流量之外的任何传入流量。但是应用规则并重新启动系统后,通过 SSH 登录需要 30 秒,登录过程非常慢,但连接后一切正常。
我应该添加哪些规则才能加快通过 SSH 登录的速度?
答案1
从iptables --help
:
--numeric -n numeric output of addresses and ports
https://serverfault.com/questions/85602/iptables-l-pretty-slow-is-this-normal
包含该-n
选项,这样它就不会尝试使用 DNS 来解析每个 IP 地址、网络和端口的名称。这样它就会很快。
https://help.ubuntu.com/community/IptablesHowTo
允许建立会话
我们可以允许已建立的会话接收流量:
sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
如果上面这行代码不起作用,那么你可能使用的是劣质 VPS,其提供商没有提供该扩展,在这种情况下,可以使用劣质版本作为最后的手段:
sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
编辑掉,因为 help.ubuntu.com 提供了更好更完整的解决方案忽略下面
根据现有流量接受流量的规则
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
答案2
运行以下两个命令后我解决了同样的问题
# Allow Established and Related Incoming Connections
iptables -I INPUT 1 -m conntrack --ctstate ESTABLISH
ED,RELATED -j ACCEPT
# Allow Established Outgoing Connections
iptables -I OUTPUT 1 -m conntrack --ctstate ESTABLISHED -j ACCEPT
这两个命令来自Iptables 基础知识:常见防火墙规则和命令。