雷达智富

首页 > 内容 > 程序笔记 > 正文

程序笔记

常见 UFW 防火墙命令及示例

2024-06-28 35

防火墙是一个安全控制网络,它根据设定的规则来控制和过滤流量。简而言之,它可以保护系统免受来自外部的不需要的流量的影响。根据定义的防火墙规则,可以阻止或允许流量通过。

Linux 内核附带了一个称为 Netfilter 的子系统。它负责决定和操纵进出服务器的网络流量。 Linux 防火墙的所有现代解决方案都利用该系统进行数据包过滤。如果没有用户管理,这个数据包过滤系统就没有什么用处。这就是 Iptables 的用武之地。当数据包到达系统时,它由 Netfilter 子系统处理,该子系统根据用户通过 iptables 提供给它的规则来决定是否接受、操作或拒绝它。因此,系统管理员和 Linux 专业人员需要 iptables 来管理他们的防火墙。为了使其变得更容易,有许多前端工具可用于简化此任务。其中一些工具包括 Shorewall、Firewalld、ufw、nftables、gufw 等。

Uncomplicated Firewall 缩写为 ufw 是基于 Debian 的系统的默认防火墙配置工具。该工具在 iptables 之上运行,以简化防火墙配置。它提供了一种简单且用户友好的方法来创建基于 IPv4 和 IPv6 主机的防火墙规则。尽管 UFW 不通过其 CLI 提供完整的防火墙功能,但它仍然可用于添加和删除简单的基于主机的规则。

下面是一个备忘单,可以捕获所有常见的 UFW 防火墙命令和示例。对于想要在系统上快速配置防火墙规则的系统管理员来说,这可以作为快速参考材料。

在 Linux 系统上安装 UFW

UFW 在基于 Debian 的系统上很常见。但是,该工具仍然可以在任何其他 Linux 系统上安装和使用。以下命令可用于在 Linux 上安装 UFW。

##On debian-based Systems
sudo apt update
sudo apt install ufw

##On RHEL-based Systems
sudo yum install epel-release
sudo yum install ufw

##On Fedora
sudo dnf install ufw

##On OpenSuse
sudo zypper install ufw

##On Arch Linux
sudo pacman -S ufw

##On Alpine Linux
sudo apk add ufw

使用以下命令验证安装:

$ ufw version
ufw 0.35
Copyright 2008-2015 Canonical Ltd.

1.1.启动、启用/禁用 UFW

安装后,确保服务已启动并启用:

sudo systemctl enable --now ufw

检查服务的状态:

$ systemctl status ufw
● ufw.service - Uncomplicated firewall
     Loaded: loaded (/usr/lib/systemd/system/ufw.service; enabled; vendor preset: disabled)
     Active: active (exited) since Sun 2023-09-17 11:20:36 CEST; 4s ago
       Docs: man:ufw(8)
             man:ufw-framework(8)
             file://usr/share/doc/ufw/README
    Process: 72156 ExecStart=/usr/libexec/ufw/ufw-init start (code=exited, status=0/SUCCESS)
   Main PID: 72156 (code=exited, status=0/SUCCESS)
        CPU: 80ms

您还可以使用以下命令检查状态:

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
SSH                        ALLOW       Anywhere                  
224.0.0.251 mDNS           ALLOW       Anywhere                  
SSH (v6)                   ALLOW       Anywhere (v6)             
ff02::fb mDNS              ALLOW       Anywhere (v6) 

如果未激活,请使用以下命令启用它:

$ sudo ufw enable
Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
Firewall is active and enabled on system startup

禁用它,请使用:

sudo ufw disable

常见 UFW 防火墙命令及示例

安装并激活后,您可以使用 UFW 在系统上设置所需的防火墙规则。以下是包含的常用命令。

2.1.阻止 IP 地址

UFW 允许用户创建一条规则来阻止来自特定 IP 地址的所有连接。要拒绝来自特定地址的流量,请使用具有以下语法的命令:

sudo ufw deny from <IP_Address>

在命令中,您需要正确替换您的IP地址。例如:

$ sudo ufw deny from 192.168.200.51
Rule added

