一、安装vsftpd
[root@Debug ~]# yum -y install vsftpd
二、编写配置文件vsftpd.conf
0. 编辑 /etc/vsftpd/vsftpd.conf
[root@Debug ~]# vim /etc/vsftpd/vsftpd.conf
1. 关闭匿名访问
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
anonymous_enable=NO
2. 允许本地用户登录
设置为允许/etc/passwd里面的本地用户登录ftp
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
local_enable=YES
3. 关闭全局默认写权限
为了进一步提升安全性,我们需要关闭vsftpd对于全局的默认写权限,而后按需为每个ftp用户在其独立配置文件中分配写权限:
write_enable=NO
4. 限制ftp用户空间
vsftpd中,chroot_local_user指示了是否限制各个用户在其用户空间(/home/user)中活动。当vsftpd被安装后,默认chroot_local_user为NO的情况下,用户登录ftp后是允许切换到上级目录的,这意味着它们将有能力访问整个磁盘上所有具有权限的文件,假如登录的用户是root,这将会是很严重的一个安全隐患。
chroot_list_enable指示是否开启例外列表,当chroot_local_user为YES时,例外列表里的用户名为允许切换目录,当chroot_local_user为NO时,例外列表中的用户将被限制在用户空间。
chroot_list_file指定例外用户名列表文件的路径,默认为/etc/vsftpd/chroot_list。
# 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
chroot_list_file=/etc/vsftpd/chroot_list
5. 指定用户配置目录
为了满足多ftp用户的需求,我们需要为每个ftp用户进行独立配置,为了实现这一点,我们可以通过指明user_config_dir来让vsftpd加载指定目录中的配置文件。
# This powerful option allows the override of any config option specified
# in the manual page, on a per-user basis. Usage is simple, and is best
# illustrated with an example. If you set user_config_dir to be /etc/vsftpd_user_conf
# and then log on as the user "chris", then vsftpd will apply the settings
# in the file /etc/vsftpd_user_conf/chris for the duration of the session.
# Default: (none)
user_config_dir=/etc/vsftpd/vsftpd_user_conf
三、测试
0. 新建用户ftptest
[root@Debug vsftpd]# useradd -s /sbin/nologin -g ftp ftptest
[root@Debug vsftpd]# passwd ftptest
1. 编辑ftptest配置文件
来到我们指定的用户配置目录(/etc/vsftpd/vsftpd_user_conf)下,我们创建名为ftptest的用户配置文件:
# vsftpd per-user basis config file (override of any config option specified
# in the vsftpd server config file)
#
# TEMPLATE
#
# User test - Description for user test
#
# Set local root
local_root=/home/ftptest
# Disable any form of FTP write command.
# Allowed values: YES/NO
write_enable=YES
allow_writeable_chroot=YES
allow_writeable_chroot 对于当前案例非常重要,假如一个被限制在其用户空间的用户,在登陆时其用户空间具有写权限,那么将不被vsftpd允许登录,解决方法要么是去掉目录写权限,要么是添加allow_writeable_chroot=YES。
2. 创建ftptest所属的ftp空间
[root@Debug vsftpd_user_conf]# mkdir -p /www/ftptest
[root@Debug vsftpd_user_conf]# chown -R ftptest /www/ftptest
[root@Debug vsftpd_user_conf]# chmod -R 755 /www/ftptest
用户组及目录权限设置应该根据ftp用户实际所扮演的角色进行配置,如ftp目录是网站根目录的话,可能需要将ftptest归属于www或apache用户组。
3. 将ftptest用户空间绑定到ftp空间
[root@Debug vsftpd_user_conf]# mount --bind /www/ftptest /home/ftptest
绑定后相当于创建了一个从/home/ftptest到/www/ftptest目录的链接,对于非测试账户需要让绑定关系在重启后依然生效,需要修改fstab文件,添加如下一行:
[root@Debug ~]# vim /etc/fstab
/www/ftptest /home/ftptest none defaults,bind 0 0
4. 启动vsftpd服务
[root@Debug vsftpd_user_conf]# service vsftpd start
Redirecting to /bin/systemctl start vsftpd.service
5. 在其它主机上采用FlashFXP进行测试
四、开机自动启动vsftpd服务
[root@Debug ~]# chkconfig vsftpd on
Note: Forwarding request to 'systemctl enable vsftpd.service'.
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.