【问题标题】:Open firewall port on CentOS 7 [closed]在 CentOS 7 上打开防火墙端口 [关闭]
【发布时间】:2014-09-03 22:13:50
【问题描述】:

我使用的是 CentOS 7,我必须确保端口 2888 和 3888 是开放的。

我阅读了this article 但这不起作用,因为在 CentOS 7 操作系统上没有 iptables save 命令。

有人告诉我上面的 URL 对 CentOS 7 无效。我应该关注this。 但是这篇文章我不清楚我需要执行什么命令。

我也找到了

firewall-cmd --zone=public --add-port=2888/tcp 

但这不会在重新启动后继续存在。

那么我怎样才能打开端口并使其在重启后仍然有效?

【问题讨论】:

标签: centos port firewall


【解决方案1】:

使用此命令查找您的活动区域:

firewall-cmd --get-active-zones

它将显示 public、dmz 或其他内容。您应该只申请所需的区域。

在公开尝试的情况下:

firewall-cmd --zone=public --add-port=2888/tcp --permanent

然后记得重新加载防火墙以使更改生效。

firewall-cmd --reload

否则,请用 public 代替您的区域,例如,如果您的区域是 dmz:

firewall-cmd --zone=dmz --add-port=2888/tcp --permanent

【讨论】:

  • 我使用了这个命令,但它对我不起作用,是什么,将--zone=dmz 更改为--zone=public
  • 是的,使用 public 也很有用。请检查 Sotsir 的以下命令以进行更正。
  • 这里的前两个答案都没有真正解释--permanent 做了什么,他们只是说去做。如果解释了答案的工作原理,两者都会是更完整和更有用的答案。来自 OP 的资源之一:“可以通过添加 --permanent 选项使规则永久化[...]。如果规则没有永久化,则需要在每次收到启动后应用它们,重新启动或使用 D-BUS 从 firewalld 重新加载消息。”
  • 请不要盲目地启用所有区域的端口,直到找到正确的端口。使用firewall-cmd --get-active-zones 找出系统上使用的区域。另外,man firewall-cmd。
  • 在阅读 fedora documentation 时,它声明了 The --permanent option needs to be the first option for all permanent calls。使用上述方法我没有收到任何错误,但我不确定这是否会给其他人带来问题。
【解决方案2】:

ganeshragav 的答案是正确的,但是 知道你可以使用也很有用:

firewall-cmd --permanent --zone=public --add-port=2888/tcp 

但如果是已知服务,则可以使用:

firewall-cmd --permanent --zone=public --add-service=http 

然后重新加载防火墙

firewall-cmd --reload

[修改答案以反映 Martin Peter 的评论,原始答案在命令行末尾有 --permanent]

【讨论】:

  • 我不会依赖在语句末尾使用--permanent 选项。文档明确指出,它应该是第一个选项。
  • @MartinPeter man firewall-cmd 在我的机器(Fedora 21)上没有给出这样的指示。
  • @JonathonReinhart 我指的是Fedora's FirewallD Wiki 声明:The --permanent option needs to be the first option for all permanent calls.
  • 它让我的 firewallD 没有运行
【解决方案3】:

CentOS (RHEL) 7 已将防火墙更改为使用 firewall-cmd,它具有类似于 Windows 版本的公共、家庭和专用网络的区域概念。您应该查看here 以确定您认为应该使用哪一个。 EL7 默认使用public,这就是我下面的示例所使用的。

您可以使用firewall-cmd --list-all 检查您正在使用的区域,并使用firewall-cmd --set-default-zone=<zone> 进行更改。

然后您将知道允许服务(或端口)在哪个区域:

firewall-cmd --permanent --zone=<zone> --add-service=http

firewall-cmd --permanent --zone=<zone> --add-port=80/tcp

你可以通过运行检查端口是否真的打开了:

firewall-cmd --zone=<zone> --query-port=80/tcp

firewall-cmd --zone=<zone> --query-service=http

根据documentation

在永久模式下更改防火墙设置时,您的 选择只会在您重新加载防火墙或 系统重新启动。

您可以使用以下命令重新加载防火墙设置:firewall-cmd --reload

