温馨提示×

debian防火墙怎么放行IP

小樊
59
2026-10-25 21:08:42
栏目: 智能运维

在 Debian 里“防火墙”通常有两层含义:iptables / nftables 或 UFW(简化前端)。下面按常见场景说明。


一、使用 UFW(最推荐,简单)

1. 安装并启用

sudo apt update
sudo apt install ufw
sudo ufw enable

2. 放行指定 IP(全部端口)

sudo ufw allow from 1.2.3.4

3. 放行指定 IP 的指定端口

sudo ufw allow from 1.2.3.4 to any port 22

4. 放行一个 IP 段

sudo ufw allow from 192.168.1.0/24

5. 查看规则

sudo ufw status

二、使用 iptables(底层方式)

放行某个 IP 的所有访问

iptables -A INPUT -s 1.2.3.4 -j ACCEPT

放行某个 IP 访问 22 端口

iptables -A INPUT -p tcp -s 1.2.3.4 --dport 22 -j ACCEPT

保存规则(Debian)

apt install iptables-persistent
netfilter-persistent save

三、使用 nftables(新系统推荐)

编辑配置

nano /etc/nftables.conf

示例:

table inet filter {
    chain input {
        type filter hook input priority 0;
        ip saddr 1.2.3.4 accept
    }
}

应用

nft -f /etc/nftables.conf
systemctl enable nftables

四、常见注意点

  • 顺序很重要:UFW / iptables 是“先匹配先生效”
  • 若已 deny all,必须先 allow 再 deny
  • 云服务器还需检查 安全组
  • SSH 放行前确保自己不会被锁外面

如果你能告诉我:

  • Debian 版本
  • 用的是 UFW / iptables / nftables
  • 要放行哪个 IP、哪个端口

我可以给你精确命令。

0 踩