tl;dr

firewall-cmd --permanent --zone=public --add-port=2888/tcp
firewall-cmd --reload #重新载入服务

永久配置firewalld开启端口。

之前的一些坑

之前的一篇文章CentOS 7部署Node.js+MongoDB:在VPS上从安装到Hello world中,讲到了CentOS开启端口用这个命令

firewall-cmd --add-port=3000/tcp

这样是没错,开启了端口,但是后面发现这个端口会莫名其妙的被关闭

Google一番后了解到这样添加端口是运行时配置(Runtime configuration),在重载或重启firewalld后,这个配置就失效了。

自动关闭原因

CentOS 7 采用了firewalld作为防火墙服务,在Red Hat官方文档的Security Guide中有介绍firewalld

The dynamic firewall daemon firewalld provides a dynamically managed firewall with support for network “zones” to assign a level of trust to a network and its associated connections and interfaces. It has support for IPv4 and IPv6 firewall settings. It supports Ethernet bridges and has a separation of runtime and permanent configuration options. It also has an interface for services or applications to add firewall rules directly.

动态防火墙守护进程firewalld提供一个动态管理的防火墙,支持网络“区域”(zones),以用来给一个网络以及其关联的链接和接口分配一个信任层级。firewalld支持IPv4跟IPv6的防火墙设置。它还支持以太网桥,并且有运行时配置选项跟永久配置选项(runtime and permanent configuration options),二者相互分离。并且firewalld为服务或应用直接添加防火墙规则提供了接口。

有关Network Zones的概念这边不细讲,参照Security Guide。

firewalld有个图形化配置工具firewall-config,还有个命令行客户端,就是firewall-cmd了。

我们暂时还没用到图形化工具,所以这边就说一下firewall-cmd

Security Guide中关于firewall-cmd的介绍:

A command line client, firewall-cmd, is provided. It can be used to make permanent and non-permanent runtime changes as explained in man firewall-cmd(1). Permanent changes need to be made as explained in the firewalld(1) man page. Note that the firewall-cmd command can be run by the root user and also by an administrative user, in other words, a member of the wheel group. In the latter case the command will be authorized via the polkit mechanism.

具体就不翻译了,大概是说

firewall-cmd可以永久或非永久地改变配置,永久配置需要如man page中解释的那样改变(日了狗了)。

于是又去翻了翻firewalld(1)的man page,里面有两段关于Runtime configuration跟Permanent configuration的解释。

Runtime configuration

Runtime configuration is the actual active configuration and is not permanent. After

reload/restart of the service or a system reboot, runtime settings will be gone if they

haven’t been also in permanent configuration.

Permanent configuration

The permanent configuration is stored in config files and will be loaded and become new

runtime configuration with every machine boot or service reload/restart.

运行时配置

运行时配置是实际上启用了但不是永久的配置。在服务重载/重启或系统重启之后,运行时的设置如果不存在永久配置中,就会失效。

永久配置

永久配置被保存在配置文件中,随着每次机器启动或服务重载/重启,永久配置都会被载入,变成新的运行时配置。

永久开启端口

好了,说了那么多,应该是理清楚端口被自动关闭的原委了,那怎么永久开启端口呢,万能的Security Guide中给出了答案

The rules can be made permanent by adding the –permanent option using the firewall-cmd –permanent –direct command or by modifying /etc/firewalld/direct.xml.

只要添加规则时加上–permanent参数或者修改/etc/firewalld/direct.xml就行了。

所以只需要两条命令

firewall-cmd --permanent --zone=public --add-port=2888/tcp
firewall-cmd --reload #重新载入服务

参考

