firewalld 详细介绍配置(二)

简介: 【8月更文挑战第10天】配置`firewalld`涉及安装、启动与管理防火墙规则。首先通过`yum install firewalld`安装,然后使用`systemctl`命令进行启动、停止及禁用服务。`firewall-cmd`命令用于管理防火墙状态、查看版本与帮助信息、设置默认区域及接口所属区域。可通过添加或移除端口和服务来控制网络流量,并利用丰富规则进行更细粒度的控制。此外,`firewalld`支持动态规则更新,无需重启服务即可生效。可以通过配置文件持久化规则设置,适用于长期维护场景。

配置firewalld

安装firewalld:# yum install firewalld

启动:# systemctl start firewalld
查看状态:# systemctl status firewalld 或者 firewall-cmd --state
停止:# systemctl  stop  firewalld
禁用:# systemctl  disable  firewalld



查看版本:# firewall-cmd --version
查看帮助:# firewall-cmd --help
显示状态:# firewall-cmd --state
查看区域信息:# firewall-cmd --get-active-zones
查看指定接口所属区域:$ firewall-cmd --get-zone-of-interface=ens33
拒绝所有包:# firewall-cmd --panic-on
取消拒绝状态:# firewall-cmd --panic-off
查看是否拒绝:# firewall-cmd --query-panic

更新防火墙规则:

 # firewall-cmd --reload
# firewall-cmd --complete-reload
两者的区别就是第一个无需断开连接,就是firewalld特性之一动态添加规则,第二个需要断开连接,类似重启服务

使用下面的命令分别列出所有支持的 zone 和查看当前的默认 zone:

[root@xuegod63 ~]# firewall-cmd --get-zones

block dmz drop external home internal public trusted work

[root@xuegod63 ~]# firewall-cmd --get-default-zone

public

 

设置默认接口区域
# firewall-cmd --set-default-zone=public
立即生效无需重启

 

将接口添加到区域,默认接口都在public
# firewall-cmd --zone=public --add-interface=ens33
永久生效再加上 --permanent 然后reload防火墙

打开端口(貌似这个才最常用)
查看所有打开的端口:
# firewall-cmd --zone=dmz --list-ports
加入一个端口到区域:
# firewall-cmd --zone=dmz --add-port=8080/tcp
若要永久生效方法同上

打开一个服务,类似于将端口可视化,服务需要在配置文件中添加,/etc/firewalld 目录下有services文件夹

# firewall-cmd --zone=work --add-service=smtp
移除服务
# firewall-cmd --zone=work --remove-service=smtp

 

# 输出区域  全部启用的特性。如果省略区域,将显示默认区域的信息。
firewall-cmd [–zone=] –list-all

[root@xuegod63 ~]# firewall-cmd --list-all

public (default, active)

interfaces: eno16777736

sources:

services: dhcpv6-client ssh

ports:

masquerade: no

forward-ports:

icmp-blocks:

rich rules:

 

[root@xuegod63 ~]# firewall-cmd --zone=work --list-all

work

interfaces:

sources:

services: dhcpv6-client ipp-client ssh

ports:

masquerade: no

forward-ports:

icmp-blocks:

rich rules:

 

# 获取活动的区域

[root@xuegod63 ~]# firewall-cmd --get-active-zones

work

interfaces: ens33

 

# 根据接口获取区域
firewall-cmd -get-zone-of-interface=

[root@xuegod63 ~]# firewall-cmd --get-zone-of-interface=ens33

# 以下关于区域和接口的操作,就不一一举例了,可以根据实际情况修改.
# 将接口增加到区域

firewall-cmd [--zone=] --add-interface=

# 如果接口不属于区域,接口将被增加到区域。如果区域被省略了,将使用默认区域。接口在重新加载后将重新应用。

# 修改接口所属区域

firewall-cmd [--zone=] --change-interface=

# 这个选项与 –add-interface 选项相似,但是当接口已经存在于另一个区域的时候,该接口将被添加到新的区域。

# 从区域中删除一个接口

firewall-cmd [--zone=] --remove-interface=

# 查询区域中是否包含某接口

firewall-cmd [--zone=] --query-interface=

# 注意:返回接口是否存在于该区域。没有输出。

# 列举区域中启用的服务

firewall-cmd [ --zone= ] --list-services

# 这两条简单点说,就是断网和连网.

firewall-cmd --panic-on

# 启用应急模式阻断所有网络连接,以防出现紧急状况

firewall-cmd --panic-off

# 查询应急模式

firewall-cmd --query-panic

 

#####通过配置文件来使用Firewalld的方法#####

系统本身已经内置了一些常用服务的防火墙规则,存放在/usr/lib/firewalld/services/

