温馨提示×

Debian FTP Server防火墙设置

小樊
44
2025-06-27 02:27:12
栏目: 智能运维

在Debian系统上设置FTP服务器的防火墙可以通过使用 iptablesufw(Uncomplicated Firewall) 来实现。以下是使用这两种工具的详细步骤:

使用 ufw 设置防火墙

  1. 安装ufw(如果尚未安装)

    sudo apt update sudo apt install ufw 
  2. 启用ufw

    sudo ufw enable 
  3. 允许FTP流量

    • 允许FTP控制端口(21):
      sudo ufw allow 21/tcp 
    • 允许FTP数据传输端口(20):
      sudo ufw allow 20/tcp 
    • 允许FTP被动模式端口范围(例如30000-31000):
      sudo ufw allow 30000:31000/tcp 
  4. 重新加载防火墙规则

    sudo ufw reload 
  5. 检查防火墙状态

    sudo ufw status 

使用 iptables 设置防火墙

  1. 安装iptables(如果尚未安装)

    sudo apt update sudo apt install iptables 
  2. 配置防火墙规则

    sudo iptables -A INPUT -p tcp --dport 21 -j ACCEPT # 允许FTP控制端口 sudo iptables -A INPUT -p tcp --dport 20 -j ACCEPT # 允许FTP数据传输端口 sudo iptables -A INPUT -p tcp --dport 30000:31000 -j ACCEPT # 允许FTP被动模式端口范围 sudo iptables -A INPUT -i lo -j ACCEPT # 允许本地回环接口 sudo iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT # 允许已建立的连接 sudo iptables -A INPUT -j REJECT --reject-with icmp-host-prohibited # 拒绝其他所有输入 
  3. 保存iptables规则

    sudo iptables-save > /etc/iptables/rules.v4 
  4. 创建自启动脚本(确保规则在系统重启后依然有效)

    sudo nano /etc/network/if-pre-up.d/iptables 

    添加以下内容:

    #!/bin/sh /sbin/iptables-restore < /etc/iptables/rules.v4 

    赋予脚本执行权限:

    sudo chmod +x /etc/network/if-pre-up.d/iptables 
  5. 测试防火墙规则

    sudo iptables -L -n 

注意事项

  • 被动模式配置:确保你的FTP服务器配置文件(通常是 /etc/vsftpd.conf)中启用了被动模式,并且正确配置了被动模式的端口范围。
  • 安全性:FTP协议本身不安全,建议使用SFTP(SSH File Transfer Protocol)来代替FTP,或者使用FTPS(FTP over SSL/TLS)来加密数据传输。

通过以上步骤,你应该能够在Debian系统上成功配置FTP服务器的防火墙规则,确保FTP服务的正常运行同时保护服务器的安全。

0