![]() | ufwUFW (Uncomplicated Firewall) 简单防火墙 |
| |
简介
UFW(Uncomplicated Firewall)是 Ubuntu 团队为 iptables 打造的“懒人壳”,一行命令就能完成常规防火墙策略,适合不想背 iptables 语法的同学。
安装
Linux
Ubuntu 
bash
apt install ufw1
Debian 12+ 
bash
apt install ufw1
Alpine 
bash
apk add ufw1
配置&使用
基本操作
bash
# 启用
ufw enable
# 禁用
ufw disable
# 查看状态
ufw status
# 查看状态(带数字编号)
ufw status numbered
# 重载
ufw reload
# 重置(会提示备份现有规则)
ufw reset
# 开启日志
ufw logging on
# 开启日志
ufw logging off
# 查询日志
cat /var/log/ufw.log1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
ipv6支持
修改 /etc/default/ufw
shell
IPV6=yes1
开启设置的默认规则
启用默认设置,默认策略将 拒绝所有传入连接,允许所有传出连接
这样可按需打开指定的端口或允许指定的ip来连接服务器。
shell
ufw default deny incoming # 拒绝所有传入连接
ufw default allow outgoing # 允许所有传出连接1
2
2
查看当前规则
shell
ufw status
# 或
ufw status numbered1
2
3
2
3
新增规则
指定端口
shell
# 允许
ufw allow 80 # 不限协议
ufw allow 80/tcp # tcp
ufw allow 80/udp # udp
# 拒绝
ufw deny 80 # 不限协议
ufw deny 80/tcp # tcp
ufw deny 80/udp # udp1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
指定端口范围
shell
# 允许
ufw allow 7000:7200 # 不限协议
ufw allow 7000:7200/tcp # tcp
ufw allow 7000:7200/udp # udp
# 拒绝
ufw deny 7000:7200 # 不限协议
ufw deny 7000:7200/tcp # tcp
ufw deny 7000:7200/udp # udp1
2
3
4
5
6
7
8
9
2
3
4
5
6
7
8
9
指定服务
shell
# 允许
ufw allow ssh
# 拒绝
ufw deny ssh1
2
3
4
5
2
3
4
5
指定IP/IP段
shell
ufw allow from 8.8.8.8 # 允许IP访问
ufw deny from 8.8.8.8 # 拒绝IP访问
ufw allow from 8.8.8.8/24 # 允许IP段访问
ufw deny from 8.8.8.8/24 # 拒绝IP段访问
ufw allow from 8.8.8.8 to any port 22 # 允许IP访问22端口
ufw deny from 8.8.8.8 to any port 22 # 拒绝IP访问22端口
ufw allow from 8.8.8.8/24 to any port 22 # 允许IP段访问22端口
ufw deny from 8.8.8.8/24 to any port 22 # 允许IP段访问22端口
ufw allow from 8.8.8.8 to any port 22 proto tcp # 允许IP通过tcp协议访问22端口
ufw deny from 8.8.8.8 to any port 22 proto tcp # 拒绝IP通过tcp协议访问22端口
ufw allow from 8.8.8.8/24 to any port 22 proto tcp # 允许IP段通过tcp协议访问22端口
ufw deny from 8.8.8.8/24 to any port 22 proto tcp # 拒绝IP段通过tcp协议访问22端口1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
删除规则
shell
ufw delete allow ssh # 删除允许的ssh服务的规则
ufw delete allow 22 # 删除允许的端口22的规则
ufw status numbered # 获取规则编号
ufw delete 1 # 删除编号1的规则1
2
3
4
5
2
3
4
5
