ftp有两种工作模式,主动模式(port)和被动模式(passive)
主动模式:主动模式相对有利于服务端,在客户端访问服务端的21端口(默认端口不变的情况下)确认要进行ftp数据传输后,是客户端开放1024以上的随机端口给服务端去访问进行数据传输,对于客户端的网络管理是较不便的。
被动模式:被动模式相对有利于客户端,在客户端发送pasv信号通过21端口传送给服务端口,服务端会响应并开放vsftp.conf文件中配置的pasv_min_port和pasv_max_port范围内的随机端口,对于服务端防火墙等配置有要求。
一、准备工作
1、检查服务器有没有安装过vsftp,然后检查本地yum库里有没有安装包。
[root@docker ~]# rpm -qa | grep vsftpd [root@docker ~]# which vsftpd /usr/bin/which: no vsftpd in (/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/root/bin) 上面说明服务器还没有安装ftp server,可以检查服务器有没有这个安装包了 [root@docker ~]# yum list | grep vsftpd vsftpd.x86_64 3.0.3-28.el8 AppStream
2、关闭selinux
二、开始安装
1、执行yum安装语句
yum -y isntall vsftpd
2、启动相关程序
[root@docker ~]# systemctl start vsftpd --启动ftp [root@docker ~]# systemctl enable vsftpd --将ftp加入到开机自启动 Created symlink /etc/systemd/system/multi-user.target.wants/vsftpd.service → /usr/lib/systemd/system/vsftpd.service. [root@docker ~]# netstat -antup | grep ftp --检查ftp服务是否在监听 tcp6 0 0 :::21 :::* LISTEN 32333/vsftpd
三、vsftp的相关配置解析
[root@docker ~]# cd /etc/vsftpd/
[root@docker vsftpd]# ls
ftpusers user_list vsftpd.conf vsftpd_conf_migrate.sh
1、ftpusers 即ftp黑名单用户,只要在这个文件内的用户,均无法使用ftp服务。
2、user_list 是和vsftpd.conf中的userlist_enable和userlist_deny两个配置项紧密相关的,它可以有效,也可以无效,有效时它可以是一个黑名单,也可以是一个白名单!
(1)userlist_enable和userlist_deny两个选项联合起来针对的是:本地全体用户(除去ftpusers中的用户)和出现在user_list文件中的用户以及不在在user_list文件中的用户这三类用户集合进行的设置。 (2)当且仅当userlist_enable=YES时:userlist_deny项的配置才有效,user_list文件才会被使用;当其为NO时,无论userlist_deny项为何值都是无效的,本地全体用户(除去ftpusers中的用户)都可以登入FTP (3)当userlist_enable=YES时,userlist_deny=YES时:user_list是一个黑名单,即:所有出现在名单中的用户都会被拒绝登入; (4)当userlist_enable=YES时,userlist_deny=NO时:user_list是一个白名单,即:只有出现在名单中的用户才会被准许登入(user_list之外的用户都被拒绝登入);另外需要特别提醒的是:使用白名单后,匿名用户将无法登入!除非显式在user_list中加入一行:anonymous
3、配置vsftp.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=NO ---是否允许匿名登录 # # Uncomment this to allow local users to log in. # When SELinux is enforcing check for SE bool ftp_home_dir local_enable=YES ----是否允许本地用户登录FTP服务器 # # 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 --目录和文件被创建时得到的初始权限,022即目录权限755,文件权限644 # # 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 --当设定为 YES 时,使用者上传与下载文件都会被纪录起来。 # # Make sure PORT transfer connections originate from port 20 (ftp-data). connect_from_port_20=YES --默认的数据传输端口为20 # # 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=YES ---指定用户列表文件中的用户允许切换到上级目录 chroot_list_enable=YES --是否启用chroot_list_file配置指定的用户列表 # (default follows) chroot_list_file=/etc/vsftpd/chroot_list ---默认该文件不存在,需手动创建,主要是在 前两项均为YES时,配置可以cd到上级目录的用户。 # 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=YES pam_service_name=vsftpd userlist_enable=YES
allow_writeable_chroot=YES --对vsftpd有用,否则因home目录权限而无法登录
pasv_enable=YES
pasv_min_port=50000
pasv_max_port=60000
listen_port=2121 --修改默认端口号
注意
编辑/etc/services文件
修改ftp配置,将21改成2121
重启vsftpd
[root@docker vsftpd]# systemctl restart vsftpd
测试端口是否已变更。
[root@docker vsftpd]# netstat -antup | grep vsftpd tcp6 0 0 :::2121 :::* LISTEN 33275/vsftpd
四、添加ftp用户
一般添加ftp服务,都是要存文件之类的,可以指定一个挂在了较大磁盘空间的目录做为ftp用户的家目录。
[root@docker ~]# useradd -d /data -s /sbin/nologin -g ftptest ahftp useradd: warning: the home directory already exists. Not copying any file from skel directory into it.
[root@docker ~]# cat /etc/passwd
ahftp:x:1001:1001::/data:/sbin/nologin
设置ftp用户nologin后,需要在shell内添加该项。
输入 cat /etc/pam.d/vsftpd | fgrep pam_shell 后显示为:
auth required pam_shells.so
看到以上结果后,再次输入 :echo "/sbin/nologin" >> /etc/shells
五、防火墙策略
[root@docker data]# firewall-cmd --permanent --add-port=2121/tcp success [root@docker data]# firewall-cmd --permanent --add-port=50000-60000/tcp success [root@docker data]# firewall-cmd --reload success [root@docker data]# firewall-cmd --list-all public (active) target: default icmp-block-inversion: no interfaces: ens33 sources: services: cockpit dhcpv6-client ssh ports: 10086/tcp 2121/tcp 50000-60000/tcp protocols: masquerade: no forward-ports: source-ports: icmp-blocks: rich rules:
六、测试
建议先本地测试。