使用vsftpd 搭建ftp 服务器

1. 简介:

使用vsftpd 在ubuntu环境中搭建ftp服务器。。Vsftp 是一个专门为unix类型系统设计一个ftp服务器,如linux。
Vsftpd 支持ipv6和ssl。支持explicit 和implicit FTPS(不是SFTP)。
vsftp是Ubuntu, CentOS, Fedora, NimbleX, Slackware,RHEL Linux 系统的默认ftp服务器.

2. 配置环境

ubuntu16.04.1 x86_64 GNU/Linux
测试工具:FileZilla 3.36.0
**

3. 配置方法:

**
3.1. 安装vsftpd
sudo apt-get install vsftpd
vsftp安装好后会自动生成ftp user和ftp group

3.2. 备份配置文件
vsftpd的配置文件位于/etc/vsftpd.conf
修改之前按需求备份一个原文件。
sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.original

3.3. 部分配置文件内容介绍

3.3.1. 匿名用户登陆

是否允许匿名用户登陆ftp,默认YES,只允许Anonymous用户下载

# Allow anonymous FTP? (Disabled by default).                                                                                                
anonymous_enable=NO

3.3.2. 本地用户登陆

为了允许在/etc/passwd内部用户登陆ftp,需将此项设置为YES

# Uncomment this to allow local users to log in.
local_enable=YES

3.3.3. 上传设置

若需对系统进行读写操作,需将此项设置为YES
# Uncomment this to enable any form of FTP write command.

write_enable=YES

3.3.4. Chroot jail

#You may specify an explicit list of local users to chroot() to their home                                                                  
# directory. If chroot_local_user is YES, then this list becomes a list of
# users to NOT chroot().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=YES
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list

这是个很有趣的变量,这个就是为了配置是否运行ftp用户离开其默认目录。设置之前,需手动添加vsftpd.chroot_list文件,此文件内存储ftp用户,其代表的含义根据chroot_local_user和chroot_list_enable不同而不同。
《使用vsftpd 搭建ftp 服务器》

此处将两项均设置为YES,并将希望有所有目录访问权限的ftp用户存于vsftpd.chroot_list文件中。

3.3.5. 阻止用户登陆

可以通过设置阻止部分用户访问ftp服务器。配置如下:

userlist_enable=YES
userlist_file=/etc/vsftpd.user_list
userlist_deny=NO

手动添加此三项到配置文件,同时创建vsftpd.user_list。与chroot jail配置参数相似,不同的设置导致不同的效果

《使用vsftpd 搭建ftp 服务器》

3.3.6. pam_service_name

pam_service_name参数用户配置虚拟ftp用户,需将此项设置为ftp,否则可能会出现530 Login incorrect登陆失败错误。

# This string is the name of the PAM service vsftpd will use.
#pam_service_name=vsftpd
pam_service_name=ftp

3.3.7. 连接数量限制

将下列配置信息添加到vsftpd文件内

local_max_rate=1000000 # Maximum data transfer rate in bytes per second
max_clients=50         # Maximum number of clients that may be connected
max_per_ip=2           # Maximum connections per IP

3.3.8. 其他设置

显示隐藏文件

# Show hidden files and the "." and ".." folders.
# Useful to not write over hidden files:
force_dot_files=YES

隐藏文件所有者信息

# Hide the info about the owner (user and group) of the files.
hide_ids=YES

3.4. 重启vsftpd服务

通过重启vsftpd应用配置修改,重启服务有以下三种方式

1) sudo service vsftpd restart
2) sudo systemctl restart vsftpd.service
3) sudo /etc/init.d/vsftpd restart

3.5. FTP用户创建

useradd myusername

会在/home/路径下创建一个与用户名相同的文件夹,作为用户默认路径。可使用useradd –d [directory] myusername选项在创建时指定默认路径,也可使用usermod –d [directory] myusername 重新指定用户默认路径。

在创建用户时,可以使用命令sudo usermod –s /usr/sbin/nologin myusername 来禁止ssh登陆ftp用户

创建完成后,即可使用ftp工具进行连接。

4. 部分ERROR解决方法

4.1. 500 OOPS: vsftpd: refusing to run with writable root inside chroot ()

若将新建ftp用户路径指定到root路径下,如/var/www/ftp,在连接时可能会报500 OOPS: vsftpd: refusing to run with writable root inside chroot () 错误。解决方法有两种:
1、 使用 sudo chmod –R a-w [directory] 去除可写权限,采用此方法会失去上传功能
2、 在vsftpd.conf配置文件中添加allow_writeable_chroot=YES

4.2. 550 Access is denied

在进行上传操作时可能会遇到550访问被拒绝错误,此时需要查看上传路径是否给ftp用户提供了写入权限。

可能涉及到的命令:
1) 将myusername 用户加入另一个用户组

usermod –a –G theothergroupname myusername

2) 将所有文件夹权限设置为755(drwxr-xr-x)

find /opt/lampp/htdocs -type d -exec chmod 755 {} \;

3) 将所有文件权限设置为644(-rw-r–r–)

find /opt/lampp/htdocs -type f -exec chmod 644 {} \;

**

5. 参考资料

**
[1]. Howto: Easy FTP with vsftpd, https://ubuntuforums.org/show…
[2]. Very Secure FTP Daemon , https://wiki.archlinux.org/in…
[3]. vsftpd 配置:chroot_local_user与chroot_list_enable详解, https://blog.csdn.net/bluishg…
[4]. How do I set chmod for a folder and all of its subfolders and files?, https://stackoverflow.com/que…

    原文作者:潘小闲
    原文地址: https://segmentfault.com/a/1190000016376962
    本文转自网络文章,转载此文章仅为分享知识,如有侵权,请联系博主进行删除。
点赞