在上面的命令中,我们设置系统删除来自源 192.168.200.51 的所有连接。要检查添加的规则,请运行:

$ sudo ufw status

To                         Action      From
--                         ------      ----
224.0.0.251 mDNS           ALLOW       Anywhere                  
Anywhere                   DENY        192.168.200.51            

2.2.阻止子网

除了阻止单个 IP 地址之外,您还可以阻止整个子网。为此,您可以使用以下语法的命令:

sudo ufw deny from <subnet>

例如:

$ sudo ufw deny from 192.168.200.0/24
Rule added

2.3.阻止网络接口

您可以阻止从 IP 地址或子网到特定网络接口的传入连接。为此,请使用以下命令:

sudo ufw deny in on <interface_name> from  <IP_Address>

例如:

$ sudo ufw deny in on eth0 from 192.168.200.51
Rule added

2.4.允许 IP 地址/网络接口

要允许来自特定地址的流量,您可以使用以下命令,并根据需要替换值。

sudo ufw allow from <IP_Address>

例如:

sudo ufw allow from 192.168.200.59

查看规则:

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
224.0.0.251 mDNS           ALLOW       Anywhere                  
Anywhere                   DENY        192.168.200.51            
Anywhere on eth0           DENY        192.168.200.51            
Anywhere on ens18          DENY        192.168.200.51            
22                         ALLOW       Anywhere                  
Anywhere                   ALLOW       192.168.200.59            
SSH (v6)                   ALLOW       Anywhere (v6)             
ff02::fb mDNS              ALLOW       Anywhere (v6)             
22 (v6)                    ALLOW       Anywhere (v6)  

要允许来自特定接口上的 IP 的传入流量,请使用:

sudo ufw allow in on <interface_name> from <IP_Address>

例如:

$ sudo ufw allow in on eth0 from 192.168.200.51
Rule updated

2.5.启用/禁用应用程序配置文件

UFW 允许用户阻止或允许来自应用程序的流量。要查看可用应用程序配置文件的列表,请使用:

$ sudo ufw app list
Available applications:
  AIM
  Bonjour
  CIFS
  DNS
  Deluge
  IMAP
  IMAPS
  IPP
  Icecream
  KDE Connect
  KTorrent
  Kerberos Admin
  Kerberos Full
  Kerberos KDC
  Kerberos Password
  LDAP
  LDAPS
  LPD
  MSN
  MSN SSL
  Mail submission
  NFS
  POP3
  POP3S
  PeopleNearby
  SMTP
  SSH
......

您还可以过滤给定的应用程序,例如 Nginx:

$ sudo ufw app list | grep Nginx
  Nginx Full
  Nginx HTTP
  Nginx HTTPS

要启用应用程序 pforiles,您需要在命令中提供应用程序配置文件的名称:

sudo ufw allow “<application_profile>”

例如:

$ sudo ufw allow "OpenSSH"
ufw allow "OpenSSH"
Rules updated
Rules updated (v6)

要查看启用的配置文件,请运行:

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
5432/tcp                   ALLOW       Anywhere                  
OpenSSH                    ALLOW       Anywhere                  
5432/tcp (v6)              ALLOW       Anywhere (v6)             
OpenSSH (v6)               ALLOW       Anywhere (v6)    

要禁用应用程序配置文件,请使用以下命令在需要时替换变量:

sudo ufw delete allow "<application_profile>”

例如,Nginx 有两个配置文件,Nginx Full 允许 HTTP 和 HTTPS 连接,Nginx HTTPS 只允许 HTTPS 连接。我们可以禁用已启用的 Nginx Full 配置文件并仅使用以下命令启用 HTTPS 配置文件:

##Disable the Nginx Full profile
sudo ufw delete allow "Nginx Full"

##Enable Nginx HTTPs
sudo ufw allow "Nginx HTTPS"

2.6。允许/阻止端口上的流量

除了启用/禁用应用程序配置文件之外,UFW 还允许您阻止/允许流向特定端口的流量。用于该操作的命令具有以下语法:

sudo ufw allow <port_number>

例如:

sudo ufw allow 80

