Centos7 关于防火墙的一些简单配置
近期安装了linux系统Centos7,接触下来发现了与原来的Centos6.5有一些差别,这里主要记录下来我的一些关于Centos7防火墙的了解。
一、firewall简介
CentOS 7中防火墙是一个非常的强大的功能,在CentOS 6.5中在iptables防火墙中进行了升级了。
在Centos中的图形化界面打开的firewall界面如上所示,可以看到在其中有“区域”、“服务”、“IPSets”,在“区域”下面有“block”、“dmz”....一些,这些每一个又有图上的右面“服务”,“端口”,“协议”.....
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.
网络区域定义了网络连接的可信等级,具体的请参考官网。
二、firewall配置
1.系统配置目录
1 | /usr/lib/firewalld/ |
[root@localhost firewalld]# ls
helpers icmptypes ipsets services xmlschema zones
目录中存放定义好的网络服务和端口参数,系统参数,不能修改。
2.用户配置目录
/etc/firewalld/
3.1自定义配置端口开放
用户可以通过修改配置文件的方式添加端口,也可以通过命令的方式添加端口,注意,修改的内容会在/etc/firewalld/
目录下的配置文件中体现。
firewall-cmd --permanent --add-port=80/tcp
firewall-cmd --permanent --add-port=8080/tcp
#注意重新加载firewall
firewall-cmd --reload
参数的介绍:
1、firewall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:标识添加的端口;
另外,firewall中有Zone的概念,可以将具体的端口制定到具体的zone配置文件中。
1 | firewall-cmd --zone=public --permanent --add-port=8010/tcp --zone=public:指定的zone为public; |
添加之后在 vim /etc/firewalld/zones/public.xml 中的结果如下所示:
如果–zone=dmz 这样设置的话,会在dmz.xml文件中新增所配置的端口。
3.2修改配置文件的方式配置端口开放
1 <?xml version="1.0" encoding="utf-8"?> 2 <zone> 3 <short>Public</short> 4 <description>For use in public areas.</description> 5 <rule family="ipv4"> 6 <source address="122.10.70.234"/> 7 <port protocol="udp" port="514"/> 8 <accept/> 9 </rule> 10 <rule family="ipv4"> 11 <source address="123.60.255.14"/> 12 <port protocol="tcp" port="10050-10051"/> 13 <accept/> 14 </rule> 15 <rule family="ipv4"> 16 <source address="192.249.87.114"/> 放通指定ip,指定端口、协议 17 <port protocol="tcp" port="80"/> 18 <accept/> 19 </rule> 20 <rule family="ipv4"> 放通任意ip访问服务器的9527端口 21 <port protocol="tcp" port="9527"/> 22 <accept/> 23 </rule> 24 </zone>
上面的配置文件理解为:
1、添加需要的规则,开放通源ip为122.10.70.234,端口514,协议udp;
2、开放通源ip为123.60.255.14,端口10050-10051,协议tcp;
3、开放通源ip为192.249.87.144,端口为80,洗衣为tcp;与上一个相同;
4、开放通源ip为任意,端口9527,协议tcp;
三、firewall常用命令
1、重启、关闭、开启firewalld.service服务
service firewalld restart 重启
service firewalld start 开启
service firewalld stop 关闭
2、查看firewall服务状态
1 | systemctl status firewall |
3、查看firewall的状态
firewall-cmd --state
4、查看防火墙规则
firewall-cmd --list-all
5、查询、开放、关闭端口
# 查询端口是否开放
firewall-cmd --query-port=8080/tcp
# 开放80端口
firewall-cmd --permanent --add-port=80/tcp
# 移除端口
firewall-cmd --permanent --remove-port=8080/tcp
#重启防火墙(修改配置后要重启防火墙)
firewall-cmd --reload
# 参数解释
1、firwall-cmd:是Linux提供的操作firewall的一个工具;
2、--permanent:表示设置为持久;
3、--add-port:表示添加的端口;
本文参考:
https://blog.csdn.net/xlgen157387/article/details/52672988
https://www.cnblogs.com/xxoome/p/7115614.html
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述