温馨提示×

CentOS下FTPServer怎么用

小樊
50
2026-08-04 03:26:49
栏目: 智能运维

CentOS 下搭建和使用 FTP Server,常用的是 vsftpd(Very Secure FTP Daemon)。下面给你一套 从安装到使用的完整流程(适用于 CentOS 7 / CentOS Stream,8/9 类似)。


一、安装 vsftpd

yum install -y vsftpd

二、启动并设置开机自启

systemctl start vsftpd
systemctl enable vsftpd

查看状态:

systemctl status vsftpd

三、防火墙配置(非常重要)

1️⃣ 开放 FTP 端口(默认 21)

firewall-cmd --permanent --add-service=ftp
firewall-cmd --reload

如果是云服务器,还需要在安全组中放行 TCP 21


四、关闭 SELinux(或放行 FTP)

临时关闭(测试用)

setenforce 0

永久关闭(不推荐生产)

sed -i 's/SELINUX=enforcing/SELINUX=disabled/' /etc/selinux/config

推荐方式(生产环境)

setsebool -P ftp_home_dir on
setsebool -P allow_ftpd_full_access on

五、配置 vsftpd(关键)

配置文件位置:

/etc/vsftpd/vsftpd.conf

常用基础配置

anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
chroot_local_user=YES
allow_writeable_chroot=YES
listen=YES

✅ 说明:

  • anonymous_enable=NO:禁止匿名登录
  • chroot_local_user=YES:用户限制在主目录
  • allow_writeable_chroot=YES:防止登录失败

修改后重启:

systemctl restart vsftpd

六、创建 FTP 用户

useradd -m ftpuser
passwd ftpuser

限制用户只能通过 FTP 登录(不能 SSH):

usermod -s /sbin/nologin ftpuser

七、客户端连接测试

Linux 本地测试

ftp localhost

Windows / FileZilla

  • 主机:服务器IP
  • 端口:21
  • 协议:FTP
  • 加密:只使用明文 FTP(或配置 FTPS)

八、常见问题排查

❌ 530 Login incorrect

cat /var/log/secure

常见原因:

  • 密码错误
  • nologin shell 未在 /etc/shells
echo /sbin/nologin >> /etc/shells

❌ 无法上传文件

  • 目录权限
  • SELinux
  • vsftpd 写权限未开

九、被动模式(PASV,推荐)

如果客户端在 NAT 后面,需要配置被动模式:

pasv_enable=YES
pasv_min_port=30000
pasv_max_port=31000

防火墙放行:

firewall-cmd --permanent --add-port=30000-31000/tcp
firewall-cmd --reload

十、FTPS(FTP over SSL,可选)

如果你需要 加密 FTP,我可以单独给你一份 FTPS 配置教程(vsftpd + SSL)。


总结一句话

CentOS 下用 vsftpd 是最稳妥的 FTP Server 方案,配置简单、稳定、安全。

如果你告诉我:

  • ✅ CentOS 版本
  • ✅ 是否要匿名访问
  • ✅ 是否要加密(FTPS)
  • ✅ 公网还是内网

我可以给你 一份完全适合你环境的配置文件

0