CentOS 7 防止端口自动关闭的更多相关文章

  1. CentOS 下做端口映射/端口转发

    CentOS 下做端口映射/端口转发==[实现目标]==================[服务器A]有2块网卡,一块接内网,一块接外网,[服务器B]只有一块内网网卡:访问[服务器A]的7890端口跳转 ...

  2. centos添加开放端口

    Centos 开放 80 端口 firewall-cmd --zone=public --add-port=80/tcp --permanent 重启防火墙 firewall-cmd --reload ...

  3. CentOS 7 防火墙端口配置

    CentOS 7 防火墙端口配置查看防火墙是否开启systemctl status firewalld 若没有开启则开启systemctl start firewalld 查看所有开启的端口firew ...

  4. Centos安装nmap端口查看工具

    CentOS安装nmap端口查看工具 一.安装nmap yum install nmap    #输入y安装 二.使用nmap      nmap localhost    #查看主机当前开放的端口  ...

  5. Linux CentOS 7 防火墙/端口设置

    CentOS升级到7之后用firewall代替了iptables来设置Linux端口, 下面是具体的设置方法: []:选填 <>:必填 [<zone>]:作用域(block.d ...

  6. Linux CentOS 7 防火墙/端口设置【转发】

    CentOS升级到7之后用firewall代替了iptables来设置Linux端口, 下面是具体的设置方法: []:选填 <>:必填 [<zone>]:作用域(block.d ...

  7. Centos 7 开启端口

    CentOS 7 默认没有使用iptables,所以通过编辑iptables的配置文件来开启80端口是不可以的 CentOS 7 采用了 firewalld 防火墙 如要查询是否开启80端口则: 1 ...

  8. [Linux.NET] CentOS 开启/关闭端口

    最近一直在学习研究mvc网站部署到Linux上,Web服务器用的Jexus,linux版本是阿里云提供的centos.一个服务器又要放多个独立网站,对于学习阶段使用多个端口标识还是挺方便的,本文记录下 ...

  9. Centos 开放80端口

    一.添加规则 #/sbin/iptables -I INPUT -p tcp --dport 80 -j ACCEPT #/sbin/iptables -I INPUT -p tcp --dport ...

随机推荐

  1. 关于清除arp 缓存的那点事儿

    在Linux下,清除arp缓存表,例如: arp -d 10.0.3.6 我们可以用上面这条命令清除某一条记录,也可以用 arp -n |awk '/^[1-9]/{print "arp - ...

  2. MVC中使用Entity Framework 基于方法的查询学习笔记 (二)

    解释,不解释: 紧接上文,我们在Visual Studio2012中看到系统为我们自动创建的视图(View)文件Index.cshtml中,开头有如下这句话: @model IEnumerable&l ...

  3. 收集免费可用稳定的vpn

    收集免费可用稳定的vpn,经常用到,所以记录一下,方便自己不备之需. 1,https://www.lvbeivpn.cc/share.shtml?id=a3bd9527225d4746bb3a5761 ...

  4. 【转】使用Python matplotlib绘制股票走势图

    转载出处 一.前言 matplotlib[1]是著名的python绘图库,它提供了一整套绘图API,十分适合交互式绘图.本人在工作过程中涉及到股票数据的处理如绘制K线等,因此将matplotlib的使 ...

  5. 基础知识《十》unchecked异常和checked异常

    运行时异常在运行期间才能被检查出来,一般运行期异常不需要处理.也称为unchecked异常.Checked异常在编译时就能确定,Checked异常需要自己处理. checked 异常也就是我们经常遇到 ...

  6. linux系统root用户忘记密码的重置方法

    如果不小心忘记了新安装的lCentOS7的root密码,现在将找回过程分享给大家. 1.首先,在启动grub菜单,选择编辑选项启动: 2.然后,按e 进入编辑模式: 3.将'linux 16'行'ro ...

  7. C++小结

    1.输入:cin>>变量名: 输出:cout<<变量名<<endl: 2.类 public 公有,此类及其他类中使用   private 私有,只能在本类中使用   ...

  8. Windows 网络编程

    网络编程 API ,失败返回 -,错误代码 WSASYSNOTREADY 表示基础网络子系统没有准备好网络通行,WSAVERNOTSUPPORTED 表示 Socket 版本不支持,WSAEINPRO ...

  9. PHP新手常见的一些不好习惯(抄的 有待理解)

    1.不写注释(是个好习惯,不过也没必要每个语句都要写) 2.不使用可以提高生产效率的IDE工具 3.不使用版本控制 4.不按照编程规范写代码 5.不使用统一的方法 6.编码前不去思考和计划 7.在执行 ...

  10. word

    # word中向下箭头 原文:http://jingyan.baidu.com/article/c1465413b0a9c70bfcfc4cbc.html 使用替换功能,把^l(l是小写的L)替换为^ ...