#!/bin/sh
# Written by Simon Richter <[email protected]>
# modified by Jonathan Wiltshire <[email protected]>
# with help from Christoph Anton Mitterer
#
### BEGIN INIT INFO
# Provides: iptables-persistent
# Required-Start: mountkernfs $local_fs
# Required-Stop: $local_fs
# Default-Start: 2 3 4 5
# Default-Stop: 0 1 6
# X-Start-Before: $network
# X-Stop-After: $network
# Short-Description: Set up iptables rules
# Description: Loads/saves current iptables rules from/to /etc/iptables
# to provide a persistent rule set during boot time
### END INIT INFO
. /lib/lsb/init-functions
rc=0
load_rules()
{
log_action_begin_msg "Loading iptables rules"
#load IPv4 rules
if [ ! -f /etc/iptables/rules.v4 ]; then
log_action_cont_msg " skipping IPv4 (no rules to load)"
else
log_action_cont_msg " IPv4"
iptables-restore < /etc/iptables/rules.v4 2> /dev/null
if [ $? -ne 0 ]; then
rc=1
fi
fi
#load IPv6 rules
if [ ! -f /etc/iptables/rules.v6 ]; then
log_action_cont_msg " skipping IPv6 (no rules to load)"
else
log_action_cont_msg " IPv6"
ip6tables-restore < /etc/iptables/rules.v6 2> /dev/null
if [ $? -ne 0 ]; then
rc=1
fi
fi
log_action_end_msg $rc
}
save_rules()
{
log_action_begin_msg "Saving rules"
#save IPv4 rules
#need at least iptable_filter loaded:
/sbin/modprobe -q iptable_filter
if [ ! -f /proc/net/ip_tables_names ]; then
log_action_cont_msg " skipping IPv4 (no modules loaded)"
elif [ -x /sbin/iptables-save ]; then
log_action_cont_msg " IPv4"
iptables-save > /etc/iptables/rules.v4
if [ $? -ne 0 ]; then
rc=1
fi
fi
#save IPv6 rules
#need at least ip6table_filter loaded:
/sbin/modprobe -q ip6table_filter
if [ ! -f /proc/net/ip6_tables_names ]; then
log_action_cont_msg " skipping IPv6 (no modules loaded)"
elif [ -x /sbin/ip6tables-save ]; then
log_action_cont_msg " IPv6"
ip6tables-save > /etc/iptables/rules.v6
if [ $? -ne 0 ]; then
rc=1
fi
fi
log_action_end_msg $rc
}
flush_rules()
{
log_action_begin_msg "Flushing rules"
if [ ! -f /proc/net/ip_tables_names ]; then
log_action_cont_msg " skipping IPv4 (no module loaded)"
elif [ -x /sbin/iptables ]; then
log_action_cont_msg " IPv4"
for param in F Z X; do /sbin/iptables -$param; done
for table in $(cat /proc/net/ip_tables_names)
do
/sbin/iptables -t $table -F
/sbin/iptables -t $table -Z
/sbin/iptables -t $table -X
done
for chain in INPUT FORWARD OUTPUT
do
/sbin/iptables -P $chain ACCEPT
done
fi
if [ ! -f /proc/net/ip6_tables_names ]; then
log_action_cont_msg " skipping IPv6 (no module loaded)"
elif [ -x /sbin/ip6tables ]; then
log_action_cont_msg " IPv6"
for param in F Z X; do /sbin/ip6tables -$param; done
for table in $(cat /proc/net/ip6_tables_names)
do
/sbin/ip6tables -t $table -F
/sbin/ip6tables -t $table -Z
/sbin/ip6tables -t $table -X
done
for chain in INPUT FORWARD OUTPUT
do
/sbin/ip6tables -P $chain ACCEPT
done
fi
log_action_end_msg 0
}
case "$1" in
start|restart|reload|force-reload)
load_rules
;;
save)
save_rules
;;
stop)
# Why? because if stop is used, the firewall gets flushed for a variable
# amount of time during package upgrades, leaving the machine vulnerable
# It's also not always desirable to flush during purge
echo "Automatic flushing disabled, use \"flush\" instead of \"stop\""
;;
flush)
flush_rules
;;
*)
echo "Usage: $0 {start|restart|reload|force-reload|save|flush}" >&2
exit 1
;;
esac
exit $rc
我不知道“Ubuntu”,但在 Linux 中,“iptables”通常不是服务——它是一个操作 netfilter 内核防火墙的命令。您可以通过将所有标准链上的默认策略设置为“接受”并刷新规则来“禁用”(或停止)防火墙。
(您可能还需要刷新其他表,例如“nat”,如果您使用过它们)
Ubuntu 网站上的以下文章描述了设置 iptables 以与 NetworkManager 一起使用:https ://help.ubuntu.com/community/IptablesHowTo
你们都错了:-)
您正在寻找的命令是:
我会首先检查它是否安装了(可能是):
在 Ubuntu 上,iptables 不是服务。为了阻止它,您必须执行以下操作:
为了恢复您以前的规则:
这取自http://www.cyberciti.biz/faq/turn-on-turn-off-firewall-in-linux/并在许多 Ubuntu 8.X 和 9.10 安装上进行了测试。
iptables 是一个命令它不是一个服务,所以通常不可能使用类似的命令
或者
为了启动和停止防火墙,但是像centos这样的一些发行版已经安装了一个名为iptables的服务来启动和停止防火墙以及一个配置文件来配置它。无论如何,可以提供服务来管理 ipotables 编辑或安装此范围的脚本。linux 中的所有服务,ubuntu 也不例外,都是 /etc/init.d 文件夹中的可执行脚本,实现标准接口(启动、停止、重启) 可能的脚本如下所示:
这个脚本是本教程的一部分,所有配置防火墙的命令都必须根据上面的脚本插入到 /etc/iptables.conf 文件中。必须将此脚本插入到 /etc/init.d 中名为iptables的文件中,并使用以下命令使其可执行
并使用将服务添加到运行级别
您可以从 shell 添加新规则,这些规则将立即生效,并在服务停止时添加到 /etc/iptables.conf(这意味着它们将在系统关闭时确定保存)。
我希望这对每个人都有帮助。
因为 iptables 和 ufw 都是在 Linux 中管理netfilter防火墙的方法,并且因为两者都在 Ubuntu 中默认可用,所以您可以使用其中任何一个来启动和停止(和管理)防火墙规则。
iptables 更灵活,但是因为 ufw 提供了一种非常简单的界面语言,您可以使用简单而典型的功能:
sudo ufw disable
# 禁用防火墙sudo ufw enable
# 开启防火墙要查看当前防火墙设置
sudo ufw status verbose
,请使用 或iptables -L
。iptables和UFW上的 Ubuntu 社区文档页面有更多信息。
看起来有几种方法可以在 Ubuntu 中管理防火墙,因此您可能有兴趣阅读以下内容:https ://help.ubuntu.com/community/IptablesHowTo#Configuration%20on%20startup
要删除所有当前规则,您可以使用这些命令(将它们放在一些脚本中):
通常情况下,您的默认防火墙规则保存在某个文件中(例如,/etc/iptables.rules)。在启动系统命令
iptables-restore </etc/iptables.rules
时执行加载防火墙规则。因此,在使用上述命令删除所有规则后执行相同的命令将导致您要求的“重新加载防火墙”。如果我没记错的话,建议在 ubuntu 指南中设置 iptables 的方法是将其设置为网络脚本的一部分。这意味着没有像 BSD 风格的操作系统那样的 /etc/init.d/iptables 脚本。
在 /etc/init.d/ 上创建一个文件
使文件可执行 chmod +x
在 /etc/rc2.d/ 上创建指向该文件的符号链接
编辑 S80firewall 并添加以下内容
您可以在此文件中添加所有自定义 iptables 规则
现在您可以通过运行 /etc/rc2.d/S80firewall 重新启动防火墙(iptables)(必须是 root)
我遇到过同样的问题。其实里面没有iptables-persistent
/etc/init.d
所以,我创建了 iptables-persistent 文件
/etc/init.d
并在里面写了以下内容:
然后给了 chmod 755 权限。
现在它完美地工作了!希望它可以帮助某人。
如果您将 Ubuntu 服务器作为 VM 来宾运行(例如在 VirtualBox 中),则可能会启用libvirt 。如果是这样,libvirt 包含一些利用 iptables 的内置网络过滤器。这些过滤器可以按照nwfilters的防火墙部分中的描述进行配置。
要禁用 iptables 规则,您需要从 libvirt 中删除所有违规规则,或者如果您不使用 libvirt,则可以禁用它 - 例如安装手动覆盖配置(然后重新启动):