↓

ubuntu中ufw的使用方法

··

在新版本(18.04+)的ubuntu中,ufw取代了iptables作为默认的防火墙管理工具。其使用方法比iptables简单。

ufw默认规则阻止所有的外来连接(incoming),允许所有的出口连接(outgoing),阻止路由连接(routed)。

基本使用方法:

  1. 首先配置默认规则,如果上述规则不符合需求

    1
    
    ufw default allow incoming
  2. 然后允许ssh连接(重要,否则启用后无法通过ssh连接服务器)

    1
    2
    3
    
    ufw allow ssh
    # 如果自定义了ssh端口,则需要
    ufw allow from any port 2222
  3. 然后启用

    1
    
    ufw enable
  4. 然后定义自定义规则

    1
    2
    3
    4
    5
    6
    
    # 允许来自192.168.*.*到端口3333的连接
    ufw allow from 192.168.0.0/16 to any port 3333
    # 拒绝来自10.0.1.1的任何连接
    ufw deny from 10.0.1.1
    # 拒绝来自47.68.*.*到端口1234的连接
    ufw deny from 47.68.0.0/16 to any port 1234
  5. 可以通过以下命令维护ufw

     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    
    # 显示所有自定义规则
    ufw status
    # 显示所有自定义规则,并显示规则序号
    ufw status numbered
    # 显示包括默认规则在内的所有规则
    ufw status verbose
    # 删除序号1对应的规则
    ufw delete 1
    # 停用ufw
    ufw disable
  6. 完成