注:个人对偏向于底层基本上拿来就用的应用,倾向于使用安装包,直接yum或者rpm安装;而对于应用层面控制较多或者需要大范围维护的,倾向于直接使用tar.gz版本。
对于linux下的ftp服务器,实际上有很多的实现,只不过较广泛的在使用的是vsftpd,它是UNIX下一个GPL FTP服务器,全称very secure FTP daemon,官网https://security.appspot.com/vsftpd.html。
1、安装vsftpd,yum install vsftpd
安装完成后,可以执行which vsftpd检查是否安装成功,同时会在/etc/vsftpd下创建三个配置文件,如下:
[root@elk1 vsftpd]# ll
总用量 20-rw-------. 1 root root 125 8月 3 2017 ftpusers # 指定哪些用户不能访问FTP服务器,这里的用户包括root在内的一些重要用户。-rw-------. 1 root root 361 8月 3 2017 user_list # 指定的用户是否可以访问ftp服务器,通过vsftpd.conf文件中的userlist_deny的配置来决定配置中的用户是否可以访问,userlist_enable=YES ,userlist_deny=YES ,userlist_file=/etc/vsftpd/user_list 这三个配置允许文件中的用户访问FTP。-rw-------. 1 root root 5135 6月 8 11:32 vsftpd.conf # 主配置文件注:vsftpd虽然共享linux的用户,但是做了更加精细化二次安全防护,避免常见的脆弱性安全问题,所以如果希望让root用户访问,需要在ftpusers中注释掉root。
2、安装完成后,默认情况下,直接service vsftpd start就可以启动ftp服务,这就是最傻瓜的方式,全部是使用默认设置,但是在实际中,我们一般需要对配置进行修改满足我们的特定管理要求。比如:
- 指定根目录,而非默认的/pub。
- 指定是否启用匿名访问,允许哪些用户访问哪些目录。
- 设置用户是否可以创建目录,如果不存在的话。
一般来说,对于公有的ftp服务器,需要隔离不同用户的访问权限,一个公司/BU内部的话,一般一个用户,根据目录进行划分就足够,在管理和便利之间权衡。
这里,我们假设ftp目录为/usr/local/app/yidoo/ebs-k3c,同时在其下创建一个file目录,允许ftpadmin/123456用户读写file下的所有文件,但是不允许创建新的目录。匿名用户可以读file下的文件,但是不能写。
- 首先创建一个专用于ftp的用户ftpadmin。
[root@elk1 vsftpd]# useradd ftpadmin -s /sbin/nologin[root@elk1 vsftpd]# passwd ftpadmin更改用户 ftpadmin 的密码 。新的 密码:无效的密码: 密码少于 8 个字符重新输入新的 密码:passwd:所有的身份验证令牌已经成功更新。[root@elk1 vsftpd]#
此时,就可以使用ftpadmin访问ftp服务器了,如下:
2. vsftpd的根目录是通过两个参数控制,local_root控制非匿名用户登录时vsftpd进入的目录,anon_root则是匿名用户登录时vsftpd进入的目录,这两个参数都没有默认值,不过笔者测下来是/pub。所以,如果要更改ftp目录的话,设置这两个参数即可。
vim /etc/vsftpd/vsftpd.conf
local_root=/usr/local/app/yidoo/ebs-k3c
anon_root=/usr/local/app/yidoo/ebs-k3c
3. 设置匿名用户可读/不可写,登录用户可写、不可创建目录、不可访问指定的ftp目录外的其他目录。默认情况下,ftp用户登录之后,可以cd到其他目录,比如我们上面创建的ftpadmin可以切换到/etc目录。
- anonymous_enable=true #启用匿名用户登录,默认启用
- anon_world_readable_only=true #允许匿名用户下载,默认启用
- local_enable=true,默认false,这个默认值感觉不合适
- write_enable=true
- # 下面三个配置主要用于控制细粒度的访问权限
- chroot_list_enable=NO #设置是否启用chroot_list_file配置项指定的用户列表文件。默认值为NO。 YES为启用 NO禁用,默认禁用
- chroot_local_user=YES #是否将所有用户限制在主目录下,也就是切换到和主目录平级的其他目录或者干脆不相关目录,如上文说的切换到/etc目录,YES为启用 NO禁用.(该项默认值是NO,即在安装vsftpd后不做配置的话,ftp用户是可以向上切换到根目录之外的)
- chroot_list_file=/etc/vsftpd/chroot_list #设置是否限制在主目录下的用户名单,至于是限制名单还是排除名单,这取决于chroot_local_user的值,我们可以这样记忆: chroot_local_user总是一个全局性的设定,其为YES时,全部用户被锁定于主目录,其为NO时,全部用户不被锁定于主目录。那么我们势必需要在全局设定下能做出一些“微调”,即,我们总是需要一种“例外机制",所以当chroot_list_enable=YES时,表示我们“需要例外”。而”例外“的含义总是有一个上下文的,即,当”全部用户被锁定于主目录“时(即chroot_local_user=YES),"例外"就是:不被锁定的用户是哪些;当"全部用户不被锁定于主目录"时(即chroot_local_user=NO),"例外"“就是:要被锁定的用户是哪些。这样解释和记忆两者之间的关系就很清晰了!
4.上述配置修改后,重启vsftpd,service vsftpd restart
再次明确访问上述目录的时候,就会提示无法显示远程文件夹,即ftp客户端是不会知道当前根目录对应的实际物理目录的。
5.如果启用了selinux,则需要禁用,并执行如下,否则在文件上传的时候会提示vsftp 553 Could not create file。
[root@elk1 vsftpd]# vim /etc/selinux/config [root@elk1 vsftpd]# cat /etc/selinux/config # This file controls the state of SELinux on the system.# SELINUX= can take one of these three values:# enforcing - SELinux security policy is enforced.# permissive - SELinux prints warnings instead of enforcing.# disabled - No SELinux policy is loaded.SELINUX=disabled # 这儿# SELINUXTYPE= can take one of three two values:# targeted - Targeted processes are protected,# minimum - Modification of targeted policy. Only selected processes are protected. # mls - Multi Level Security protection.SELINUXTYPE=targeted [root@elk1 vsftpd]# getsebool -a | grep ftpftpd_anon_write --> offftpd_connect_all_unreserved --> offftpd_connect_db --> offftpd_full_access --> offftpd_use_cifs --> offftpd_use_fusefs --> offftpd_use_nfs --> offftpd_use_passive_mode --> offhttpd_can_connect_ftp --> offhttpd_enable_ftp_server --> offtftp_anon_write --> offtftp_home_dir --> off[root@elk1 vsftpd]# setsebool allow_ftpd_full_access on # 这儿[root@elk1 vsftpd]# getsebool -a | grep ftpftpd_anon_write --> offftpd_connect_all_unreserved --> offftpd_connect_db --> offftpd_full_access --> onftpd_use_cifs --> offftpd_use_fusefs --> offftpd_use_nfs --> offftpd_use_passive_mode --> offhttpd_can_connect_ftp --> offhttpd_enable_ftp_server --> offtftp_anon_write --> offtftp_home_dir --> off
参考:
vsftpd.conf各配置项官方文档参考https://security.appspot.com/vsftpd/vsftpd_conf.html(注,可能需要FQ才能访问)
配置修改后,启动时遇到Job for vsftpd.service failed because the control process exited with error code. See "systemctl status vsftpd.service" and "journalctl -xe" for details. http://bbs.51cto.com/thread-1480655-1-1.html 19楼终极解决方法,重装,于是把配置全部改回默认值,一个个修改看问题出在哪里。
VSFTPD一直提示“用户身份验证失败”:搭建好之后,当日都正常,自从第二天服务器被重启之后,无论是xftp还是java客户端登录就一直提示“用户身份验证失败”,无论是匿名还是使用root账户或者ftpadmin账户均如此,搜了下网上不少帖子,比如https://yq.aliyun.com/wenzhang/show_11618(但是它仅仅指的是java客户端访问),https://bbs.aliyun.com/read/252932.html?pos=6(它讲的是通过ssh登录,并非ftp),均未看到解决。使用命令行下ftp客户端登录,如下:
用户(192.168.230.128:(none)): ftpadmin
331 Please specify the password.密码:500 OOPS: vsftpd: refusing to run with writable root inside chroot()远程主机关闭连接。完整配置如下:
[root@elk1 vsftpd]# cat vsftpd.conf # Example config file /etc/vsftpd/vsftpd.conf## The default compiled in settings are fairly paranoid. This sample file# loosens things up a bit, to make the ftp daemon more usable.# Please see vsftpd.conf.5 for all compiled in defaults.## READ THIS: This example file is NOT an exhaustive list of vsftpd options.# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's# capabilities.## Allow anonymous FTP? (Beware - allowed by default if you comment this out).anonymous_enable=YES## Uncomment this to allow local users to log in.# When SELinux is enforcing check for SE bool ftp_home_dirlocal_enable=YES## Uncomment this to enable any form of FTP write command.write_enable=YES## Default umask for local users is 077. You may wish to change this to 022,# if your users expect that (022 is used by most other ftpd's)local_umask=022## Uncomment this to allow the anonymous FTP user to upload files. This only# has an effect if the above global write enable is activated. Also, you will# obviously need to create a directory writable by the FTP user.# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_accessanon_upload_enable=YES## Uncomment this if you want the anonymous FTP user to be able to create# new directories.anon_mkdir_write_enable=YES## Activate directory messages - messages given to remote users when they# go into a certain directory.dirmessage_enable=YES## Activate logging of uploads/downloads.xferlog_enable=YES## Make sure PORT transfer connections originate from port 20 (ftp-data).connect_from_port_20=YES## If you want, you can arrange for uploaded anonymous files to be owned by# a different user. Note! Using "root" for uploaded files is not# recommended!#chown_uploads=YES#chown_username=whoever## You may override where the log file goes if you like. The default is shown# below.#xferlog_file=/var/log/xferlog## If you want, you can have your log file in standard ftpd xferlog format.# Note that the default log file location is /var/log/xferlog in this case.xferlog_std_format=YES## You may change the default value for timing out an idle session.#idle_session_timeout=600## You may change the default value for timing out a data connection.#data_connection_timeout=120## It is recommended that you define on your system a unique user which the# ftp server can use as a totally isolated and unprivileged user.#nopriv_user=ftpsecure## Enable this and the server will recognise asynchronous ABOR requests. Not# recommended for security (the code is non-trivial). Not enabling it,# however, may confuse older FTP clients.#async_abor_enable=YES## By default the server will pretend to allow ASCII mode but in fact ignore# the request. Turn on the below options to have the server actually do ASCII# mangling on files when in ASCII mode.# Beware that on some FTP servers, ASCII support allows a denial of service# attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd# predicted this attack and has always been safe, reporting the size of the# raw file.# ASCII mangling is a horrible feature of the protocol.#ascii_upload_enable=YES#ascii_download_enable=YES## You may fully customise the login banner string:#ftpd_banner=Welcome to blah FTP service.## You may specify a file of disallowed anonymous e-mail addresses. Apparently# useful for combatting certain DoS attacks.#deny_email_enable=YES# (default follows)#banned_email_file=/etc/vsftpd/banned_emails## 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## You may activate the "-R" option to the builtin ls. This is disabled by# default to avoid remote users being able to cause excessive I/O on large# sites. However, some broken FTP clients such as "ncftp" and "mirror" assume# the presence of the "-R" option, so there is a strong case for enabling it.#ls_recurse_enable=YES## When "listen" directive is enabled, vsftpd runs in standalone mode and# listens on IPv4 sockets. This directive cannot be used in conjunction# with the listen_ipv6 directive.listen=NO## This directive enables listening on IPv6 sockets. By default, listening# on the IPv6 "any" address (::) will accept connections from both IPv6# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6# sockets. If you want that (perhaps because you want to listen on specific# addresses) then you must run two copies of vsftpd with two configuration# files.# Make sure, that one of the listen options is commented !!listen_ipv6=YESpam_service_name=vsftpduserlist_enable=YEStcp_wrappers=YESlocal_root=/usr/local/app/yidoo/ebs-k3cchroot_local_user=YES# chroot_list_enable=falseanon_root=/usr/local/app/yidoo/ebs-k3c
当我注释掉chroot_local_user的时候,也就是不限制用户切换到主目录之外就不出错了。找到具体的错误信息解决问题就很简单了,经查:
这个问题发生在最新的这是由于下面的更新造成的:
- Add stronger checks for the configuration error of running with a writeable root directory inside a chroot(). This may bite people who carelessly turned on chroot_local_user but such is life.
从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!如果检查发现还有写权限,就会报该错误。
要修复这个错误,可以用命令chmod a-w /home/user去除用户主目录的写权限,注意把目录替换成你自己的。或者你可以在vsftpd的配置文件中增加下列两项中的一项:
allow_writeable_chroot=YES
在配置文件中加上上面的配置项之后,无论是xftp还是原生ftp客户端,均可以正常访问了。