在 Debian 系统中,防火墙通常是通过 ufw(简单前端)或 nftables / iptables(底层工具)来管理的。下面按常见方式给你说明如何“开启防火墙服务”。
sudo apt update
sudo apt install ufw
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw allow 22/tcp
# 或
sudo ufw allow ssh
sudo ufw enable
sudo ufw status
✅ 防火墙已作为服务自动开机启动
sudo apt install nftables
编辑或创建:
sudo nano /etc/nftables.conf
示例规则:
table inet filter {
chain input {
type filter hook input priority 0;
policy drop;
ct state established,related accept;
iif "lo" accept;
tcp dport 22 accept;
}
}
sudo systemctl enable nftables
sudo systemctl start nftables
sudo systemctl status nftables
Debian 新版本已逐步用 nftables 替代 iptables
sudo apt install iptables
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
sudo iptables -P INPUT DROP
保存规则(取决于版本):
sudo iptables-save > /etc/iptables/rules.v4
systemctl is-enabled ufw
# 或
systemctl is-enabled nftables
| 场景 | 推荐 |
|---|---|
| 个人 / 服务器新手 | UFW |
| 专业 / 定制化 | nftables |
| 老系统兼容 | iptables |
如果你告诉我 Debian 版本(如 11 / 12) 和 使用场景(家庭 / 云服务器),我可以给你更精确的规则配置。