温馨提示×

如何在SecureCRT中配置Debian服务器的防火墙

小樊
52
2025-08-15 22:25:25
栏目: 云计算

SecureCRT 本身不直接配置防火墙,需通过 SSH 连接到 Debian 服务器后,使用系统防火墙工具(如 ufwiptables)操作。以下是具体步骤:

一、使用 UFW 配置(推荐,简单易用)

1. 安装并启用 UFW

sudo apt update
sudo apt install ufw  # 安装UFW
sudo ufw enable       # 启用UFW
sudo ufw default deny incoming  # 默认拒绝入站流量
sudo ufw default allow outgoing # 允许出站流量

2. 添加防火墙规则

  • 允许 SSH(默认 22 端口)
    sudo ufw allow ssh/tcp  # 或 sudo ufw allow 22/tcp
    
  • 允许 HTTP/HTTPS
    sudo ufw allow http/tcp  # 允许HTTP(80端口)
    sudo ufw allow https/tcp # 允许HTTPS(443端口)
    
  • 允许特定 IP 访问
    sudo ufw allow from 192.168.1.100/32  # 允许单个IP
    sudo ufw deny from 192.168.1.0/24     # 拒绝整个网段
    

3. 保存并验证规则

sudo ufw save       # 保存规则(Debian 10+需安装`iptables-persistent`)
sudo ufw status verbose  # 查看规则详情

二、使用 iptables 配置(需手动保存规则)

1. 安装 iptables

sudo apt update
sudo apt install iptables

2. 添加规则示例

  • 允许 SSH
    sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
    
  • 拒绝其他入站流量
    sudo iptables -P INPUT DROP  # 设置默认策略为拒绝
    sudo iptables -A INPUT -i lo -j ACCEPT  # 允许本地回环
    

3. 保存规则(关键步骤)

sudo iptables-save > /etc/iptables/rules.v4  # 保存规则
sudo apt install iptables-persistent  # 安装持久化工具(可选)
sudo netfilter-persistent save  # 保存规则到持久化文件
sudo netfilter-persistent reload  # 重启后加载规则

三、在 SecureCRT 中连接验证

  1. 打开 SecureCRT,新建 SSH 连接,输入 Debian 服务器 IP 和端口(默认 22)。
  2. 确保连接设置中 防火墙选项为“None”(或无需特殊配置)。
  3. 登录后执行上述防火墙命令,验证规则是否生效(如 sudo ufw statussudo iptables -L)。

注意事项

  • 优先使用 UFW:操作更简洁,适合新手,规则保存更可靠。
  • 规则顺序影响iptables 规则按添加顺序匹配,需确保允许规则在拒绝规则之前。
  • 保存规则iptables 规则默认重启后丢失,必须通过 iptables-saveiptables-persistent 保存。

参考来源:

0