文章目录
环境
Ubuntu 版本信息:
$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description: Ubuntu 22.04 LTS
Release: 22.04
Codename: jammy
开放端口,删除已经开放的端口
ufw - program for managing a netfilter firewall
查看帮助信息:
$ ufw --help
Usage: ufw COMMAND
Commands:
enable enables the firewall
disable disables the firewall
default ARG set default policy
logging LEVEL set logging to LEVEL
allow ARGS add allow rule
deny ARGS add deny rule
reject ARGS add reject rule
limit ARGS add limit rule
delete RULE|NUM delete RULE
insert NUM RULE insert RULE at NUM
prepend RULE prepend RULE
route RULE add route RULE
route delete RULE|NUM delete route RULE
route insert NUM RULE insert route RULE at NUM
reload reload firewall
reset reset firewall
status show firewall status
status numbered show firewall status as numbered list of RULES
status verbose show verbose firewall status
show ARG show firewall report
version display version information
Application profile commands:
app list list application profiles
app info PROFILE show information on PROFILE
app update PROFILE update PROFILE
app default ARG set default application policy
查看防火墙状态:
$ sudo ufw status
Status: active
开放 22 端口:
$ sudo ufw allow 22
Rule added
Rule added (v6)
添加完成,查看防火墙状态:
$ sudo ufw status
Status: active
To Action From
-- ------ ----
22 ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
添加 8888 端口,并且只接受 TCP 协议:
$ sudo ufw allow 8888/tcp
Rule added
Rule added (v6)
添加完成,查看防火墙状态:
$ sudo ufw status
Status: active
To Action From
-- ------ ----
22 ALLOW Anywhere
8888/tcp ALLOW Anywhere
22 (v6) ALLOW Anywhere (v6)
8888/tcp (v6) ALLOW Anywhere (v6)
查看防火墙状态编号列表的规则:
$ sudo ufw status numbered
Status: active
To Action From
-- ------ ----
[ 1] 22 ALLOW IN Anywhere
[ 2] 8888/tcp ALLOW IN Anywhere
[ 3] 22 (v6) ALLOW IN Anywhere (v6)
[ 4] 8888/tcp (v6) ALLOW IN Anywhere (v6)
删除编号 3 的规则:
$ sudo ufw delete 3
Deleting:
allow 22
Proceed with operation (y|n)? y
Rule deleted (v6)
删除完成,重新查看防火墙状态编号列表的规则:
$ sudo ufw status numbered
Status: active
To Action From
-- ------ ----
[ 1] 22 ALLOW IN Anywhere
[ 2] 8888/tcp ALLOW IN Anywhere
[ 3] 8888/tcp (v6) ALLOW IN Anywhere (v6)
继续删除编号 3 的规则:
$ sudo ufw delete 3
Deleting:
allow 8888/tcp
Proceed with operation (y|n)? y
Rule deleted (v6)
删除完成,重新查看防火墙状态编号列表的规则:
$ sudo ufw status numbered
Status: active
To Action From
-- ------ ----
[ 1] 22 ALLOW IN Anywhere
[ 2] 8888/tcp ALLOW IN Anywhere