您还可以指定服务名称。例如:

##For HTTP
sudo ufw allow http

##For HTTPS
sudo ufw allow https

要拒绝端口上的任何传出流量,请使用:

sudo ufw deny out <port_number>

例如,要丢弃 SMTP 端口 25 上的所有传出流量,请使用:

sudo ufw deny out 25

您可以使用以下命令拒绝来自给定端口上特定地址的流量:

sudo ufw deny from <ip address> to <protocol> port <port number>

例如:

sudo ufw deny from 192.168.20.51 to any port 3306

2.7.允许端口上来自特定地址的流量

还可以允许端口上来自特定地址的流量。对于这种情况,您将使用以下命令:

##From IP address
sudo ufw allow from <IP_Address> proto tcp to any port <port_number>

##Form subnet
sudo ufw allow from <subnet> proto tcp to any port <port_number>

例如,允许来自特定地址的 SSH,您将使用以下命令:

sudo ufw allow from 192.168.200.51 proto tcp to any port 22

要允许来自特定子网的 MySQL 连接:

sudo ufw allow from 192.168.200.0/24 proto tcp to any port 3306

2.8.如何删除规则

在某些情况下,您需要删除 UFW 上设置的 fireall 规则。为此,首先列出可用的规则:

$ sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 5432/tcp                   ALLOW IN    Anywhere                  
[ 2] OpenSSH                    ALLOW IN    Anywhere                  
[ 3] Nginx HTTPS                ALLOW IN    Anywhere                  
[ 4] 80                         ALLOW IN    Anywhere                  
[ 5] 80/tcp                     ALLOW IN    Anywhere                  
[ 6] 25                         DENY OUT    Anywhere                   (out)
[ 7] 3306                       DENY IN     192.168.20.51             
[ 8] 3306/tcp                   ALLOW IN    192.168.200.0/24          
[ 9] Anywhere                   ALLOW IN    192.168.200.51            
[10] 5432/tcp (v6)              ALLOW IN    Anywhere (v6)             
[11] OpenSSH (v6)               ALLOW IN    Anywhere (v6)             
[12] Nginx HTTPS (v6)           ALLOW IN    Anywhere (v6)             
[13] 80 (v6)                    ALLOW IN    Anywhere (v6)             
[14] 80/tcp (v6)                ALLOW IN    Anywhere (v6)             
[15] 25 (v6)                    DENY OUT    Anywhere (v6)              (out)

要删除所需的规则,请使用具有以下语法的命令

##Using the target specification
sudo ufw delete <action> from <target>

##Using the Rule number
sudo ufw delete <rule_number>

例如,删除 192.168.200.51 中的允许规则,命令将为:

sudo ufw delete allow from 192.168.200.51

您还可以通过提供规则编号来删除该规则。例如:

sudo ufw delete 9

验证更改:

$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
5432/tcp                   ALLOW       Anywhere                  
OpenSSH                    ALLOW       Anywhere                  
Nginx HTTPS                ALLOW       Anywhere                  
80                         ALLOW       Anywhere                  
80/tcp                     ALLOW       Anywhere                  
3306                       DENY        192.168.20.51             
3306/tcp                   ALLOW       192.168.200.0/24          
5432/tcp (v6)              ALLOW       Anywhere (v6)             
OpenSSH (v6)               ALLOW       Anywhere (v6)             
Nginx HTTPS (v6)           ALLOW       Anywhere (v6)             
80 (v6)                    ALLOW       Anywhere (v6)             
80/tcp (v6)                ALLOW       Anywhere (v6)             

25                         DENY OUT    Anywhere                  
25 (v6)                    DENY OUT    Anywhere (v6)

结论

这标志着本关于如何安装和使用 UFW 配置基于主机的防火墙规则的指南的结束。本指南仅提供基本和常用的命令。要探索更多信息,请访问 UFW 文档页面。

查看更多:

  • 如何在 Windows Server 防火墙中打开端口
  • 在 Debian 上安装和配置 Firewalld
  • 在 RHEL/CentOS 上安装和使用 CSF 防火墙

更新于:2个月前
赞一波!

文章评论

全部评论