首页·终端命令

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