Linux 防火牆 ufw 簡介

Linux 防火牆 ufw 簡介 | Linux 中國

我們來研究下 Linux 上的 ufw(簡單防火牆),為你更改防火牆提供一些見解和命令。-- Sandra Henry-stocker(作者)


ufw( 簡單防火牆(Uncomplicated FireWall))真正地簡化了 iptables ,它從出現的這幾年,已經成為 Ubuntu 和 Debian 等系統上的默認防火牆。而且 ufw 出乎意料的簡單,這對新管理員來說是一個福音,否則他們可能需要投入大量時間來學習防火牆管理。

ufw 也有 GUI 客戶端(例如 gufw),但是 ufw 命令通常在命令行上執行的。本文介紹了一些使用 ufw 的命令,並研究了它的工作方式。

首先,快速查看 ufw 配置的方法是查看其配置文件 —— /etc/default/ufw。使用下面的命令可以查看其配置,使用 grep 來抑制了空行和註釋(以 # 開頭的行)的顯示。

<code>$ grep -v '^#\\|^$' /etc/default/ufw
IPV6=yes
DEFAULT_INPUT_POLICY="DROP"
DEFAULT_OUTPUT_POLICY="ACCEPT"
DEFAULT_FORWARD_POLICY="DROP"
DEFAULT_APPLICATION_POLICY="SKIP"
MANAGE_BUILTINS=no
IPT_SYSCTL=/etc/ufw/sysctl.conf
IPT_MODULES="nf_conntrack_ftp nf_nat_ftp nf_conntrack_netbios_ns"/<code>

正如你所看到的,默認策略是丟棄輸入但允許輸出。允許你接受特定的連接的其它規則是需要單獨配置的。

ufw 命令的基本語法如下所示,但是這個概要並不意味著你只需要輸入 ufw 就行,而是一個告訴你需要哪些參數的快速提示。

<code>ufw [--dry-run] [options] [rule syntax]/<code>

--dry-run 選項意味著 ufw 不會運行你指定的命令,但會顯示給你如果執行後的結果。但是它會顯示假如更改後的整個規則集,因此你要做有好多行輸出的準備。

要檢查 ufw 的狀態,請運行以下命令。注意,即使是這個命令也需要使用 sudo 或 root 賬戶。

<code>$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       192.168.0.0/24
9090                       ALLOW       Anywhere
9090 (v6)                  ALLOW       Anywhere (v6)/<code>

否則,你會看到以下內容:

<code>$ ufw status
ERROR: You need to be root to run this>

加上 verbose 選項會提供一些其它細節:

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

To                         Action      From
--                         ------      ----

22                         ALLOW IN    192.168.0.0/24
9090                       ALLOW IN    Anywhere
9090 (v6)                  ALLOW IN    Anywhere (v6)/<code>

你可以使用以下命令輕鬆地通過端口號允許和拒絕連接:

<code>$ sudo ufw allow 80         <== 允許 http 訪問
$ sudo ufw deny 25              <== 拒絕 smtp 訪問/<code>

你可以查看 /etc/services 文件來找到端口號和服務名稱之間的聯繫。

<code>$ grep 80/ /etc/services
http            80/tcp          www             # WorldWideWeb HTTP
socks           1080/tcp                        # socks proxy server
socks           1080/udp
http-alt        8080/tcp        webcache        # WWW caching service
http-alt        8080/udp
amanda          10080/tcp                       # amanda backup services
amanda          10080/udp
canna           5680/tcp                        # cannaserver/<code>

或者,你可以命令中直接使用服務的名稱。

<code>$ sudo ufw allow http
Rule added
Rule added (v6)
$ sudo ufw allow https
Rule added
Rule added (v6)/<code>

進行更改後,你應該再次檢查狀態來查看是否生效:

<code>$ sudo ufw status
Status: active

To                         Action      From
--                         ------      ----
22                         ALLOW       192.168.0.0/24
9090                       ALLOW       Anywhere
80/tcp                     ALLOW       Anywhere         <==
443/tcp                    ALLOW       Anywhere         <==

9090 (v6)                  ALLOW       Anywhere (v6)
80/tcp (v6)                ALLOW       Anywhere (v6)    <==
443/tcp (v6)               ALLOW       Anywhere (v6)    <==/<code>

ufw 遵循的規則存儲在 /etc/ufw 目錄中。注意,你需要 root 用戶訪問權限才能查看這些文件,每個文件都包含大量規則。

<code>$ ls -ltr /etc/ufw
total 48
-rw-r--r-- 1 root root 1391 Aug 15  2017 sysctl.conf
-rw-r----- 1 root root 1004 Aug 17  2017 after.rules
-rw-r----- 1 root root  915 Aug 17  2017 after6.rules
-rw-r----- 1 root root 1130 Jan  5  2018 before.init
-rw-r----- 1 root root 1126 Jan  5  2018 after.init
-rw-r----- 1 root root 2537 Mar 25  2019 before.rules
-rw-r----- 1 root root 6700 Mar 25  2019 before6.rules
drwxr-xr-x 3 root root 4096 Nov 12 08:21 applications.d
-rw-r--r-- 1 root root  313 Mar 18 17:30 ufw.conf
-rw-r----- 1 root root 1711 Mar 19 10:42 user.rules
-rw-r----- 1 root root 1530 Mar 19 10:42 user6.rules/<code>

本文前面所作的更改,為 http 訪問添加了端口 80 和為 https 訪問添加了端口 443,在 user.rules 和 user6.rules 文件中看起來像這樣:

<code># grep " 80 " user*.rules
user6.rules:### tuple ### allow tcp 80 ::/0 any ::/0 in
user6.rules:-A ufw6-user-input -p tcp --dport 80 -j ACCEPT
user.rules:### tuple ### allow tcp 80 0.0.0.0/0 any 0.0.0.0/0 in
user.rules:-A ufw-user-input -p tcp --dport 80 -j ACCEPT
You have new mail in /var/mail/root
# grep 443 user*.rules
user6.rules:### tuple ### allow tcp 443 ::/0 any ::/0 in
user6.rules:-A ufw6-user-input -p tcp --dport 443 -j ACCEPT
user.rules:### tuple ### allow tcp 443 0.0.0.0/0 any 0.0.0.0/0 in
user.rules:-A ufw-user-input -p tcp --dport 443 -j ACCEPT/<code>

使用 ufw,你還可以使用以下命令輕鬆地阻止來自一個 IP 地址的連接:

<code>$ sudo ufw deny from 208.176.0.50
Rule added/<code>

status 命令將顯示更改:

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

To                         Action      From
--                         ------      ----
22                         ALLOW IN    192.168.0.0/24
9090                       ALLOW IN    Anywhere
80/tcp                     ALLOW IN    Anywhere
443/tcp                    ALLOW IN    Anywhere
Anywhere                   DENY IN     208.176.0.50             <== new
9090 (v6)                  ALLOW IN    Anywhere (v6)
80/tcp (v6)                ALLOW IN    Anywhere (v6)
443/tcp (v6)               ALLOW IN    Anywhere (v6)/<code>

總而言之,ufw 不僅容易配置,而且且容易理解。


via: https://www.networkworld.com/article/3533551/linux-firewall-basics-with-ufw.html

作者: Sandra Henry-Stocker 選題: lujun9972 譯者: MjSeven 校對: wxy

本文由 LCTT 原創編譯, Linux中國 榮譽推出


分享到:


相關文章: