AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / server / 问题 / 129086
In Process
neha soni
neha soni
Asked: 2010-04-04 03:15:41 +0800 CST2010-04-04 03:15:41 +0800 CST 2010-04-04 03:15:41 +0800 CST

如何在 Ubuntu 上启动/停止 iptables?

  • 772

如何在 Ubuntu 上启动/停止 iptables 服务?

我努力了

 service iptables stop

但它正在提供“无法识别的服务”。

为什么要这样做?还有其他方法吗?

ubuntu iptables
  • 12 12 个回答
  • 697732 Views

12 个回答

  • Voted
  1. JMusgrove
    2010-04-04T09:36:09+08:002010-04-04T09:36:09+08:00

    我不知道“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

    • 93
  2. Frederik
    2012-08-16T10:40:00+08:002012-08-16T10:40:00+08:00

    你们都错了:-)

    您正在寻找的命令是:

    $ sudo ufw disable
    
    • 52
  3. Embreau
    2010-04-05T23:46:47+08:002010-04-05T23:46:47+08:00

    我会首先检查它是否安装了(可能是):

    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 安装上进行了测试。

    • 38
  4. puzzle-it
    2011-10-17T23:53:32+08:002011-10-17T23:53:32+08:00

    iptables 是一个命令它不是一个服务,所以通常不可能使用类似的命令

    service iptables start
    

    或者

    service iptables stop
    

    为了启动和停止防火墙,但是像centos这样的一些发行版已经安装了一个名为iptables的服务来启动和停止防火墙以及一个配置文件来配置它。无论如何,可以提供服务来管理 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 
    

    这个脚本是本教程的一部分,所有配置防火墙的命令都必须根据上面的脚本插入到 /etc/iptables.conf 文件中。必须将此脚本插入到 /etc/init.d 中名为iptables的文件中,并使用以下命令使其可执行

    chmod+x *iptables* 
    

    并使用将服务添加到运行级别

    update-rc.d iptables defaults
    

    您可以从 shell 添加新规则,这些规则将立即生效,并在服务停止时添加到 /etc/iptables.conf(这意味着它们将在系统关闭时确定保存)。

    我希望这对每个人都有帮助。

    • 19
  5. belacqua
    2013-07-03T14:37:18+08:002013-07-03T14:37:18+08:00

    因为 iptables 和 ufw 都是在 Linux 中管理netfilter防火墙的方法,并且因为两者都在 Ubuntu 中默认可用,所以您可以使用其中任何一个来启动和停止(和管理)防火墙规则。

    iptables 更灵活,但是因为 ufw 提供了一种非常简单的界面语言,您可以使用简单而典型的功能:

    sudo ufw disable# 禁用防火墙

    sudo ufw enable # 开启防火墙

    要查看当前防火墙设置sudo ufw status verbose,请使用 或iptables -L。

    iptables和UFW上的 Ubuntu 社区文档页面有更多信息。

    • 6
  6. Powerman
    2010-04-05T20:32:28+08:002010-04-05T20:32:28+08:00

    看起来有几种方法可以在 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时执行加载防火墙规则。因此,在使用上述命令删除所有规则后执行相同的命令将导致您要求的“重新加载防火墙”。

    • 4
  7. xenoterracide
    2010-04-04T15:05:47+08:002010-04-04T15:05:47+08:00

    如果我没记错的话,建议在 ubuntu 指南中设置 iptables 的方法是将其设置为网络脚本的一部分。这意味着没有像 BSD 风格的操作系统那样的 /etc/init.d/iptables 脚本。

    • 2
  8. jerichorivera
    2011-10-18T00:37:25+08:002011-10-18T00:37:25+08:00

    在 /etc/init.d/ 上创建一个文件

    touch fw.rc
    

    使文件可执行 chmod +x

    在 /etc/rc2.d/ 上创建指向该文件的符号链接

    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)

    • 2
  9. capitainabloc
    2015-04-18T02:39:28+08:002015-04-18T02:39:28+08:00

    我遇到过同样的问题。其实里面没有iptables-persistent/etc/init.d

    所以,我创建了 iptables-persistent 文件/etc/init.d

    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 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
    

    然后给了 chmod 755 权限。

    chmod 755 /etc/init.d/iptables-persistent
    

    现在它完美地工作了!希望它可以帮助某人。

    • 2
  10. Pierz
    2016-04-02T06:38:44+08:002016-04-02T06:38:44+08:00

    如果您将 Ubuntu 服务器作为 VM 来宾运行(例如在 VirtualBox 中),则可能会启用libvirt 。如果是这样,libvirt 包含一些利用 iptables 的内置网络过滤器。这些过滤器可以按照nwfilters的防火墙部分中的描述进行配置。

    要禁用 iptables 规则,您需要从 libvirt 中删除所有违规规则,或者如果您不使用 libvirt,则可以禁用它 - 例如安装手动覆盖配置(然后重新启动):

    sudo bash -c 'echo "manual" > /etc/init/libvirt-bin.override'
    
    • 2

相关问题

  • 如何在 Ubuntu 上设置简单的防火墙?

  • 设置没有密码的用户

  • 在 Ubuntu 上设置电子邮件服务器

  • 保护新的 Ubuntu 服务器 [关闭]

  • (软)Ubuntu 7.10 上的 RAID 6,我应该迁移到 8.10 吗?

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    新安装后 postgres 的默认超级用户用户名/密码是什么?

    • 5 个回答
  • Marko Smith

    SFTP 使用什么端口?

    • 6 个回答
  • Marko Smith

    从 IP 地址解析主机名

    • 8 个回答
  • Marko Smith

    如何按大小对 du -h 输出进行排序

    • 30 个回答
  • Marko Smith

    命令行列出 Windows Active Directory 组中的用户?

    • 9 个回答
  • Marko Smith

    什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同?

    • 3 个回答
  • Marko Smith

    如何确定bash变量是否为空?

    • 15 个回答
  • Martin Hope
    MikeN 在 Nginx 中,如何在维护子域的同时将所有 http 请求重写为 https? 2009-09-22 06:04:43 +0800 CST
  • Martin Hope
    Tom Feiner 如何按大小对 du -h 输出进行排序 2009-02-26 05:42:42 +0800 CST
  • Martin Hope
    0x89 bash中的双方括号和单方括号有什么区别? 2009-08-10 13:11:51 +0800 CST
  • Martin Hope
    Kyle Brandt IPv4 子网如何工作? 2009-08-05 06:05:31 +0800 CST
  • Martin Hope
    Noah Goodrich 什么是 Pem 文件,它与其他 OpenSSL 生成的密钥文件格式有何不同? 2009-05-19 18:24:42 +0800 CST
  • Martin Hope
    Brent 如何确定bash变量是否为空? 2009-05-13 09:54:48 +0800 CST
  • Martin Hope
    cletus 您如何找到在 Windows 中打开文件的进程? 2009-05-01 16:47:16 +0800 CST

热门标签

linux nginx windows networking ubuntu domain-name-system amazon-web-services active-directory apache-2.4 ssh

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve