【问题标题】:How can I use iptables on centos 7? [closed]如何在 centos 7 上使用 iptables? [关闭]
【发布时间】:2014-09-05 12:47:12
【问题描述】:

我以最少的配置(操作系统 + 开发工具)安装了 CentOS 7。我正在尝试为httpd 服务打开 80 端口,但是我的 iptables 服务有问题……它有什么问题?我究竟做错了什么?

# ifconfig/sbin/service iptables save
bash: ifconfig/sbin/service: No such file or directory


# /sbin/service iptables save
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

# sudo service iptables status
Redirecting to /bin/systemctl status  iptables.service
iptables.service
   Loaded: not-found (Reason: No such file or directory)
   Active: inactive (dead)

# /sbin/service iptables save
The service command supports only basic LSB actions (start, stop, restart, try-restart, reload, force-reload, status). For other actions, please try to use systemctl.

# sudo service iptables start
Redirecting to /bin/systemctl start  iptables.service
Failed to issue method call: Unit iptables.service failed to load: No such file or directory.

【问题讨论】:

  • 试试这个 /etc/init.d/iptables 保存
  • 我得到了答案here。 RHEL 7 使用 firewalld 代替 iptables
  • IMO firewalld 比 iptables 更加用户友好。首先安装并启动firewalld服务sudo yum install -y firewalld && sudo systemctl start firewalld。然后添加服务 HTTP sudo firewall-cmd --permanent --add-service=http(如果需要,还可以添加服务 https 以打开端口 443)。最后将新配置加载到 firewalld sudo firewall-cmd --reload

标签: networking centos iptables systemd


【解决方案1】:

上个月我尝试在 LXC VM 容器上配置 iptables,但每次重启后 iptables 配置都不会自动加载。

我让它工作的唯一方法是运行以下命令:

yum -y 安装 iptables-services; systemctl 禁用防火墙; systemctl 掩码防火墙;服务 iptables 重启;服务 iptables 保存

【讨论】:

  • 我相信这是因为容器不会持久保存任何东西,除非它位于主机操作系统的持久卷中。大多数容器操作系统和配置通常不在卷中,因此每次重新启动容器时都会重置。
【解决方案2】:

如果您这样做,并且您使用的是 fail2ban,则需要启用正确的过滤器/操作:

将以下行放入/etc/fail2ban/jail.d/sshd.local

[ssh-iptables]
enabled  = true
filter   = sshd
action   = iptables[name=SSH, port=ssh, protocol=tcp]
logpath  = /var/log/secure
maxretry = 5
bantime = 86400

启用并启动fail2ban:

systemctl enable fail2ban
systemctl start fail2ban

参考:http://blog.iopsl.com/fail2ban-on-centos-7-to-protect-ssh-part-ii/

【讨论】:

    【解决方案3】:

    此外,在运行systemctl mask firewalld 命令后,您还应该能够对 ip6tables 执行相同的操作:

        systemctl start ip6tables.service
        systemctl enable ip6tables.service
    

    【讨论】:

      【解决方案4】:

      将IPtables配置放在传统文件中,开机后会加载:

      /etc/sysconfig/iptables

      【讨论】:

        【解决方案5】:

        在 RHEL 7 / CentOS 7 中,引入了firewalld 来管理 iptables。恕我直言,firewalld 更适合工作站而不是服务器环境。

        可以回到更经典的 iptables 设置。首先,停止并屏蔽firewalld服务:

        systemctl stop firewalld
        systemctl mask firewalld
        

        然后,安装 iptables-services 包:

        yum install iptables-services
        

        在启动时启用服务:

        systemctl enable iptables
        

        管理服务

        systemctl [stop|start|restart] iptables
        

        保存防火墙规则的方法如下:

        service iptables save
        

        /usr/libexec/iptables/iptables.init save
        

        【讨论】:

        • 它没有保存 iptables。重新启动服务器会丢失所有更改。
        • 在我的系统上,iptables 被正确保存。您是否确保 iptables 服务在启动时启动?你可以通过运行'systemctl enable iptables'来做到这一点
        • 您可能还运行了 firewalld,它会在列表中添加额外的 iptables 规则(每次重新启动系统时)。要停止 firewalld 运行“systemctl mask firewalld”
        • 按照@TroodoN-Mike 的建议,将有关屏蔽 firewalld 的信息添加到答案中
        • @Sgaduuw 您能否详细说明为什么您认为 fitewalld 不/不太适合服务器?
        【解决方案6】:

        我修改了/etc/sysconfig/ip6tables-config文件更改:

        IP6TABLES_SAVE_ON_STOP="no"
        

        收件人:

        IP6TABLES_SAVE_ON_STOP="yes"
        

        还有这个:

        IP6TABLES_SAVE_ON_RESTART="no"
        

        收件人:

        IP6TABLES_SAVE_ON_RESTART="yes"
        

        这似乎保存了我通过重新启动使用 iptables 命令所做的更改。

        【讨论】:

          【解决方案7】:

          试试下面的命令iptables-save

          【讨论】:

          • man iptables-save - 将 iptables 规则转储到标准输出。
          • 那么我们可以使用iptables-save > /etc/sysconfig/iptables
          【解决方案8】:

          RHEL and CentOS 7 use firewall-cmd instead of iptables。你应该使用那种命令:

          # add ssh port as permanent opened port
          firewall-cmd --zone=public --add-port=22/tcp --permanent
          

          然后,您可以重新加载规则以确保一切正常

          firewall-cmd --reload
          

          这比使用 iptable-save 更好,尤其是如果您打算使用 lxc 或 docker 容器。启动 docker 服务会添加一些 iptable-save 命令会提示的规则。如果你保存结果,你会有很多不应该保存的规则。因为 docker 容器可以在下次重启时更改它们的 IP 地址。

          具有永久选项的防火墙-cmd 更好。

          检查“man firewall-cmd”或check the official firewalld docs 以查看选项。有很多选项可以检查区域、配置、工作方式……手册页真的很完整。

          我强烈建议从 Centos 7 开始不要使用 iptables-service

          【讨论】:

          • firewall-cmd --reload 不起作用。我必须使用“systemctl restart firewalld”重新启动才能使更改生效。
          • "因为 firewalld 是动态的,所以可以随时对其配置进行更改,并立即实施。防火墙的任何部分都不需要重新加载,因此不会意外中断现有网络连接" -- 来自the official firewalld docs
          【解决方案9】:

          我遇到了重启无法启动 iptables 的问题。

          这解决了它:

          yum install iptables-services
          systemctl mask firewalld
          systemctl enable iptables
          systemctl enable ip6tables
          systemctl stop firewalld
          systemctl start iptables
          systemctl start ip6tables
          

          【讨论】:

          • 在 CentOS 7 上不起作用:# systemctl start iptables 给出 Failed to start iptables.service: Unit not found.# systemctl start ip6tables 给出 Failed to start ip6tables.service: Unit not found.
          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2015-12-11
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2016-04-23
          • 1970-01-01
          相关资源
          最近更新 更多