Linux 防火墙 firewalld 常用命令

1 防火墙 - firewalld

1.1 开启防火墙

shell 复制代码
# 临时性开启,服务器重启后会恢复为原来的状态
systemctl start firewalld
# 永久性开启(即开机启动),重启服务器后生效
systemctl enable firewalld

1.2 关闭防火墙

shell 复制代码
# 临时性关闭,服务器重启后会恢复为原来的状态
systemctl stop firewalld
# 永久性关闭,重启服务器后生效
systemctl disable firewalld

1.3 重启防火墙

shell 复制代码
systemctl restart firewalld

1.4 查看防火墙状态

shell 复制代码
# 使用 systemctl 命令检查 firewalld 服务状态
systemctl status firewalld
# 使用 firewall-cmd 命令查看 firewalld 的状态
firewall-cmd --state

1.5 重载防火墙配置

shell 复制代码
# 重载防火墙配置,在不影响网络连接的情况下更新防火墙规则
firewall-cmd --reload

2 区域 - zone

2.1 查看所有可用的区域

shell 复制代码
firewall-cmd --get-zones

2.2 查看当前激活的区域

shell 复制代码
firewall-cmd --get-active-zones

2.3 查看指定区域的详细信息

shell 复制代码
firewall-cmd --zone=public --list-all

# 注:如果指定区域为默认区域,可以省略 --zone 参数
firewall-cmd --list-all

2.4 查看默认区域

shell 复制代码
firewall-cmd --get-default-zone

2.5 设置默认区域

shell 复制代码
firewall-cmd --set-default-zone=public

2.6 添加区域

shell 复制代码
firewall-cmd --permanent --new-zone=myzone

2.7 删除区域

shell 复制代码
firewall-cmd --permanent --delete-zone=myzone

2.8 分配网络接口到指定区域

shell 复制代码
firewall-cmd --zone=public --change-interface=eth0

3 端口 - port

3.1 查看已开放的端口

shell 复制代码
firewall-cmd --list-ports

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --list-ports

3.2 添加指定开放端口

shell 复制代码
firewall-cmd --permanent --add-port=3306/tcp
# 开放指定端口范围
firewall-cmd --permanent --add-port=9000-9008/tcp

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --permanent --add-port=3306/tcp
firewall-cmd --zone=public --permanent --add-port=9000-9008/tcp

3.3 移除指定开放端口

shell 复制代码
firewall-cmd --permanent --remove-port=3306/tcp
# 移除指定端口范围
firewall-cmd --permanent --remove-port=9000-9008/tcp

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --permanent --remove-port=3306/tcp
firewall-cmd --zone=public --permanent --remove-port=9000-9008/tcp

3.4 查询指定开放端口

shell 复制代码
# yes 表示开启,no 表示未开启
firewall-cmd --query-port=3306/tcp

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --query-port=3306/tcp

4 服务 - service

4.1 查看已启用的服务

shell 复制代码
firewall-cmd --list-services

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --list-services

4.2 查看所有可用的服务

shell 复制代码
firewall-cmd --get-services

4.3 查看指定防火墙规则

shell 复制代码
firewall-cmd --info-service=ssh

4.4 添加指定服务

shell 复制代码
firewall-cmd --permanent --add-service=http

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --permanent --add-service=http

4.5 移除指定服务

shell 复制代码
firewall-cmd --permanent --remove-service=http

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --permanent --remove-service=http

5 富规则 - rich-rule

5.1 查看已设置的富规则

shell 复制代码
firewall-cmd --list-rich-rules

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --list-rich-rules

5.2 添加规则

shell 复制代码
# 添加富规则
# family="ipv4":规则的 IP 协议版本为 IPv4
# source address="192.168.198.200":规则适用于源地址为 192.168.198.200 的流量
# port protocol="tcp" port="3306":规则适用于 TCP 协议的 3306 端口
# accept:允许规则匹配的流量通过
# reject:拒绝规则匹配的流量通过

# 允许来自 192.168.198.200 地址的 IPv4 TCP 3306 端口的流量通过防火墙,即允许 192.168.198.200 访问 3306 端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.198.200" port protocol="tcp" port="3306" accept"
# 拒绝来自 192.168.198.200 地址的 IPv4 TCP 3306 端口的流量通过防火墙,即拒绝 192.168.198.200 访问 3306 端口
firewall-cmd --permanent --add-rich-rule="rule family="ipv4" source address="192.168.198.200" port protocol="tcp" port="3306" reject"

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --permanent --add-rich-rule="rule family="ipv4" source address="192.168.198.200" port protocol="tcp" port="3306" accept"
firewall-cmd --zone=public --permanent --add-rich-rule="rule family="ipv4" source address="192.168.198.200" port protocol="tcp" port="3306" reject"

5.3 移除规则

shell 复制代码
firewall-cmd --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.198.200" port protocol="tcp" port="3306" accept"

# 注:未显式指定所属区域则使用默认区域 public,即上述命令相当于
firewall-cmd --zone=public --permanent --remove-rich-rule="rule family="ipv4" source address="192.168.198.200" port protocol="tcp" port="3306" accept"

6 注意事项

  • --permanent参数会将修改的防火墙配置永久性保存到防火墙配置文件中;修改防火墙配置时如果没有使用该参数则会在系统重启时丢失修改的配置。
相关推荐
苦学编程的谢4 分钟前
Linux
linux·运维·服务器
G_H_S_3_12 分钟前
【网络运维】Linux 文本处理利器:sed 命令
linux·运维·网络·操作文本
Linux运维技术栈22 分钟前
多系统 Node.js 环境自动化部署脚本:从 Ubuntu 到 CentOS,再到版本自由定制
linux·ubuntu·centos·node.js·自动化
拾心2139 分钟前
【运维进阶】Linux 正则表达式
linux·运维·正则表达式
xcs194051 小时前
AI 自动化编程 trae 体验 页面添加富编辑器
运维·自动化·编辑器
Gss7772 小时前
源代码编译安装lamp
linux·运维·服务器
444A4E2 小时前
深入理解Linux进程管理:从创建到替换的完整指南
linux·c语言·操作系统
G_H_S_3_2 小时前
【网络运维】Linux:正则表达式
linux·运维·网络·正则表达式
敲上瘾2 小时前
Linux I/O 多路复用实战:Select/Poll 编程指南
linux·服务器·c语言·c++·select·tcp·poll
huangyuchi.2 小时前
【Linux系统】匿名管道以及进程池的简单实现
linux·运维·服务器·c++·管道·匿名管道·进程池简单实现