注意!

请勿编辑/usr/lib/firewalld/services/ ,只有 /etc/firewalld/services 的文件可以被编辑。

[root@xuegod63 ~]# ls /usr/lib/firewalld/services/

amanda-client.xml dhcpv6.xml high-availability.xml ipp-client.xml kpasswd.xml libvirt.xml

mysql.xml pmcd.xml pop3s.xml RH-Satellite-6.xml smtp.xml tftp.xml

bacula-client.xml dhcp.xml https.xml ipp.xml ldaps.xml mdns.xml nfs.xml pmproxy.xml

postgresql.xml rpc-bind.xml ssh.xml transmission-client.xml

bacula.xml dns.xml http.xml ipsec.xml ldap.xml mountd.xml ntp.xml pmwebapis.xml

proxy-dhcp.xml samba-client.xml telnet.xml vnc-server.xml

dhcpv6-client.xml ftp.xml imaps.xml kerberos.xml libvirt-tls.xml ms-wbt.xml openvpn.xml

pmwebapi.xml radius.xml samba.xml tftp-client.xml wbem-https.xml

动态添加一条防火墙规则如下:

以下例子均以系统自带的public zone 为例子.


假设自定义的 ssh 端口号为 12222,使用下面的命令来添加新端口的防火墙规则:

 

如果需要使规则保存到 zone 配置文件,则需要加参数 –permanent
举例如下:

[root@xuegod63 ~]# firewall-cmd --add-port=12222/tcp

success

[root@xuegod63 ~]# vim  /etc/firewalld/zones/public.xml



Public

For use in public areas. You do not trust the other computers on networks to

not harm your computer. Only selected incoming connections are accepted.




发现没有写到配置文件里


[root@xuegod63 ~]# firewall-cmd --add-port=12222/tcp --permanent

success

[root@xuegod63 ~]# cat /etc/firewalld/zones/public.xml



Public

For use in public areas. You do not trust the other computers on networks to

not harm your computer. Only selected incoming connections are accepted.





已经写到配置文件里

 

# 注意:防火墙配置文件也可以手动修改,修改后记得重载

9.3.2  firewalld配置

如果想开放80端口供外网访问http服务,例子如下

1. 将 http.xml复制到/etc/firewalld/services/下面,以服务形式管理防火墙,

# 这个cp命令其实是可以省略的,系统会优先去读取 /etc/firewalld 里面的文件,读取完毕后,会去/usr/lib/firewalld/services/ 再次读取.为了方便修改和管理,建议复制到/etc/firewalld

[root@xuegod63 ~]# cp /usr/lib/firewalld/services/http.xml /etc/firewalld/services/

[root@xuegod63 ~]# ls /etc/firewalld/services/

http.xml

[root@xuegod63 ~]# ls /etc/firewalld/zones/

public.xml public.xml.old

 

(1) 修改public.xml,加入http服务

[root@xuegod63 ~]# cat /etc/firewalld/zones/public.xml



Public

For use in public areas. You do not trust the other computers on networks to

not harm your computer. Only selected incoming connections are accepted.



    #这行是后加的,要匹配 /etc/firewalld/services/目录下的文件名


 

(2) 重新载入 两条命令都可以的

以 root 身份输入以下命令,重新加载防火墙,并不中断用户连接,即不丢失状态信息:

[root@xuegod63 ~]# firewall-cmd --reload

success

 

2. 修改防火墙ssh的端口方法

(1) 复制ssh.xml文件到/etc/firewalld/services/

[root@xuegod63 ~]# cp /usr/lib/firewalld/services/ssh.xml /etc/firewalld/services/

 

(2) 修改ssh.xml文件 12222为ssh端口

[root@xuegod63 ~]# vim /etc/firewalld/services/ssh.xml



SSH

Secure Shell (SSH) is a protocol for logging into and executing commands on

remote machines. It provides secure encrypted communications. If you plan on accessing

your machine remotely via SSH over a firewalled interface, enable this option. You need the

openssh-server package installed for this option to be useful.



 

(3) 重新载入 两条命令都可以的

以 root 身份输入以下命令,重新加载防火墙,并不中断用户连接,即不丢失状态信息:

[root@xuegod63 ~]# firewall-cmd --reload

success

 

3. 指定ip访问ssh端口

修改/etc/firewalld/zones/

# rule字段内为要限制的ip和使用的服务

[root@xuegod63 ~]# vim /etc/firewalld/zones/public.xml



Public

For use in public areas. You do not trust the other computers on networks to

not harm your computer. Only selected incoming connections are accepted.








[root@xuegod63 ~]# firewall-cmd --reload

success

 

4. 添加自定义服务

举例添加8080端口为新的Service
(1) 添加新文件 8080.xml

