如何在 Linux CentOS 7.5 上搭建 FTP 服务器
工具/原料
CentOS 7.5
vsftpd
安装 vsftp 软件包
1、rpm -qa | grep vsftpdyum install -y vsftpd配置好 yum 源,使用 yum 安装 vsftpd 软件包。
2、rpm -qa | grep vsftpd检查 vsftpd 是否安装成功,可以看到 vsftpd 已安装。
3、systemctl status vsftpdsystemctl start vsftpd检查 vsftpd 服务状态,启动 vsftpd 服务。
4、systemctl status vsftpdsystemctl enable vsftpd设置 vsftpd 服务为开机自启动。
5、[root@localhost ~]# netstat -antup | grep ftptcp6 0 0 :::21 :::* LISTEN 9466/vsftpd可以看出 ftp 服务已启动,监听端口为21。
6、vsftp 默认开启匿名访问功能,输入用户名anonymous,无需输入密码或者随便数个密码,即可访问服务器,但是没有修改或上传文件的权限。
配置 vsftpd
1、建议禁用 selinux,否则会遇到很多问题。配置 vsftpd,使用操作系统本地用户认证登录。
2、#创建操作系统本地用户 ftpuser1,并将其密码设置为 123.com。useradd ftpuser1echo &qu泠贾高框ot;123.com" | passwd --stdin ftpuser1#创建操作系统本地用户 ftpuser2,并将其密码设置为 123.com。useradd ftpuser2echo "123.com" | passwd --stdin ftpuser2# 创建本地用户组 ftpgroupgroupadd ftpgroup#将 ftpuser1 和 ftpuser2 的用户主组修改为 ftpgroupusermod -g ftpgroup ftpuser1usermod -g ftpgroup ftpuser2# 查看用户信息[root@localhost ~]# id ftpuser1uid=1001(ftpuser1) gid=1003(ftpgroup) groups=1003(ftpgroup)[root@localhost ~]# id ftpuser2uid=1002(ftpuser2) gid=1003(ftpgroup) groups=1003(ftpgroup)
3、# 创建 ftp 根目录mkdir /ftproot# 修改 ftp 根楫默礤鲼目录用户属组chown -R ftpuser1:ftp爿讥旌护group /ftproot# 修改 ftp 根目录权限,这样同组的用户可以对目录下的文件进行读写操作。chmod -R 775 /ftproot# 查看目录权限及用户属组信息[root@localhost ~]# ls -ld /ftprootdrwxrwxr-x. 2 ftpuser1 ftpgroup 61 Jul 15 13:51 /ftproot
4、# 备份配置文件cp /etc/vsftpd/vsftpd.conf/etc/vsftpd/vsftpd.conf.bak# 编辑配置文件即枢潋雳vi/etc/vsftpd/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=YESanonymous_enable=NO## Uncomment this to allow local users to log in.# When SELinux is enforcing check for SE bool ftp_home_dir# 允许操作系统本地用户登录local_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_access#anon_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. The vsftpd.conf(5) man page explains# the behaviour when these options are disabled.# 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=YESchroot_local_user=YES# 启用例外用户清单# chroot_list_enable=YESchroot_list_enable=YES# (default follows)# 指定例外用户列表文件,列表中的用户不被锁定在主目录中。# chroot_list_file=/etc/vsftpd/chroot_listchroot_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.# 开启 IPv4 监听# listen=NOlisten=YES## 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 !!# 关闭 IPv6 监听# listen_ipv6=YESlisten_ipv6=NOpam_service_name=vsftpduserlist_enable=YEStcp_wrappers=YES# 添加下列参数# 设置本地用户登录后所在目录local_root=/ftproot# 开启被动模式pasv_enable=YESallow_writeable_chroot=YES-------------------------------------------------------------------------------
5、查看有效配置文件,过滤掉注释的内容和空行。[root@localhost ~]# grep -v ^# /etc/vsftpd/vsftpd.c泠贾高框onf | grep -v ^$anonymous_enable=NOlocal_enable=YESwrite_enable=YESlocal_umask=022dirmessage_enable=YESxferlog_enable=YESconnect_from_port_20=YESxferlog_std_format=YESchroot_local_user=YESchroot_list_enable=YESchroot_list_file=/etc/vsftpd/chroot_listlisten=YESlisten_ipv6=NOpam_service_name=vsftpduserlist_enable=YEStcp_wrappers=YESlocal_root=/ftprootpasv_enable=YESallow_writeable_chroot=YES
6、# 创建 chroot_list 文件,并将文件权限修改为 600。touch /etc/vsftpd/chroot_listchmod 600 /etc/vsftpd/chroot_list# 这里我们将 ftpuser2 写入例外名单vi /etc/vsftpd/chroot_listftpuser2
7、# 重启 vsftpd 服务,以便配置文件生效。systemctl restart vsftpdsystemctl status vsftpd
客户端测试
1、# 客户端使用 ftpuser1 用户登录 ftp 服务器。# 登录成功,可以使用 get 命令下载文件,使用 put 命令上传文件。# 但是不能切换到主目录之外的其他目录,目录逃逸失败。
2、# 客户端使用 ftpuser2 用户登录 ftp 服务器。# 登录成功,可以使用 get 命令下载文件,使用 put 命令上传文件。# 可以切换到主目录之外的其他目录,目录逃逸成功。