【讨论】:

    【解决方案4】:

    Fedora,通过 iptables 完成的

    sudo iptables -I INPUT -p tcp --dport 3030 -j ACCEPT
    sudo service iptables save
    

    似乎有效

    【讨论】:

      【解决方案5】:

      要查看打开的端口,请使用以下命令。

      firewall-cmd --list-ports
      

      我们使用以下内容查看端口打开的服务。

      firewall-cmd --list-services
      

      我们使用以下内容查看端口打开的服务,并查看打开端口

      firewall-cmd --list-all
      
      要将服务添加到防火墙,我们使用以下命令,在这种情况下,服务将在防火墙中使用任何端口打开。
      firewall-cmd --add-services=ntp 
      

      要永久打开我们使用以下命令。

      firewall-cmd —add-service=ntp --permanent 
      

      要添加端口,请使用以下命令

      firewall-cmd --add-port=132/tcp  --permanent
      
      要运行防火墙必须使用以下命令重新加载。
      firewall-cmd --reload
      

      亚阿里

      【讨论】:

      • firewall-cmd -add-service = ntp -permanent“应该将”防火墙-cmd --add-service = ntp -permanent“ span>更改为”firewall-cmd -add-service“
      【解决方案6】:

      虽然ganeshragavSotsir 提供了正确且直接适用的方法,但请注意,您可以将自己的服务添加到/etc/firewalld/services。如需灵感,请查看/usr/lib/firewalld/services/,firewalld 的预定义服务所在的位置。

      这种方法的优点是稍后您会知道为什么这些端口是打开的,正如您在服务文件中描述的那样。此外,您现在可以将其应用于任何区域,而不会出现拼写错误。此外,对服务的更改不需要单独应用于所有区域,而只需应用于服务文件。

      例如,您可以创建/etc/firewalld/services/foobar.xml

      <?xml version="1.0" encoding="utf-8"?>
      <service>
        <short>FooBar</short>
        <description>
          This option allows you to create FooBar connections between
          your computer and mobile device. You need to have FooBar
          installed on both sides for this option to be useful.
        </description>
        <port protocol="tcp" port="2888"/>
        <port protocol="tcp" port="3888"/>
      </service>
      

      (有关语法的信息,请执行man firewalld.service。)

      创建此文件后,您可以firewall-cmd --reload 使其可用,然后将其永久添加到某个区域

      firewall-cmd --permanent --zone=<zone> --add-service=foobar
      

      跟随firewall-cmd --reload 使其立即激活。

      【讨论】:

        【解决方案7】:

        要查看打开的端口,请使用以下命令:

        firewall-cmd --list-ports
        

        我们使用以下来查看端口开放的服务:

        firewall-cmd --list-services
        

        我们使用以下来查看端口开放的服务并查看开放端口:

        firewall-cmd --list-all
        

        要将服务添加到防火墙,我们使用以下命令,在这种情况下,服务将使用防火墙中的任何端口打开:

        firewall-cmd --add-services=ntp 
        

        为了使这项服务永久开放,我们使用以下命令:

        firewall-cmd -add-service=ntp --permanent 
        

        要添加端口,请使用以下命令:

        firewall-cmd --add-port=132/tcp  --permanent
        

        【讨论】:

        • 别忘了使用 firewall-cmd --reload 重新加载
        【解决方案8】:

        这里的最佳答案有效,但我在Michael Hampton's answer 中发现了一些更优雅的相关问题。 firewall-cmd 的“新”(firewalld-0.3.9-11+) --runtime-to-permanent 选项可让您创建运行时规则并在将它们永久化之前对其进行测试:

        $ firewall-cmd --zone=<zone> --add-port=2888/tcp
        <Test it out>
        $ firewall-cmd --runtime-to-permanent
        

        或恢复仅运行时的更改:

        $ firewall-cmd --reload
        

        另见Antony Nguyen's comment。显然 firewall-cmd --reload 在某些规则已被删除的情况下可能无法正常工作。在这种情况下,他建议重新启动 firewalld 服务:

        $ systemctl restart firewalld
        

        【讨论】:

          【解决方案9】:

          如果您在 Centos 7 防火墙中允许多个端口,那么我们可以使用以下命令。

          #firewall-cmd --add-port={port number/tcp,port number/tcp} --permanent
          
          #firewall-cmd --reload
          
          
          And check the Port opened or not after reloading the firewall.
          
          
          #firewall-cmd --list-port
          
          
          For other configuration [Linuxwindo.com][1]
          

          【讨论】:

            【解决方案10】:

            如果你熟悉centos 6或更早的iptables服务,你仍然可以通过手动安装来使用iptables服务:

            步骤 1 => 安装 epel repo

            yum install epel-release

            第 2 步 => 安装 iptables 服务

            yum 安装 iptables-services

            第 3 步 => 停止 firewalld 服务

            systemctl 停止防火墙

            第 4 步 => 启动时禁用 firewalld 服务

            systemctl 禁用防火墙

            步骤 5 => 启动 iptables 服务

            systemctl 启动 iptables

            第 6 步 => 在启动时启用 iptables

            systemctl 启用 iptables

            你现在终于可以在 /etc/sysconfig/iptables 编辑你的 iptables 配置了。

            所以 -> 编辑规则 -> 重新加载/重新启动。

            喜欢具有相同功能的旧版centos,如firewalld。

            【讨论】:

              【解决方案11】:

              Centos 7 防火墙-cmd 中你好。 是的,如果你使用是正确的 防火墙-cmd --zone=public --add-port=2888/tcp 但是如果你重新加载防火墙 防火墙-cmd --reload

              您的配置不会被保存

              您需要添加密钥

              firewall-cmd --permanent --zone=public --add-port=2888/tcp

              【讨论】:

                【解决方案12】:

                Firewalld 对于 iptables 老手来说有点不直观。对于那些喜欢在易于配置的树中使用类似 iptables 语法的 iptables 驱动的防火墙的人,请尝试将 firewalld 替换为 fwtree: https://www.linuxglobal.com/fwtree-flexible-linux-tree-based-firewall/ 然后执行以下操作:

                 echo '-p tcp --dport 80 -m conntrack --cstate NEW -j ACCEPT' > /etc/fwtree.d/filter/INPUT/80-allow.rule
                 systemctl reload fwtree 
                

                【讨论】:

                  猜你喜欢
                  • 2023-04-01
                  • 2021-04-13
                  • 2015-06-24
                  • 1970-01-01
                  • 2015-06-17
                  • 1970-01-01
                  • 2021-01-11
                  • 1970-01-01
                  • 1970-01-01
                  相关资源
                  最近更新 更多