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

我读了这篇文章,但这并不管用,因为在CentOS 7操作系统上没有iptables保存命令。

有人告诉我上面的URL对CentOS 7无效。我应该跟着这个走。 但是这篇文章对我来说并不清楚我需要执行什么命令。

我还发现

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

但这在重启后就不存在了。

所以我怎么能打开端口,使它幸存重启?


当前回答

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

步骤1 => install epel repo

Yum安装epel-release

步骤2 => install iptables service . sh

Yum安装iptables-services

步骤3 =>停止防火墙服务

Systemctl停止防火墙

步骤4 =>启动时禁用防火墙服务

Systemctl禁用防火墙

步骤5 =>启动iptables服务

Systemctl启动iptables

步骤6 => enable iptables on startup

Systemctl启用iptables

最后,您现在可以在/etc/sysconfig/iptables编辑您的iptables配置。

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

做像更老的centos与相同的功能像防火墙。

其他回答

防火墙对于iptables老手来说有点不直观。对于那些喜欢使用iptables驱动的防火墙,并在易于配置的树中使用类似iptables的语法的人,可以尝试用fwtree替换firewall: 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 

使用以下命令查看开放端口:

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

Fedora是通过iptables实现的

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

似乎有效

虽然ganeshragav和Sotsir提供了正确且直接适用的方法,但值得注意的是,您可以将自己的服务添加到/etc/firewall /services.为了获得灵感,请查看/usr/lib/firewall /services/,其中包含firewall的预定义服务。

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

例如,可以创建/etc/firewall /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>

(有关语法的信息,请参阅do man firewall .service。)

一旦创建了这个文件,您可以firewall-cmd—reload使其可用,然后永久地将其添加到某个带

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

跟着firewall-cmd -reload使它立即激活。

上面的答案是有用的,但我在迈克尔·汉普顿(Michael Hampton)对一个相关问题的回答中发现了一些更优雅的东西。firewall-cmd的“new”(firewall -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的评论。显然firewall-cmd -reload可能无法正常工作在某些情况下,规则已被删除。在这种情况下,他建议重新启动防火墙服务:

$ systemctl restart firewalld