Debian Stream 8如何设置防火墙规则
导读:Debian Stream 8 防火墙规则设置指南 工具选择与说明 在 Debian 10(Buster)及之后版本(含 Debian Stream 8),系统默认使用 nftables 作为内核的防火墙框架;iptables 仍可用但底...
Debian Stream 8 防火墙规则设置指南
工具选择与说明
- 在 Debian 10(Buster)及之后版本(含 Debian Stream 8),系统默认使用 nftables 作为内核的防火墙框架;iptables 仍可用但底层已转向 nftables。若你倾向传统语法,可继续使用 iptables;若希望面向未来,建议直接使用 nftables。Debian 家族中常见的简化工具 ufw 在 Ubuntu 上默认提供,在 Debian 上可按需安装;而 firewalld 多见于 RHEL/CentOS/Fedora,并非 Debian 的默认选择。
方案一 使用 nftables(推荐)
- 安装与启用
- 安装组件:sudo apt update & & sudo apt install -y nftables
- 启用开机启动:sudo systemctl enable --now nftables
- 基本规则模板(示例)
- 查看当前规则:sudo nft list ruleset
- 允许回环与已建立连接:
- sudo nft add rule inet filter input iif lo accept
- sudo nft add rule inet filter input ct state established,related accept
- 放行常用服务(SSH/HTTP/HTTPS/DNS):
- sudo nft add rule inet filter input tcp dport { 22, 80, 443 } accept
- sudo nft add rule inet filter input udp dport 53 accept
- 默认策略(谨慎:确保已放行 SSH,否则可能被锁):
- sudo nft chain inet filter input { policy drop ; }
- 持久化与回滚
- 保存规则:sudo nft list ruleset > /etc/nftables.conf
- 系统启动会自动加载 /etc/nftables.conf;如需回滚,可用备份覆盖该文件后执行:sudo systemctl restart nftables。
方案二 使用 iptables(传统方式)
- 安装与持久化
- 安装:sudo apt update & & sudo apt install -y iptables
- 持久化方案:sudo apt install -y iptables-persistent
- 安装过程中会提示是否保存当前规则;后续可用:sudo netfilter-persistent save 与 sudo netfilter-persistent start
- 基本规则模板(示例)
- 查看规则:sudo iptables -L -n -v
- 允许回环与已建立连接:
- sudo iptables -A INPUT -i lo -j ACCEPT
- sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
- 放行常用服务(SSH/HTTP/HTTPS/DNS):
- sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
- sudo iptables -A INPUT -p tcp --dport 80 -j ACCEPT
- sudo iptables -A INPUT -p tcp --dport 443 -j ACCEPT
- sudo iptables -A INPUT -p udp --dport 53 -j ACCEPT
- 默认策略(谨慎:确保已放行 SSH):
- sudo iptables -P INPUT DROP
- 备份与恢复
- 备份:sudo iptables-save > /etc/iptables/rules.v4
- 恢复:sudo iptables-restore < /etc/iptables/rules.v4。
方案三 使用 UFW(仅在需要时安装)
- 安装与启用
- 安装:sudo apt update & & sudo apt install -y ufw
- 启用并设默认拒绝入站:sudo ufw enable & & sudo ufw default deny incoming
- 常用命令
- 放行端口/服务:sudo ufw allow 22/tcp;sudo ufw allow 80,443/tcp;sudo ufw allow http,https
- 按来源限制:sudo ufw allow from 192.168.1.0/24 to any port 22
- 查看与删除:sudo ufw status numbered;sudo ufw delete [编号/规则]
- 说明
- UFW 并非 Debian 默认组件,但在 Debian 上可用;适合快速配置与桌面/简单服务器场景。
验证与运维要点
- 验证端口监听与连通
- 本地监听:ss -tulpen | grep -E ‘(:22|:80|:443)’
- 外部探测:nmap -p 22,80,443 your_server_ip
- 安全与变更建议
- 原则:默认拒绝入站,仅放行必要端口;对 SSH 建议限制来源 IP 或改用密钥登录并禁用密码。
- 变更前备份规则;远程修改防火墙时,优先放行 SSH 22/TCP 并使用带外/控制台应急通道,避免被锁。
声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!
若转载请注明出处: Debian Stream 8如何设置防火墙规则
本文地址: https://pptw.com/jishu/788253.html
