Ubuntu Server 20.04.1 預設 UFW 防火牆 Firewall 設定規則詳解和教學 - MIS 腳印
MIS 腳印 logo

MIS 腳印

記錄 IT 學習的軌跡

Ubuntu Server 20.04.1 預設 UFW 防火牆 Firewall 設定規則詳解和教學

在 Ubuntu Server 啟用和停用 UFW 預設防火牆,並詳細解說如何針對特定 IP、網段及 port,來各別設定允許或拒絕的規則。

Ubuntu

Ubuntu 預設安裝的 Firewall (防火牆) 組件是 UFW,且預設為未啟用狀態:

sudo ufw status
Status: inactive

UFW 啟用和停用

啟用 UFW,且開機自動載入和啟用:

sudo ufw enable
Firewall is active and enabled on system startup

停用 UFW,且開機停用:

sudo ufw disable
Firewall stoped and disabled on system startup

設定 UFW 預設允許、拒絕所有連線

可依下述設定預設允許或拒絕所有連結,在依需求設定服務 port 的規則。

允許所有連線

較少場景會預設允許所有連線:

sudo ufw default allow
Default incoming policy changed to 'allow'
(be sure to update your rules accordingly)

拒絕所有連線

通常會先預設拒絕所有連線,在各別設定允許服務 port 規則。

sudo ufw default deny
Default incoming policy changed to 'deny'
(be sure to update your rules accordingly)

通訊埠對應的服務名稱

所有服務名稱對應的通訊埠 (port) 可查看檔案 /etc/services,在設定 UFW 規則時即可直接使用服務名稱即可,例如查看開頭為 ssh 和 http 相關服務名稱的對應通訊埠:

cat /etc/services | egrep '^ssh|^http'
ssh             22/tcp                          # SSH Remote Login Protocol
http            80/tcp          www             # WorldWideWeb HTTP
https           443/tcp                         # http protocol over TLS/SSL
http-alt        8080/tcp        webcache        # WWW caching service

設定允許、拒絕規則

特定 port

設定對外允許的 port (不限制特定 IP),例如允許 http 的 80 port:

sudo ufw allow 80
Rule added
Rule added (v6)
指令 說明
prefix allow/deny port/protocol
sudo ufw allow 80 允許 port 80 的 tcp and udp
80/tcp 允許 port 80 的 tcp
80/udp 允許 port 80 的 udp
deny 80 拒絕 port 80 的 tcp and udp
80/tcp 拒絕 port 80 的 tcp
80/udp 拒絕 port 80 的 udp

指定 IP 連線開放所有 port

簡單說就是設定白、黑名單的 IP。

prefix allow/deny 指定 IP
sudo ufw allow from 192.168.1.200
deny

指定 IP 的特定 port

僅允許指定 IP 連結特定 port。

prefix allow/deny 指定 IP port/protocol
sudo ufw allow from 192.168.1.200 to any port 80
deny

指定 IP 整個網段的特定 port

針對允許 IP 整個網段連結特定 port。

prefix allow/deny 指定 IP 子網域 port/protocol
sudo ufw allow from 192.168.1.0/24 to any port 80
deny

查看 UFW 狀態

查看 UFW 一般狀態:

sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW       61.216.xx.xx
80                         ALLOW       Anywhere
80 (v6)                    ALLOW       Anywhere (v6)

查看 UFW 狀態較多資訊:

sudo ufw status verbose
Status: active
Logging: on (low)
Default: deny (incoming), allow (outgoing), disabled (routed)
New profiles: skip

To                         Action      From
--                         ------      ----
22/tcp                     ALLOW IN    61.216.xx.xx
80                         ALLOW IN    Anywhere
80 (v6)                    ALLOW       Anywhere (v6)

刪除 UFW 規則

可直接使用設定 UFW 時的規則,加上 delete 參數即可:

sudo ufw delete allow 80
Rule deleted
Rule deleted (v6)

使用序號

先查看 UFW 規則序號:

sudo ufw status numbered
Status: active

     To                         Action      From
     --                         ------      ----
[ 1] 22/tcp                     ALLOW IN    61.216.xx.xx
[ 2] 80                         ALLOW IN    Anywhere
[ 3] 80 (v6)                    ALLOW IN    Anywhere (v6)

刪除序號 [ 2] 規則:

sudo ufw delete 3
Deleting:
 allow 80
Proceed with operation (y|n)? y
Rule deleted (v6)

重設 UFW 規則

如果 UFW 規則改的太糟太亂想重新來過的話,可重設 UFW 規則:

sudo ufw reset
Resetting all rules to installed defaults. This may disrupt existing ssh
connections. Proceed with operation (y|n)? y
Backing up 'user.rules' to '/etc/ufw/user.rules.20210217_211451'
Backing up 'before.rules' to '/etc/ufw/before.rules.20210217_211451'
Backing up 'after.rules' to '/etc/ufw/after.rules.20210217_211451'
Backing up 'user6.rules' to '/etc/ufw/user6.rules.20210217_211451'
Backing up 'before6.rules' to '/etc/ufw/before6.rules.20210217_211451'
Backing up 'after6.rules' to '/etc/ufw/after6.rules.20210217_211451'

發表迴響