当前位置:首页 > CN2资讯 > 正文内容

关闭centos某一个端口 centos怎么关闭端口

7小时前CN2资讯


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.idchg.com/info/31540.html

    分享给朋友:

    “关闭centos某一个端口 centos怎么关闭端口” 的相关文章

    UCloud年付100元的云服务选择与优势解析

    在开始探讨UCloud的计费方式之前,我想先分享一下我对云服务费用的一些理解和看法。在如今的数字化时代,选择合适的云服务提供商至关重要,计费方式也应兼顾灵活性和经济性。我在UCloud上体验过不同的计费方式,从中得出了一些实用的建议。 UCloud提供的计费方式相当多样,特别是在按年计费这一块。对于...

    如何获取便宜稳定算力以推动科研与技术创新

    便宜稳定算力是一个现代计算资源获取的概念,特别是在人工智能、深度学习及科学计算等领域中,这种计算能力的经济性与可靠性显得尤为重要。它的定义可以简单理解为以相对较低的成本获得高效、稳定的计算资源。随着科技的迅速发展,企业和研究机构对计算资源的需求逐渐增加,如何以合理的投资实现算力购买成为一个值得关注的...

    如何选择合适的VPS进行购买:关键因素解析

    选择合适的VPS进行购买是一项涉及多个因素的决策。VPS,即虚拟专用服务器,是一种介于共享主机和独立服务器之间的托管解决方案。特别适合需要灵活性和可扩展性的用户,无论是个人开发者、企业还是网站管理员。这种灵活性让VPS成为现代网络环境中一个非常受欢迎的选择。 VPS与传统的共享主机存在显著区别。传统...

    联通169回程路由怎么样?优缺点及用户评价分析

    在当下的网络环境中,联通169回程路由成为了一个备受关注的话题。很多人对它的表现充满好奇,联通169回程路由究竟如何呢?从基本信息到工作原理,再到它的主要特点,我们一起深入了解一下。 联通169回程路由的基本信息 联通169回程路由主要分为几种类型。我们通常提到的普通网络(AS4837 + AS48...

    全面掌握BBR加速技术,提升网络传输效率的终极指南

    在现代网络传输中,带宽的利用与数据传输的速度变得尤为重要。提到这里,BBR加速即以其独特的方式引起了不少人的关注。BBR,或者称为Bottleneck Bandwidth and Round-trip propagation time,是一种新的TCP拥塞控制算法,旨在优化网络性能。BBR的核心思想...

    2024美国黑五是什么时候?购物狂欢的完美攻略

    黑五简直是一场全民的购物狂欢。它通常发生在感恩节后的第二天,在美国几乎每个角落都能感受到这股购物热潮。拥有超过半个世纪的历史,黑五的起源可以追溯到1960年代。当时,许多零售商都发现,在感恩节的之后星期五,购物者们会蜂拥而至,这一天也成为了零售商们“入黑”的时刻——也就是盈利的开始。 随着时间的推移...