工具
Web 开发
Web 服务
Firewalld 防火墙配置
Firewalld 简化了防火墙配置,之前 Firewalld 底层用 iptables,CentOS 8 开始 Firewalld 的底层使用 nftable。
Firewalld 使用 Zones, Services 替代 Chain, rules。通过 Firewalld 修改规则不会影响当前已建立的 sessions 和 connections。
查看状态:
$ sudo firewall-cmd --state
running
查看当前的防火墙配置:
$ sudo firewall-cmd --list-all
Firewalld 的配置分为两种:运行时的和永久的。默认是配置运行时规则,用 --permanent
修改永久配置。
$ sudo firewall-cmd --add-service=http
$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --reload
firewalld 的基础概念
Zones
Zones 是预设定的规则组,适用于一定的场景,例如 home, public, trusted 等。默认的 zone 是 public
。查看所有的 Zones:
$ sudo firewall-cmd --list-all-zones
查看 Zone:
$ sudo firewall-cmd --get-default-zone
$ sudo firewall-cmd --get-active-zones
$ sudo firewall-cmd --zone=public --list-all
Services
Services 是预设定的服务,如 http, git, syslog, mysql 等。查看所有的 Services:
$ sudo firewall-cmd --get-services
添加删除 Service:
$ sudo firewall-cmd --get-services
$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --remove-service=http --permanent
查看系统定义的 Services:
$ cat /usr/lib/firewalld/services/http.xml
<?xml version="1.0" encoding="utf-8"?>
<service>
<short>WWW (HTTP)</short>
<description>...</description>
<port protocol="tcp" port="80"/>
</service>
Services 配置路径:
- 系统定义的服务如 http 等:
/usr/lib/firewalld/services/*.xml
- 用户定义的服务:
/etc/firewalld/services/*.xml
常用命令
常用参数列表:
--permanent
--add-port
,--remove-port
--add-source
,--remove-source
--add-rich-rule
,--remove-source
--list-ports
,--list-sources
,--list-services
--reload
开放或关闭端口+协议:
$ sudo firewall-cmd --add-port=3306/tcp --permanent
$ sudo firewall-cmd --remove-port=3306/tcp --permanent
$ sudo firewall-cmd --list-ports
$ sudo firewall-cmd --add-service=http --permanent
$ sudo firewall-cmd --remove-service=http --permanent
$ sudo firewall-cmd --list-services
添加/删除 IP 白名单:
$ sudo firewall-cmd --permanent --add-source=192.168.1.100
$ sudo firewall-cmd --permanent --add-source=192.168.1.0/24
$ sudo firewall-cmd --permanent --remove-source=192.168.1.100
$ sudo firewall-cmd --permanent --remove-source=192.168.1.0/24
$ sudo firewall-cmd --list-sources
添加/删除 IP 黑名单:
$ sudo firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.100' drop"
$ sudo firewall-cmd --permanent --add-rich-rule="rule family='ipv4' source address='192.168.1.0/24' drop"
允许 IP 访问指定服务/端口:
$ sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="mysql" accept'
$ sudo firewall-cmd --permanent --add-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="3306" accept'
$ sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.1.100" service name="mysql" accept'
$ sudo firewall-cmd --permanent --remove-rich-rule='rule family="ipv4" source address="192.168.1.100" port protocol="tcp" port="3306" accept'