在 Debian 系统中,防火墙通常使用 ufw(Uncomplicated Firewall) 或直接使用 iptables / nftables。下面按最常见、最推荐的方式给你说明。
sudo apt update
sudo apt install ufw
sudo ufw enable
查看状态:
sudo ufw status
sudo ufw allow ssh # 开放 SSH(22)
sudo ufw allow http # 开放 HTTP(80)
sudo ufw allow https # 开放 HTTPS(443)
sudo ufw allow 8080
sudo ufw allow 3306
指定协议:
sudo ufw allow 53/tcp
sudo ufw allow 53/udp
只允许某个 IP 访问 SSH:
sudo ufw allow from 192.168.1.100 to any port 22
sudo ufw delete allow 8080
sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
sudo apt install iptables-persistent
sudo netfilter-persistent save
⚠️ iptables 配置重启后不保存,需要持久化。
Debian 10 以后默认使用 nftables 替代 iptables。
sudo apt install nftables
sudo nft add rule inet filter input tcp dport 22 accept
(实际生产环境建议配置 /etc/nftables.conf)
| 服务 | 端口 |
|---|---|
| SSH | 22 |
| HTTP | 80 |
| HTTPS | 443 |
| MySQL | 3306 |
| PostgreSQL | 5432 |
| Redis | 6379 |
如果你愿意告诉我:
我可以给你一套最合适、最安全的完整配置方案。