因为我们刚才没有指定一个接口或一个协议,所以除了web和ssh流量外其他任何流量都会被阻断。
◆ 编辑 iptables
到目前为止我们设置过程中唯一的问题是回环端口(loopbakc)也被阻断了。我们本可以通过指定 -i eth0 来仅仅丢弃eth0上的数据包,但我们也可以为回环端口(loopback)添加一条规则。如果我们追加这条规则,这将太晚了----因为所有的流量已经 被丢弃。我们必须插入这条跪着到第4行。
# iptables -I INPUT 4 -i lo -j ACCEPT
# iptables -L
Chain INPUT (policy ACCEPT)
target prot opt source destination
ACCEPT all -- anywhere anywhere state RELATED,ESTABLISHED
ACCEPT tcp -- anywhere anywhere tcp dpt:ssh
ACCEPT tcp -- anywhere anywhere tcp dpt:www
ACCEPT all -- anywhere anywhere
DROP all -- anywhere anywhere
最後2行看起来几乎一样,因此我们可以让iptables列的更详细些。
# iptables -L -v
◆ 日志记录
在上面的例子中,所有的流量都不会被记录。如果您愿意在syslog中记录被丢弃的包, 下面将是最快捷的方式:
# iptables -I INPUT 5 -m limit --limit 5/min -j LOG --log-prefix "iptables denied: " --log-level 7
看 提示 段获得更多关于logging的ideas.
◆ 保存 iptables
如果您现在要重新启动机器的话,您的iptables配置将会消失。为了不用每次重新启动时敲入这些命令,您可以保存你的配置,让它在系统启动时自动启动。你可以通过iptables-save 和iptables-restore命令来保存配置。
◆ 配置启动时自动加载规则
保存您的防火墙股则到一个文件
# iptables-save > /etc/iptables.up.rules
接着修改 /etc/network/interfaces 脚本自动应用这些规则(末行是添加的)
auto eth0
iface eth0 inet dhcp
pre-up iptables-restore < /etc/iptables.up.rules
你也可以准备一组规则冰并自动应用它
auto eth0
iface eth0 inet dhcp
pre-up iptables-restore < /etc/iptables.up.rules
post-down iptables-restore < /etc/iptables.down.rules
◆ 提示
◆ 如果你要在一个规则基础上手动编辑iptables
下面的步骤复习了怎样建立你的防火墙规则,并假定它们相对固定(而且对于大多数人来说它们也应该是)。但是如果你要做许多研究工作,你也许想要你的 iptables在你每次重启时保存一次。你可以在 /etc/network/interfaces 里添加像下面的一行:
pre-up iptables-restore < /etc/iptables.up.rules
post-down iptables-save > /etc/iptables.up.rules
"post-down iptables-save > /etc/iptables.up.rules" 此行将保存规则用于下次启动时使用。
◆ 用iptables-save/restore来测试规则
如果你超出了这个指南来编辑iptables,你可能想利用iptables-save和iptables-restore来编辑和测试你的规则。你可以通过使用你喜爱的文本编辑器(此处为gedit)来打开这些规则文件来完成编辑。










