温馨提示×

centos下vsftp如何配置防火墙规则

小樊
52
2025-07-08 12:16:55
栏目: 智能运维

在CentOS系统下配置VSFTPD(Very Secure FTP Daemon)的防火墙规则,通常需要使用firewalldiptables。以下是使用这两种工具进行配置的步骤:

使用 firewalld

  1. 启动并启用 firewalld 服务

    sudo systemctl start firewalld
    sudo systemctl enable firewalld
    
  2. 开放FTP端口: VSFTPD默认使用以下端口:

    • 20(数据连接)
    • 21(控制连接)

    使用firewalld开放这些端口:

    sudo firewall-cmd --permanent --add-port=20/tcp
    sudo firewall-cmd --permanent --add-port=21/tcp
    
  3. 重新加载防火墙配置

    sudo firewall-cmd --reload
    
  4. 验证端口是否开放

    sudo firewall-cmd --list-all
    

使用 iptables

  1. 启动并启用 iptables 服务

    sudo systemctl start iptables
    sudo systemctl enable iptables
    
  2. 开放FTP端口: 使用iptables开放端口20和21:

    sudo iptables -A INPUT -p tcp --dport 20 -j ACCEPT
    sudo iptables -A INPUT -p tcp --dport 21 -j ACCEPT
    
  3. 保存 iptables 规则: CentOS 7及以上版本使用firewalld,但如果你使用的是CentOS 6或其他版本,可以使用以下命令保存规则:

    sudo service iptables save
    
  4. 重启 iptables 服务

    sudo systemctl restart iptables
    
  5. 验证端口是否开放

    sudo iptables -L -n | grep 20
    sudo iptables -L -n | grep 21
    

注意事项

  • 被动模式:如果你的FTP客户端使用被动模式(PASV),还需要开放一个范围较大的端口(通常是1024到65535)。你可以使用以下命令开放这个范围:

    sudo firewall-cmd --permanent --add-port=1024-65535/tcp
    sudo firewall-cmd --reload
    

    或者使用iptables

    sudo iptables -A INPUT -p tcp --dport 1024:65535 -j ACCEPT
    sudo service iptables save
    sudo systemctl restart iptables
    
  • SELinux:如果你的系统启用了SELinux,可能还需要配置SELinux策略以允许FTP连接。可以使用以下命令查看SELinux状态:

    sestatus
    

    如果SELinux处于 enforcing 模式,可以尝试临时将其设置为 permissive 模式以测试:

    sudo setenforce 0
    

    如果一切正常,可以配置SELinux策略以永久允许FTP连接。

通过以上步骤,你应该能够在CentOS系统下成功配置VSFTPD的防火墙规则。

0