设置iptables规则后无法访问实例

mj0*_*011 2 bash iptables shell-scripting

我正在尝试设置一些规则来阻止 TCP(SSH 和 FTP)上除 21 和 22 之外的所有端口。但是当我尝试运行这个脚本时,我被锁定在我的实例之外并且无法访问它。这是脚本:

# Flush the FW Rules 
iptables -F
iptables -X

# Block all traffic
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

# Allow SSH
iptables -A INPUT -p tcp --dport 22 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 22 -j ACCEPT

# Allow FTP
iptables -A INPUT -p tcp --dport 21 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 21 -j ACCEPT

# Allow ICMP (ping)
iptables -A INPUT -p icmp -j ACCEPT
iptables -A OUTPUT -p icmp -j ACCEPT
Run Code Online (Sandbox Code Playgroud)

在脚本中,它为 SSH 和 FTP 设置传入和传出请求,但为什么我无法访问它?

Ant*_*lov 7

  1. 对于OUTPUT链中的规则,您应该指定源端口匹配 ( --sport),而不是目标端口 ( --dport)。
  2. 无论如何DROPOUTPUT链中的策略并不常见。
  3. 阅读 iptables 教程和示例规则集。
  4. 为避免连接丢失,更好地使用iptables-saveiptables-apply工具。