温馨提示×

Ubuntu FTP Server怎样设置防火墙

小樊
53
2025-04-22 12:07:58
栏目: 智能运维

在Ubuntu上设置FTP服务器的防火墙规则,通常涉及使用 ufw (Uncomplicated Firewall)或 iptables 。以下是使用这两种方法的步骤:

使用ufw配置防火墙

  1. 安装ufw(如果尚未安装):
sudo apt update
sudo apt install ufw
  1. 启用ufw
sudo ufw enable
  1. 允许FTP流量
  • FTP协议通常使用两个端口:20(数据传输)和21(控制连接)。你可以允许这两个端口:
sudo ufw allow 20/tcp
sudo ufw allow 21/tcp
  • 允许被动模式FTP:被动模式FTP需要额外的端口范围。通常,被动模式FTP使用端口范围1024到1048。你可以根据需要调整这个范围:
sudo ufw allow 1024:1048/tcp
  1. 检查防火墙状态
sudo ufw status

使用iptables配置防火墙

  1. 安装iptables(如果尚未安装):
sudo apt-get install iptables
  1. 允许FTP流量
sudo iptables -A INPUT -p tcp --dport 20 -j ACCEPT
sudo iptables -A INPUT -p tcp --dport 21 -j ACCEPT
  1. 允许被动模式FTP
  • 假设被动模式FTP使用端口范围1024到1048:
sudo iptables -A INPUT -p tcp --dport 1024:1048 -j ACCEPT
  1. 保存iptables规则
sudo apt-get install iptables-persistent
sudo netfilter-persistent save
  1. 查看iptables规则
sudo iptables -L -n

请注意,使用 ufw 方法通常更简单且易于管理。如果你的FTP服务器使用的是vsftpd,你还需要确保vsftpd的配置文件 /etc/vsftpd.conf 中的设置允许FTP流量通过防火墙。

0