首頁·終端機命令

ufw

網路

用法

sudo ufw <allow|deny|status> ...

在 Debian 和 Ubuntu 上用簡單詞彙包裝 iptables 的防火牆;`enable` 之前先放通 SSH,否則會切斷你正坐著的這條連線,而 Docker 把埠直接插進 iptables,ufw 的規則管不到那裡。

常用選項

選項含義
allow 22/tcpOpen a port; allow OpenSSH uses the named application profile instead.
allow from 10.0.0.0/8 to any port 5432Open a port to one network only.
deny <port>Block it explicitly.
default deny incomingThe usual starting point, set before you enable.
status numberedList the rules with the numbers that delete needs.
delete <n>Remove rule number n.
enable / disableTurn the firewall on or off, persistently.

範例

sudo ufw allow OpenSSH && sudo ufw enable

Allow SSH first, then switch the firewall on.

sudo ufw status numbered

See what is open and get the rule numbers.

sudo ufw delete 3

Remove the third rule.

連不上的時候,這些能把範圍縮小——是名稱解析不了、沒有路由,還是連接埠被擋。

怎麼看

  • 方括號 [ ] 表示這部分可以不寫。
  • 省略號 … 表示可以寫多個。
  • 選項區分大小寫——有些命令裡 -r 和 -R 不是一回事。

常見問題

Q. ufw 是做什麼的?

在 Debian 和 Ubuntu 上用簡單詞彙包裝 iptables 的防火牆;`enable` 之前先放通 SSH,否則會切斷你正坐著的這條連線,而 Docker 把埠直接插進 iptables,ufw 的規則管不到那裡。

Q. 怎麼寫?

sudo ufw <allow|deny|status> ... —— 方括號表示可以省略的部分。

Q. 值得記的選項有幾個?

這裡列了 7 個,完整列表在 man ufw。這條命令屬於網路。

相關命令

man 手冊: man ufw