Skip to content

防火墙

Debian

Debian 默认使用并推荐 nftables 防火墙框架。配置文件为 /etc/nftables.conf,规则可同时处理 IPv4 和 IPv6。

安装与管理

bash
# 安装
apt install nftables

# 启动并设置开机启动
systemctl enable --now nftables

# 查看服务状态
systemctl status nftables

# 查看当前规则
nft list ruleset

# 检查配置文件语法,不应用规则
nft --check --file /etc/nftables.conf

# 重新加载规则
systemctl reload nftables

基础规则

bash
#!/usr/sbin/nft -f

flush ruleset

table inet filter {
  chain input {
    type filter hook input priority filter; policy drop;

    iifname "lo" accept
    ct state established,related accept
    ct state invalid drop

    ip protocol icmp accept
    meta l4proto ipv6-icmp accept

    tcp dport 22 accept
  }

  chain forward {
    type filter hook forward priority filter; policy drop;
  }

  chain output {
    type filter hook output priority filter; policy accept;
  }
}

启用前应根据实际服务调整开放端口。远程服务器必须先保留 SSH 端口,并在应用规则前确认配置语法。

参考

Ubuntu

UFW 是 Ubuntu 默认的防火墙配置工具,初始状态通常为禁用。

安装与管理

bash
# 安装
apt install ufw

# 远程服务器先允许 SSH
ufw allow OpenSSH

# 启用
ufw enable

# 禁用
ufw disable

# 查看状态
ufw status verbose

规则

bash
# 开放端口
ufw allow 8388/tcp

# 拒绝端口
ufw deny 8388/tcp

# 查看带编号的规则
ufw status numbered

# 删除规则
ufw delete allow 8388/tcp

参考

基于 MIT 许可发布