答案1
通常你会有这样的 iptables 规则:
# define standard chains and default behaviour (here ACCEPT, could be DROP)
:INPUT ACCEPT [0:0]
:FORWARD ACCEPT [0:0]
:OUTPUT ACCEPT [0:0]
# everything as a continuation is OK, This will be the bulk => 1st rule
-A INPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# drop garbage packages
-A INPUT -m conntrack --ctstate INVALID -j DROP
# server services including your new web server
-A INPUT -p tcp -m tcp --dport 12345 -m conntrack --ctstate NEW -j ACCEPT
-A INPUT -p tcp -m tcp --dport 2103 -m conntrack --ctstate NEW -j ACCEPT
# (perhaps some more, do include SSH!)
# allow incoming ping (good for testing)
-A INPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
-A INPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT
# rest / default: DROP!
-A INPUT -j DROP
# Again, everything related is allowed
-A OUTPUT -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
# allow DNS, both UDP and TCP
-A OUTPUT -p tcp -m tcp --dport 53 -m conntrack --ctstate NEW -j ACCEPT
-A OUTPUT -p udp -m udp --dport 53 -m conntrack --ctstate NEW -j ACCEPT
# allow access to time servers (NTP)
-A OUTPUT -p udp -m udp --dport 123 -m conntrack --ctstate NEW -j ACCEPT
# Allow anything else you need, e.g. for OS update servers
-A OUTPUT -p tcp -m conntrack --ctstate NEW -m tcp -m multiport --dports 80,443 -j ACCEPT
# Allow outgoing ping
-A OUTPUT -p icmp -m icmp --icmp-type 0 -j ACCEPT
-A OUTPUT -p icmp -m icmp --icmp-type 8 -j ACCEPT
# disallow the rest!
-A OUTPUT -j DROP
# Forward usually not needed except for routing and NAT
-A FORWARD -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
-A FORWARD -j REJECT --reject-with icmp-host-prohibited
您缺少 RELATED 状态。
类似上面的规则通常会写入文件中
# /etc/sysconfig/iptables
可以通过命令保存它们
# iptables-save > /etc/sysconfig/iptables
并从该文件中恢复
# iptables-restore < /etc/sysconfig/iptables
有一个名为的系统服务iptables.service
可以在启动时从该文件恢复表。根据您的系统,您可能有也可能没有该服务,并且它可能被禁用 ( systemctl status iptables
)。
如果你确实有该服务(yum install iptables-services
在 CentOS 中),那么在测试你的 iptables 规则后,保存它们一次,
# iptables-save > /etc/sysconfig/iptables
然后激活服务
# systemctl enable iptables
# systemctl start iptables
下次启动后,通过发出(以 root 身份,就像此处的所有命令一样)检查当前条目来测试这是否有效:
# iptables -L -nv