我们正在为一台机器设置防火墙,该防火墙应允许:
- 来自特定 IP 地址的传入 SSH 连接;
- ICMP,重定向除外。
我们选择使用 REJECT 而不是 DROP,至少在我们完成当前的测试之前是这样。
*filter
# Allow all loopback (lo0) traffic and reject traffic to localhost that does not originate from lo0.
-A INPUT -i lo -j ACCEPT
-A INPUT ! -i lo -s 127.0.0.0/8 -j REJECT
# Reject ICMP redirect.
-A INPUT -p icmp --icmp-type 5 -j REJECT
# Allow ICMP (the types which are not rejected).
-A INPUT -p icmp -j ACCEPT
# Allow SSH connections from specific address
-A INPUT -p tcp --dport 22 -s x.x.x.x -j ACCEPT
# Allow inbound traffic from established connections, including ICMP error returns.
-A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Log what was incoming but denied (optional but useful).
-A INPUT -m limit --limit 5/min -j LOG --log-prefix "iptables_INPUT_denied: " --log-level 7
# Reject all other inbound.
-A INPUT -j REJECT
# Log any traffic which was sent to you for forwarding (optional but useful).
-A FORWARD -m limit --limit 5/min -j LOG --log-prefix "iptables_FORWARD_denied: " --log-level 7
# Reject all traffic forwarding.
-A FORWARD -j REJECT
COMMIT
该机器位于 NAT 路由器后面,端口 22 转发给它。
当尝试从未经授权的 IP 地址进行连接时,与主机的连接会在一分钟左右后超时。
ssh: connect to host x.x.x.x port 22: Connection timed out
这是预期的吗?我认为 REJECT 会立即导致连接失败。是否是因为 REJECT ICMP 数据包没有返回到连接计算机?