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  #重新载入服务

参考

  • 4.5. USING FIREWALLS
  • CentOS 7.0 - man page for firewalld (centos section 1) - Unix & Linux Commands
  • centos 7 - open firewall port - Stack Overflow

转载于:https://www.cnblogs.com/chuchencheng/p/5897636.html

CentOS 7 防止端口自动关闭相关推荐

  1. linux7.5开放端口,Centos/linux开放端口

    在linux上部署tomcat发现外部无法访问可以通过两种方式解决: 1.关闭防火墙 service iptables stop(不推荐) 2.修改相关文件,开放需要开放的端口 (1)通过命令vi / ...

  2. CentOS如何查看端口是被哪个应用/进程占用

    CentOS如何查看端口是被哪个应用/进程占用 有时启动应用时会发现端口已经被占用,或者是感觉有些端口自己没有使用却发现是打开的.这时我们希望知道是哪个应用/进程在使用该端口. CentOS下可以用n ...

  3. linux 打开端口1935,CentOS服务器开放端口

    拿到服务器之后接着之前的通信步骤进行,却发现怎么也连接不上.最后发现是因为服务器端的端口5000没有开放.下面记录一下开放端口的过程. 使用命令 netstat -anp 查看端口开放情况.如果显示命 ...

  4. centos查询mysql端口被占用_centos查看端口占用情况

    1 说明 linux经常会查询端口是否被占用,被哪些程序占用. 这里使用netstat命令查看centos系统的端口占用情况. 2 步骤 (1)查看全部端口占用情况 执行命令: netstat -tu ...

  5. CentOS 打开3306端口

    CentOS 打开3306端口 在CentOS系统中防火墙默认是阻止3306端口的,我们要是想访问mysql数据库,我们需要这个端口,命令如下: 1 /sbin/iptables -I INPUT - ...

  6. 设置CentOS防火墙开放端口

    在我们使用CentOS系统的时候,CentOS防火墙有时是需要改变设置的.CentOS防火墙默认是打开的,设置CentOS防火墙开放端口方法如下: 开启防火墙     #/etc/rec.d/init ...

  7. centos怎么查看端口是否开放

    centos怎么查看端口是否开放 方法:1.利用telnet命令,语法为"telnet ip port":2.利用nc命令,语法为"telnet ip port" ...

  8. Centos关闭111端口

    Centos关闭111端口 检查111端口是否在监听,-ntlp(TCP), -nulp(UDP) # netstat -ntlp # netstat -nulp 关闭rpcbind.socket和r ...

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

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

最新文章

  1. 用计算机采集卡检测压力传感器信号故障,计算机联网检测系统应用研究
  2. java程序运行结果题_(Java程序设计)试题
  3. ubuntu下安装Firefox中国版解决Ubuntu与Windows下Firefox账号同步问题(已解决)
  4. Quartz 框架快速入门(四)
  5. Java多线程(3) Volatile的实现原理
  6. 《大规模分布式系统架构与设计实战》
  7. nssl1186-字串数量【前缀和】
  8. nginx 学习笔记【持续更新...】
  9. InstanceBeginEditable dw中特有标识
  10. 【Kettle】crt中运行spoon.sh报错
  11. java option_option 的经典属性
  12. 如何去除list中的重复元素
  13. redis中数据类型的使用,并发问题,list重复插入问题,redis使用实例-简单消息队列和排名统计
  14. 2022年茶艺师(中级)考试题及茶艺师(中级)考试技巧
  15. Android开发语音转文字,在Android上语音转文字
  16. 如果Keil不能跳转到函数的定义
  17. SQLDBX找不到服务器 no server found
  18. 小程序也能是App中的引流神器?
  19. SQL 02 基础查询与排序
  20. s7200cpu224xp手册_s7200cpu224xp模拟量转换

热门文章

  1. Android 常用权限
  2. 业务层勿用继承,不要为了方便舍弃了性能。TʌT不好意思我错了
  3. SQL入侵恢复XP_CMDSHLL与开3389
  4. springboot---基本模块详解
  5. node --- 模拟事件的异步
  6. css --- 手机端,留言模块的样式
  7. [设计模式]中介者模式之Events消息传递实现
  8. Hyper-v 2016 VHD Set
  9. 携程SQL面试题忘大牛解答解决思路
  10. 20100519 学习记录:asp CreateFolder/上传附件