我如何启动/停止Ubuntu上的iptables服务?
我努力了
service iptables stop
但它是给“无法识别的服务” 。
为什么这样做? 有没有其他方法?
我不知道“Ubuntu”,但通常在Linux中,“iptables”不是一项服务 – 它是一个操纵netfilter内核防火墙的命令。 您可以通过将所有标准链上的默认策略设置为“接受”来禁用(或停止)防火墙,并清除规则。
iptables -P INPUT ACCEPT iptables -P OUTPUT ACCEPT iptables -P FORWARD ACCEPT iptables -F
(如果你已经使用过了,你也可能需要刷新其他的表,比如“nat”)
Ubuntu网站上的以下文章介绍了如何设置iptables以用于NetworkManager: https : //help.ubuntu.com/community/IptablesHowTo
你都错了:-)
你正在寻找的命令是:
$ sudo ufw disable
我会首先检查它是否安装(它可能是):
dpkg -l | grep iptables
在Ubuntu上,iptables不是服务。 为了阻止它,你必须做到以下几点:
sudo iptables-save > /root/firewall.rules iptables -X iptables -t nat -F iptables -t nat -X iptables -t mangle -F iptables -t mangle -X iptables -P INPUT ACCEPT iptables -P FORWARD ACCEPT iptables -P OUTPUT ACCEPT
为了恢复您以前的规则:
iptables-restore < /root/firewall.rules
这是从http://www.cyberciti.biz/faq/turn-on-turn-off-firewall-in-linux/取得,并在许多Ubuntu 8.X&9.10安装testing。
iptables是一个命令,它不是一个服务,所以通常不可能使用类似的命令
service iptables start
要么
service iptables stop
为了启动和停止防火墙,但是一些像centos这样的发行版已经安装了一个名为iptables的服务来启动和停止防火墙以及一个configuration文件来configuration它。 无论如何,可以做一个服务来pipe理ipotables编辑或安装这个范围的脚本。 在linux,ubuntu中的所有服务都不是例外,是/etc/init.d文件夹内的可执行脚本,它实现了标准接口(启动,停止,重新启动)。一个可能的脚本如下所示:
#!/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
这个脚本是本教程的一部分,根据上面的脚本,必须将所有configuration防火墙的命令插入到/etc/iptables.conf文件中。 这个脚本必须插入/etc/init.d中的一个名为iptables的文件中,并使其可执行
chmod+x *iptables*
并使用该服务添加到运行级别
update-rc.d iptables defaults
您可以从shell中添加新的规则,这些规则将立即生效,并在服务停止时添加到/etc/iptables.conf(这意味着在系统closures时它们将被保存)。
我希望这会对大家有所帮助。
由于iptables和ufw都是在Linux中pipe理netfilter防火墙的方法,并且由于Ubuntu在默认情况下都是可用的,所以您可以使用它来启动和停止(并pipe理)防火墙规则。
iptables更灵活,但是由于ufw提供了一个非常简单的界面语言,可以使用简单而典型的function:
sudo ufw disable
#禁用防火墙
sudo ufw enable
#启用防火墙
要查看当前的防火墙设置,请使用sudo ufw status verbose
或iptables -L
。
Ubuntu社区文档页面在iptables和UFW有很多更多的信息。
如果我记得正确的话,build议在ubuntu指南中设置iptables的方法是将其设置为networking脚本的一部分。 这意味着没有像BSD风格的操作系统那样的/etc/init.d/iptables脚本。
看起来有几种方式来pipe理Ubuntu中的防火墙,所以你可能有兴趣阅读这个: https : //help.ubuntu.com/community/IptablesHowTo#Configuration%20on%20startup
要删除所有当前的规则,你可以使用这些命令(把它们放在一些脚本中):
iptables -t nat -P PREROUTING ACCEPT iptables -t nat -P POSTROUTING ACCEPT iptables -t nat -P OUTPUT ACCEPT iptables -t nat -F iptables -t nat -X iptables -t mangle -P PREROUTING ACCEPT iptables -t mangle -P INPUT ACCEPT iptables -t mangle -P FORWARD ACCEPT iptables -t mangle -P OUTPUT ACCEPT iptables -t mangle -P POSTROUTING ACCEPT iptables -t mangle -F iptables -t mangle -X iptables -t filter -P INPUT ACCEPT iptables -t filter -P FORWARD ACCEPT iptables -t filter -P OUTPUT ACCEPT iptables -t filter -F iptables -t filter -X
通常情况下,您的默认防火墙规则保存在某个文件中(例如/etc/iptables.rules)。 启动系统命令iptables-restore </etc/iptables.rules
执行加载防火墙规则。 因此,使用上述命令删除所有规则后执行相同的命令将导致您要求的“重新加载防火墙”。
在/etc/init.d/创build一个文件
touch fw.rc
使文件可执行文件chmod + x
在/etc/rc2.d/上创build一个符号链接
ln -s /etc/init.d/fw.rc S80firewall
编辑S80firewall并添加以下内容
iptables --flush iptables --table nat --flush iptables --delete-chain iptables --table nat --delete-chain echo "1" > /proc/sys/net/ipv4/ip_forward iptables -F
您可以将所有的自定义iptables规则添加到这个文件
现在你可以通过运行/etc/rc2.d/S80firewall重启防火墙(iptables)(必须是root)
我遇到过同样的问题。 实际上,/etc/ /etc/init.d
没有iptables-persistent
所以,我在/etc/init.d
创build了iptables-persistent文件
nano /etc/init.d/iptables-persistent
并在里面写下了:
#!/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 FZX; 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 FZX; 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
然后给予chmod 755许可。
chmod 755 /etc/init.d/iptables-persistent
现在它完美的工作! 希望它可以帮助别人。
如果您将Ubuntu服务器作为VM guest(例如在VirtualBox中)运行,那么可能会启用libvirt 。 如果libvirt包含一些使用iptables的内置networkingfilter。 这些filter可以按照nwfilters的防火墙部分中的描述进行configuration。
要禁用iptables规则,您需要从libvirt中删除所有违规规则,或者如果您不使用libvirt,则可以禁用libvirt,例如安装手动覆盖configuration(然后重新启动):
sudo bash -c 'echo "manual" > /etc/init/libvirt-bin.override'
您正在使用适用于RedHat和CentOS的命令,而不是Ubuntu或Debian。
http://www.cyberciti.biz/faq/ubuntu-server-disable-firewall/