轻聆月下
轻聆月下
发布于 2024-03-24 / 2 阅读
0
0

使用UFW配置Linux防火墙

一、UFW 简介

UFW, or Uncomplicated Firewall, is a simplified firewall management interface that hides the complexity of lower-level packet filtering technologies such as iptables and nftables. If you’re looking to get started securing your network, and you’re not sure which tool to use, UFW may be the right choice for you.

二、用法

2.1 UFW操作

关闭

sudo ufw disable

开启

sudo ufw enable

重载配置

sudo ufw reload

删除所有配置并关闭

sudo ufw reset

2.2 查看当前配置

sudo ufw status numbered

输出如下:

Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22                         ALLOW IN    15.15.15.0/24
[ 2] 80                         ALLOW IN    Anywhere

2.3 删除配置

根据上述输出的配置序号进行删除:

sudo ufw delete 2

2.4 默认设置

默认拒绝所有入站流量

sudo ufw default deny incoming

2.5 开启端口

开启22端口

sudo ufw allow 22

开启6000到7000所有端口,(此操作必须添加 tcp 或者 udp 协议)

sudo ufw allow 6000:7000/tcp
sudo ufw allow 6000:7000/udp

2.6 添加IP白名单

允许 xxx.xxx.xxx.xxx 访问所有IP的所有端口

sudo ufw allow from xxx.xxx.xxx.xxx

允许 xxx.xxx.xxx.0/24 整段 访问所有IP的所有端口

sudo ufw allow from xxx.xxx.xxx.0/24

允许 xxx.xxx.xxx.xxx 访问所有IP的22端口

sudo ufw allow from xxx.xxx.xxx.xxx to any port 22

允许 xxx.xxx.xxx.xxx 访问 yyy.yyy.yyy.yyy 上的22端口

sudo ufw allow from xxx.xxx.xxx.xxx to yyy.yyy.yyy.yyy port 22

2.7 添加IP黑名单

禁止 xxx.xxx.xxx.xxx 访问所有IP的所有端口

sudo ufw deny from xxx.xxx.xxx.xxx

2.8 指定网卡

允许 eth1 网卡上的请求访问 3306 端口

sudo ufw allow in on eth1 to any port 3306

评论