vim /etc/firewalld/services/8080.xml

8080 Test

此处为文字说明

 

(2)编辑public.xml 文件,加入相应的Server

vim /etc/firewalld/zones/public.xml

Public

For use in public areas. You do not trust the other computers on networks to

not harm your computer. Only selected incoming connections are accepted.


# 这行是后加的,要匹配 /etc/firewalld/services/8080.xml 文件名

重新载入

[root@xuegod63 ~]# firewall-cmd --reload

success

  

SNAT

firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A POSTROUTING -s 192.168.10.0/24 -j SNAT --to-source 你的公网ip

 

DNAT

firewall-cmd --permanent --direct --passthrough ipv4 -t nat -A PREROUTING -d 173.16.16.1 -p tcp--dport 2346 -j DNAT --to-destination 192.168.10.2:22


 

目录
相关文章
|
6月前
|
人工智能 监控 API
Claude Code终于有仪表盘了:3条命令装个HUD,上下文用了多少一眼就知道
老金我最近用Claude Code,遇到一个特别烦的事。 写着写着,突然蹦出来一句"context window is getting full"。 然后AI就开始犯傻了——回答变短、逻辑变乱、之前说好的方案全忘了。 每次遇到这种情况,老金我都想骂人。 问题出在哪? Claude Code的终端界面,压根看不到上下文用了多少。 你只能输入 /context手动查,但谁写代码的时候
12918 9
|
消息中间件 Kafka Windows
Kafka Windows运行错误:创建消费者报错 consumer zookeeper is not a recognized option
Kafka Windows运行错误:创建消费者报错 consumer zookeeper is not a recognized option
1011 0
Kafka Windows运行错误:创建消费者报错 consumer zookeeper is not a recognized option
|
11月前
|
存储 Kubernetes 网络安全
关于阿里云 Kubernetes 容器服务(ACK)添加镜像仓库的快速说明
本文介绍了在中国大陆地区因网络限制无法正常拉取 Docker 镜像的解决方案。作者所在的阿里云 Kubernetes 集群使用的是较旧版本的 containerd(1.2x),且无法直接通过 SSH 修改节点配置,因此采用了一种无需更改 Kubernetes 配置文件的方法。通过为 `docker.io` 添加 containerd 的镜像源,并使用脚本自动修改 containerd 配置文件中的路径错误(将错误的 `cert.d` 改为 `certs.d`),最终实现了通过多个镜像站点拉取镜像。作者还提供了一个可重复运行的脚本,用于动态配置镜像源。虽然该方案能缓解镜像拉取问题,
1103 3
|
负载均衡 算法 安全
基于Reactor模式的高性能网络库之线程池组件设计篇
EventLoopThreadPool 是 Reactor 模式中实现“一个主线程 + 多个工作线程”的关键组件,用于高效管理多个 EventLoop 并在多核 CPU 上分担高并发 I/O 压力。通过封装 Thread 类和 EventLoopThread,实现线程创建、管理和事件循环的调度,形成线程池结构。每个 EventLoopThread 管理一个子线程与对应的 EventLoop(subloop),主线程(base loop)通过负载均衡算法将任务派发至各 subloop,从而提升系统性能与并发处理能力。
601 3
|
9月前
|
机器学习/深度学习 人工智能 前端开发
终端里的 AI 编程助手:OpenCode 使用指南
OpenCode 是开源的终端 AI 编码助手,支持 Claude、GPT-4 等模型,可在命令行完成代码编写、Bug 修复、项目重构。提供原生终端界面和上下文感知能力,适合全栈开发者和终端用户使用。
59043 11
|
网络协议 数据可视化 关系型数据库
firewall-cmd命令详解
firewall-cmd命令详解
1213 1
|
12月前
|
监控 供应链 搜索推荐
实时同步淘宝订单数据接口,实现订单状态实时监控与管理
本项目旨在为中小微及大型电商企业提供高效、稳定的淘宝订单数据接口解决方案。针对不同行业需求,提供实时订单监控、库存同步、物流追踪等功能,助力企业提升运营效率,优化供应链管理。通过线上线下多渠道推广与精准营销策略,实现产品快速落地与品牌影响力提升。
|
存储 供应链 数据可视化
Java 大视界 -- 基于 Java 的大数据可视化在企业供应链风险预警与决策支持中的应用(204)
本篇文章探讨了基于 Java 的大数据可视化技术在企业供应链风险预警与决策支持中的深度应用。文章系统介绍了从数据采集、存储、处理到可视化呈现的完整技术方案,结合供应链风险预警与决策支持的实际案例,展示了 Java 大数据技术如何助力企业实现高效、智能的供应链管理。