Loading

centos ftp服务器搭建 vsftpd 安装与配置方法

vsftpd是linux下常用的ftp服务软件,配置起来不复杂。
ftp本身密码是明文传输的,如果需要安全,建议直接使用sftp,通过winscp等工具访问服务器。sftp只要能访问ssh就能使用,无需额外配置。

匿名访问

vsftp配置失败往往是由于权限问题,导致出现550 Failed to open file错误。这里我简单介绍一下注意的点。
vsftpd在Linux中默认使用的用户是ftp,访问的根目录为/var/ftp,该目录默认所有者是root。
配置的最佳实践是将var/ftp/pub目录所有者改为ftp,文件操作仅在pub目录中进行。
我在配置完成后,下载文件时遇到了ftp 550 Failed to open file 问题,仔细查看文件权限发现,新增文件权限为600,此时会出现这个错误。在配置文件中添加anon_umask=022 配置,就可解决。配置前注意禁用selinux。
详细配置方法如下,配置文件的详细说明可通过man vsftpd.conf查看

yum install vsftpd -y

chown ftp:ftp -R /var/ftp/pub

/etc/vsftpd/vsftpd.conf 

anonmous_enalbe=YES
#local_enable=YES
write_enable=YES
#local_umask=022
anon_umask=022  #设置新增文件权限umask
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES #允许修改或删除文件


systemctl start vsftpd
systemctl enable vsftpd

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

密码访问

如需改为密码访问,首先需新增用户ftpuser,并修改/var/ftp所有者为ftpuser。注释上文配置中anon_*的相关配置,并修改配置文件:

anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
chroot_local_user=YES #chroot all the users
chroot_list_enable=NO #chroot all the users
userlist_enable=YES #userlist配置文件中指定的用户禁止访问
userlist_deny=YES #userlist配置文件中指定的用户禁止访问
user_sub_token=$USER 
local_root=/var/ftp/pub
allow_writeable_chroot=YES #允许根路径写入,如果为NO则禁止写入。必须设置根目录文件夹的可写权限与此配置一致,否则无法正常访问

配置ftpuser账户去除shell访问权限

echo -e '#!/bin/sh\necho "This account is limited to FTP access only."' | sudo tee -a  /bin/ftponly
chmod +x /bin/ftponly
echo "/bin/ftponly" | sudo tee -a /etc/shells
usermod ftpuser -s /bin/ftponly  

有时vsftpd软件包升级,会重置/var/ftp/pub文件夹权限,所以将文件存储迁移到其他目录比较好

mkdir -p /mnt/ftp/pub
chown ftpuser:ftpuser /mnt/ftp/pub
mv /var/ftp/pub /mnt/ftp/pub

参考:
https://linuxize.com/post/how-to-setup-ftp-server-with-vsftpd-on-centos-7/
https://www.howtoforge.com/tutorial/how-to-install-and-configure-vsftpd/

posted @ 2019-05-16 16:39  wswind  阅读(4689)  评论(0编辑  收藏  举报