2021-11-14 15:52:46 +08:00

257 lines
8.6 KiB
Markdown
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

---
title: "Firewalld 笔记"
date: 2019-10-30T13:01:37+08:00
lastmod: 2019-10-30T13:01:37+08:00
tags: ["firwalld"]
categories: ["network"]
---
## 区域(zone)
- 查看支持的 zone
```bash
firewall-cmd --get-zones [--permanent]
```
- zone 定义
- 丢弃(drop) 任何接收的网络数据包都被丢弃,没有任何回复。仅能有发送出去的网络连接。
- 限制(block) 任何接收的网络连接都被 IPv4 的 icmp-host-prohibited 信息和 IPv6 的 icmp6-adm-prohibited 信息所拒绝。
- 公共(public) 在公共区域内使用,不能相信网络内的其他计算机不会对您的计算机造成危害,只能接收经过选取的连接。
- 外部(external) 特别是为路由器启用了伪装功能的外部网。您不能信任来自网络的其他计算,不能相信它们不会对您的计算机造成危害,只能接收经过选择的连接。
- 非军事区(dmz) 用于您的非军事区内的电脑,此区域内可公开访问,可以有限地进入您的内部网络,仅仅接收经过选择的连接。
- 工作(work) 用于工作区。您可以基本相信网络内的其他电脑不会危害您的电脑。仅仅接收经过选择的连接。
- 家庭(home) 用于家庭网络。您可以基本信任网络内的其他计算机不会危害您的计算机。仅仅接收经过选择的连接。
- 内部(internal) 用于内部网络。您可以基本上信任网络内的其他计算机不会威胁您的计算机。仅仅接受经过选择的连接。
- 信任(trusted) 可接受所有的网络连接。
- 指定其中一个区域为默认区域是可行的。当接口连接加入了 NetworkManager它们就被分配为默认区域。安装时firewalld 里的默认区域被设定为公共区域。
- 判断请求使用的 zone
- source源地址优先级最高
- interface接收请求的网卡
- 默认 zone优先级最低
- 查看默认的 zone
```bash
firewall-cmd --get-default-zone
```
- 设置默认的 zone
```bash
firewall-cmd --set-default-zone=public
```
- 查看活动的 zone
```bash
firewall-cmd --get-active-zones
```
- 查看指定 zone 下的规则
```bash
firewall-cmd [--zone=<zone>] --list-all
```
## 源地址(source)
- 列出指定zone的所有绑定的source地址
```bash
firewall-cmd [--permanent] [--zone=zone] --list-sources
```
- 查询指定zone是否跟指定source地址进行了绑定
```bash
firewall-cmd [--permanent] [--zone=zone] --query-source=ip[/mask]
```
- 用于将一个source地址绑定到指定的zone只可绑定一次第二次绑定到不同的zone会报错
```bash
firewall-cmd [--permanent] [--zone=zone] --add-source=ip[/mask]
```
- 改变source地址所绑定的zone如果原来没有绑定则进行绑定
```bash
firewall-cmd [--permanent] [--zone=zone] --change-source=ip[/mask]
```
- 删除source地址跟zone的绑定
```bash
firewall-cmd [--permanent] [--zone=zone] --remove-source=ip[/mask]
```
## 网卡(interface)
- 获取网卡所在的 zone
```bash
firewall-cmd --get-zone-of-interface=<interface>
```
- 增加网卡到 zone
```bash
firewall-cmd [--zone=<zone>] --add-interface=<interface>
```
- 修改网卡到 zone
```bash
firewall-cmd [--zone=<zone>] --change-interface=<interface>
```
- 从 zone 中删除网卡
```bash
firewall-cmd [--zone=<zone>] --remove-interface=<interface>
```
- 查看 zone 中是否包含某网卡
```bash
firewall-cmd [--zone=<zone>] --query-interface=<interface>
```
## target
- 默认可以取四个值: default、ACCEPT、%%REJECT%%、DROP
- 查看 taget
```bash
firewall-cmd --permanent [--zone=zone] --get-target
```
- 设置 target
```bash
firewall-cmd --permanent [--zone=zone] --set-target=target
```
- **必须使用参数 --permanent**,而且使用 firewall-cmd 命令不能直接生效,需 reload
## 服务(service)
- 查看支持的 service
```bash
firewall-cmd --get-services [--permanent]
```
- 查看 zone 启动的 service
```bash
firewall-cmd [--zone=<zone>] --list-services
```
- 在 zone 中启动 service
```bash
firewall-cmd [--zone=<zone>] --add-service=<service> \
[ --permanent | --timeout=<seconds> ]
```
- 禁用 zone 中的 service
```bash
firewall-cmd [--zone=<zone>] --remove-service=<service> [--permanent]
```
- 查看 zone 中是否启动 service
```bash
firewall-cmd [--zone=<zone>] --query-service=<service>
```
## 端口和协议组合
- 查看配置的全部端口规则
```bash
firewall-cmd [--permanent] [--zone=zone] --list-ports
```
- 启动 zone 中指定协议的端口
```bash
firewall-cmd [--zone=<zone>] --add-port=<port>[-<port>]/<protocol> \
[ --permanent | --timeout=<seconds> ]
```
- 禁用 zone 中指定协议的端口
```bash
firewall-cmd [--zone=<zone>] --remove-port=<port>[-<port>]/<protocol> [--permanent]
```
- 查看 zone 中指定协议的端口
```bash
firewall-cmd [--zone=<zone>] --query-port=<port>[-<port>]/<protocol> [--permanent]
```
## ICMP
- 查看支持的 icmp 类型
```bash
firewall-cmd --get-icmptypes [--permanent]
```
- 查看全部 icmp 阻塞规则
```bash
firewall-cmd [--permanent] [--zone=zone] --list-icmp-blocks
```
- 启动 zone 中的 icmp 阻塞
```bash
firewall-cmd [--zone=<zone>] --add-icmp-block=<icmptype> \
[ --permanent | --timeout=seconds ]
```
- 禁用 zone 中的 icmp 阻塞
```bash
firewall-cmd [--zone=<zone>] --remove-icmp-block=<icmptype> [--permanent]
```
- 查询 zone 的 icmp 阻塞
```bash
firewall-cmd [--zone=<zone>] --query-icmp-block=<icmptype> [--permanent]
```
## IPV4 源地址转换
- 启动 zone 中 ipv4 源地址转换
```bash
firewall-cmd [--zone=<zone>] --add-masquerade \
[ --permanent | --timeout=seconds ]
```
- 禁用 zone 中 ipv4 源地址转换
```bash
firewall-cmd [--zone=<zone>] --remove-masquerade [--permanent]
```
- 查看 zone 中 ipv4 源地址转换
```bash
firewall-cmd [--zone=<zone>] --query-masquerade [--permanent]
```
## 端口转发
- 查看全部端口转发规则
```bash
firewall-cmd [--permanent] [--zone=zone] --list-forward-ports
```
- 启动 zone 中端口转发
```bash
firewall-cmd [--zone=<zone>] --add-forward-port=port=<port>[-<port>]:proto=<protocol> \
{ :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> } \
[ --permanent | --timeout=seconds ]
```
- 禁用 zone 中端口转发
```bash
firewall-cmd [--zone=<zone>] --remove-forward-port=port=<port>[-<port>]:proto=<protocol> \
{ :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> } \
[--permanent]
```
- 查看 zone 中端口转发
```bash
firewall-cmd [--zone=<zone>] --query-forward-port=port=<port>[-<port>]:proto=<protocol> \
{ :toport=<port>[-<port>] | :toaddr=<address> | :toport=<port>[-<port>]:toaddr=<address> } \
[--permanent]
```
## Rich Rules
- 通用结构
```bash
firewall-cmd [--zone=<zone>] [ --permanent | --timeout=seconds ] \
<--add|--remove>-rich-rule='rule [family=<ipv4|ipv6> \
source address=<address[/mask]> [invert=<True|False>] \
destination address=<address[/mask]> [invert=<True|False>] \
service name=<service> \
port port=<port>[-<port>] \
protocol=<protocol> \
icmp-block name=<icmptype> \
masquerade \
forward-port port=<port>[-<port>] protocol=<protocol> to-port=<port>[-<port>] to-address=<address> \
log [prefix=<prefix text>] [level=<log level>] [limit value=<rate/duration>] \
accept|reject [type=<reject type>]|drop'
```
- 查看全部 rich rule
```bash
firewall-cmd [--permanent] [--zone=zone] --list-rich-rules
```
- 具体参数解释见系统 man 手册
```bash
man firewalld.richlanguage 5
```
## 应急模式(panic)
- 启动 panic即断网
```bash
firewall-cmd --panic-on
```
- 关闭 panic即联网
```bash
firewall-cmd --panic-off
```
- 查询应急模式
```bash
firewall-cmd --query-panic
```
## 重新载入(reload)
- 重新载入防火墙,不中断用户连接
```bash
firewall-cmd --reload
```
- 重新载入防火墙并中断用户连接
```bash
firewall-cmd --complete-reload
```
## 备注
- 参数 --timeout 是让规则生效一段时间,过期自动删除,不能与 --permanent 一起使用