我有iptables运行,因为我可以使用下面的命令…
sudo / sbin / iptables sudo / sbin / iptables -t nat -I PREROUTING -p tcp –dport 80 -j REDIRECT – 到端口8080
但我不能这样做….
服务iptables保存iptables:无法识别的服务
当我重新启动服务器时,如何在映射停止时实际保存这些更改?
谢谢。
据我所知,Ubuntu在最新版本中使用UFW ,所以很可能它没有用于iptables的SysV init脚本。 如果你对UFW不满意,你可以使用sudo iptables-save > /etc/iptables.rules来存储你的当前规则,然后从rc.local调用sudo iptables-restore < /etc/iptables.rules (例如)在启动时恢复规则。
或者,你可以添加你自己的iptables的init脚本,如下所示:
#!/bin/sh -e ### BEGIN INIT INFO # Provides: iptables # Required-Start: mountvirtfs ifupdown $local_fs # Default-Start: S # Default-Stop: 0 6 ### END INIT INFO # July 9, 2007 # James B. Crocker <[email protected]> # Creative Commons Attribution - Share Alike 3.0 License (BY,SA) # Script to load/unload/save iptables firewall settings. PATH="/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin" IPTABLES=/sbin/iptables IPTABLES_SAVE=/sbin/iptables-save IPTABLES_RESTORE=/sbin/iptables-restore IPTABLES_CONFIG=/etc/iptables.conf [ -x $IPTABLES ] || exit 0 . /lib/lsb/init-functions case "$1" in start) log_action_begin_msg "Starting firewall" type usplash_write >/dev/null 2>/dev/null && usplash_write "TIMEOUT 120" || true if $IPTABLES_RESTORE < $IPTABLES_CONFIG ; then log_action_end_msg $? else log_action_end_msg $? fi type usplash_write >/dev/null 2>/dev/null && usplash_write "TIMEOUT 15" || true ;; stop) log_action_begin_msg "Saving current firewall configuration" if $IPTABLES_SAVE > $IPTABLES_CONFIG ; then log_action_end_msg $? else log_action_end_msg $? fi log_action_begin_msg "Flushing ALL firewall rules from chains!" if $IPTABLES -F ; then log_action_end_msg $? else log_action_end_msg $? fi log_action_begin_msg "Deleting ALL firewall chains [Warning: ACCEPTING ALL PORT SERVICES!]" if $IPTABLES -X ; then $IPTABLES -P INPUT ACCEPT $IPTABLES -P FORWARD ACCEPT $IPTABLES -P OUTPUT ACCEPT log_action_end_msg $? else log_action_end_msg $? fi ;; save) log_action_begin_msg "Saving current firewall configuration" if $IPTABLES_SAVE > $IPTABLES_CONFIG ; then log_action_end_msg $? else log_action_end_msg $? fi ;; force-reload|restart) log_action_begin_msg "Reloading firewall configuration [Warning: POTENTIAL NETWORK INSECURITY DURING RELOAD]" $IPTABLES -F $IPTABLES -X if $IPTABLES_RESTORE < $IPTABLES_CONFIG ; then log_action_end_msg $? else log_action_end_msg $? fi ;; *) echo "Usage: /etc/init.d/iptables {start|stop|save|restart|force-reload}" exit 1 ;; esac exit 0
一旦你执行它,你的iptables规则就被激活了。 发起重启只会恢复你的iptablesconfiguration,然而它最后被保存。 没有必要重新启动。
iptables-save iptables-restore
会将规则保存在纯文本文件中,并且会在系统启动时由init.d脚本恢复。 有关详细信息,请参阅iptables.conf和iptables man
为什么不这么简单呢? 在Debian / Ubuntu中,您可以:
1 – 创build你的iptables规则
2 – 运行sudo apt-get install iptables-persistent ; 它会询问您是否要保存规则并在启动后恢复它们。