首页主机资讯Debian Minimal怎样配置防火墙

Debian Minimal怎样配置防火墙

时间2025-12-01 13:39:06发布访客分类主机资讯浏览422
导读:Debian Minimal 防火墙配置指南 一、前置检查与原则 确认系统版本与网络接口:cat /etc/os-release;ip a(记下公网/内网接口,如 eth0)。 原则:先允许必要流量(如 SSH 22/TCP),再设置默认...

Debian Minimal 防火墙配置指南

一、前置检查与原则

  • 确认系统版本与网络接口:cat /etc/os-release;ip a(记下公网/内网接口,如 eth0)。
  • 原则:先允许必要流量(如 SSH 22/TCP),再设置默认拒绝;始终允许 回环接口 lo已建立/相关连接,避免把自己锁在外面。
  • 远程操作务必谨慎:建议先开一个 screen/tmux 会话,或先允许当前 SSH 会话所在 IP,再收紧策略。

二、方案一 使用 UFW 快速配置(推荐给新手)

  • 安装与启用:
    • sudo apt update
    • sudo apt install ufw
    • sudo ufw enable
  • 常用规则(按需调整端口):
    • 允许 SSH:sudo ufw allow 22/tcp
    • 允许 HTTP/HTTPS:sudo ufw allow 80/tcp;sudo ufw allow 443/tcp
    • 允许某来源 IP:sudo ufw allow from 203.0.113.10
    • 查看状态与规则:sudo ufw status verbose
  • 说明:UFW 是 iptables 的前端,规则简单、易维护,适合 Debian Minimal 快速上线。

三、方案二 使用 iptables 手工配置(更灵活)

  • 安装与查看:
    • sudo apt update
    • sudo apt install iptables
    • sudo iptables -L -n -v
  • 基本规则模板(示例允许 22/80/443 与 ping):
    • 允许回环与已建立/相关:
      • sudo iptables -A INPUT -i lo -j ACCEPT
      • sudo iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
    • 按需放行端口:
      • 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
      • 可选:允许 ping(ICMPv4):sudo iptables -A INPUT -p icmp --icmp-type echo-request -j ACCEPT
    • 默认策略(谨慎:确保已放行 SSH,否则会断开):
      • sudo iptables -P INPUT DROP
      • sudo iptables -P FORWARD DROP
      • sudo iptables -P OUTPUT ACCEPT
  • 持久化保存与恢复:
    • 安装持久化包并按提示保存:sudo apt install iptables-persistent
    • 或手动保存/恢复:
      • 保存:sudo sh -c “iptables-save > /etc/iptables/rules.v4
      • 恢复:sudo iptables-restore < /etc/iptables/rules.v4
    • 开机自动加载(if-pre-up 钩子):
      • 新建:sudo tee /etc/network/if-pre-up.d/iptables < < ‘EOF’ #!/bin/sh /sbin/iptables-restore < /etc/iptables/rules.v4 EOF
      • 赋权:sudo chmod +x /etc/network/if-pre-up.d/iptables
  • 说明:Debian 默认不会自动保存 iptables 规则,需借助 iptables-persistent 或启动钩子实现持久化。

四、方案三 使用 nftables(现代替代方案)

  • 安装与保存:
    • sudo apt update
    • sudo apt install nftables
    • 保存规则:sudo nft list ruleset > /etc/nftables.conf
  • 示例最小规则集(允许 22/80/443 与回环、已建立连接):
    • 新建表与链:
      • sudo nft add table ip filter
      • sudo nft add chain ip filter input { type filter hook input priority 0 ; policy drop ; }
      • sudo nft add chain ip filter output { type filter hook output priority 0 ; policy accept ; }
    • 规则:
      • sudo nft add rule ip filter input iif lo accept
      • sudo nft add rule ip filter input ct state established,related accept
      • sudo nft add rule ip filter input tcp dport 22 accept
      • sudo nft add rule ip filter input tcp dport 80 accept
      • sudo nft add rule ip filter input tcp dport 443 accept
  • 说明:nftablesiptables 的后继,语法更简洁,适合新部署;同样需确保持久化保存并在故障时可快速回滚。

五、验证与运维要点

  • 验证连通性:ss -tnlp | grep -E ‘(:22|:80|:443)’; ping -c 4 8.8.8.8;从外部测试端口连通。
  • 避免锁死自己:在设置默认 DROP 前,先放行当前 SSH 来源 IP;或临时在 VNC/控制台 操作。
  • 备份与回滚:
    • iptables:iptables-save > /root/iptables-$(date +%F).bak
    • nftables:nft list ruleset > /root/nftables-$(date +%F).bak
  • 日志与监控:可添加日志规则(如 iptables -A INPUT -j LOG --log-prefix "IPT-DROP: "),并结合 journalctl -k/var/log/kern.log 观察。
  • 云服务器注意:安全组/防火墙可能叠加,需在控制台同步放行相同端口。

声明:本文内容由网友自发贡献,本站不承担相应法律责任。对本内容有异议或投诉,请联系2913721942#qq.com核实处理,我们将尽快回复您,谢谢合作!


若转载请注明出处: Debian Minimal怎样配置防火墙
本文地址: https://pptw.com/jishu/760051.html
C++在Linux如何进行多线程 ubuntu syslog怎么归档

游客 回复需填写必要信息