目录
本篇文章案例搭建的是本地用户模式、被动模式的FTP服务。闲篇少叙,直接开始。
一、安装vsftpd
- yum安装vsftpd(ftp服务端)
yum -y install vsftpd
- 查看vsftpd服务状态
systemctl status vsftpd
- 启动vsftpd服务
systemctl start vsftpd
- 重启vsftpd服务
systemctl restart vsftpd
- 设置下次开机自启动
systemctl enable vsftpd.service
二、检查环境
1、防火墙
vsftpd安装成功后,如果直接登录ftp很可能会失败(即使是匿名登录ftp);其中一个原因大概率是因为防火墙的配置问题。所以在vsftpd安装完成后,需要防火墙放行ftp端口。
- 防火墙添加ftp的service
firewall-cmd --add-service=ftp
- 查看防火墙所有规则
firewall-cmd --list-all
2、selinux
为了以防万一,selinux最好将其关闭!关闭方法就是修改selinux的配置文件,配置文件路径是/etc/selinux/config。将其中的SELINUX参数改成disabled即可。
vim /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 values:
# targeted - Targeted processes are protected,
# minimum - Modification of targeted policy. Only selected processes are protected.
# mls - Multi Level Security protection.
SELINUXTYPE=targeted
三、添加用户
添加用户是为了适配ftp的登录操作。root用户默认是在ftp的黑名单中的,不可以使用。但如果您这边已经有预备的用户了,就不再需要操作了。这里的用户指的其实就是操作系统的用户,使用本地登录模式登录FTP时,可以使用操作系统的用户。
- 添加用户
useradd qdz
- 设置密码
passwd qdz
Changing password for user qdz.
New password:
BAD PASSWORD: The password is a palindrome
Retype new password:
passwd: all authentication tokens updated successfully.
四、配置文件
vsftpd的配置文件默认位置是 /etc/vsftpd/vsftpd.conf。配置文件中的参数非常多,这里梳理了常用的或者常见的参数,没有将全部参数列出来。实际上在安装完vsftpd后,其实并不需要调整过多的参数。注意一点:改完配置文件参数后,记得重启vsftpd服务。
![](https://img-blog.csdnimg.cn/direct/1502d2a8d038446faf9f1389bba7fd58.png)
1、用户模式
ftp有三种用户模式:匿名用户、本地用户、虚拟用户。
#匿名用户
anonymous_enable #是否开启匿名用户模式,YES/NO
anon_upload_enable #是否允许匿名用户下载文件,YES/NO
anon_mkdir_write_enable #是否允许匿名用户上传文件、创建目录、删除文件,YES/NO
#本地用户
local_enable #是否开启本地用户模式,YES/NO
write_enable #是否允许用户上传文件、创建目录、删除文件,YES/NO
2、 日志模式
可以记录上传和下载的日志,以下是参数说明,可以根据个人需求开启或者关闭,同时考虑服务器的磁盘空间。我一般是关闭的。
xferlog_enable #是否开启日志记录功能,YES/NO
log_ftp_protocol #是否开启更加细化的日志记录功能,YES/NO
xferlog_std_format #是否将日志文件设置为标准ftpd xferlog格式
xferlog_file #默认日志文件位置,可以自定义修改,默认是/var/log/xferlog
3、用户限制
这里是限制哪些用户可以登录,可以起到一个黑白名单的作用。需要注意的一点是只有当userlist_enable=YES时,userlist_deny的配置才有效;即userlist_enable参数是userlist_deny参数的先决条件。而且当user_list变成一个白名单后,匿名用户是无法登录的,除非在user_list中加一行anonymous。
此外/etc/vsftpd目录下的ftpusers文件是一个黑名单文件,凡是在名单上的用户都不能登录FTP,即使该用户在白名单状态下的user_list文件中。
userlist_file #指定用户列表,默认路径是 /etc/vsftpd/user_list
userlist_enable #是否使用user_list,YEW/NO
userlist_deny #是否禁用user_list中的用户,YES/NO,默认是YES
4、被动模式
FTP本质上是一种文件传输协议,它可以通过两种模式来进行数据传输:主动模式,被动模式。安装完vsftpd后,FTP默认是被动模式,篇文章搭建的FTP也是被动模式。一般来说,网络稳定情况下被动模式更稳妥,但没有绝对,如果哪天碰到了问题,朋友们可以尝试切换模式来尝试解决问题。
pasv_enable #是否开启被动模式,YES/NO,默认是YES
除了配置文件,也可以通过端口查看FTP是被动模式还是主动模式。被动模式的默认端口是21,主动模式的默认端口是20。
netstat -tunlp | grep ftp
5、默认位置
如果不做任何修改,那么用户打开FTP进入的目录是 /var/ftp/pub。为了管理方便或者因为挂载点空间问题,我们是可以通过修改参数来改变此默认位置的。
local_root #FTP用户登录的默认位置,例如可修改为local_root=/datacenter
6、运行方式
vsftpd可以以两种方式运行:独立守护进程和inetd方式。
独立守护进程方式是指vsftpd作为一个独立的进程运行,它会一直在后台运行,等待客户端的连接请求。这种方式的优点是可以提高服务的响应速度和并发处理能力,因为它不需要每次都重新启动一个新的进程来处理客户端的请求。
inetd方式是指vsftpd作为inetd的一个服务运行,每当有客户端连接请求时,inetd会启动一个新的vsftpd进程来处理该请求。这种方式的优点是可以节省系统资源,因为它只在需要时才会启动一个新的进程来处理客户端请求。
配置文件中的listen参数可以调整vsftpd服务是按照独立进程还是inetd进程。参数如下:
listen #listen=NO表示vsftpd服务以独立守护进程的方式运行,listen=YSE表示vsftpd服务以inetd方式运行
7、切换目录
该类参数是控制FTP用户登录到FTP后,是否可以切换到其他目录(一般针对Linux系统ftp客户端用户登录)。在默认配置下,本地用户登入FTP后可以使用cd命令切换到其他目录。从安全角度来说,还是禁止用户切换目录,只允许其访问默认目录。
chroot_list_enable #是否使用chroot_list文件,YES/NO,该文件中包含可以允许切换的目录,如果没有,需要创建
chroot_list_file #指定chroot_list文件,填写绝对路径
chroot_local_user #用于指定用户列表文件中的用户是否允许切换到上级目录。YES/NO,默认值为NO。
下面将我搭建的FTP配置文件放在下面供朋友们参考,朋友们也可以根据上文的参数描述进行自定义调整。但无论如何调整,朋友们一定记得备份原始配置文件。
# 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
#
# 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.
# 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=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 !!
listen_ipv6=NO
pam_service_name=vsftpd
userlist_enable=YES
#userlist_deny=NO
#userlist_file=/etc/vsftpd/user_list
tcp_wrappers=YES
local_root=/datacenter
五、附加补充
如果您需要在Linux上登录FTP的话,需要再安装ftp客户端软件。
yum install ftp
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
local | 2.9 kB 00:00:00
Resolving Dependencies
--> Running transaction check
---> Package ftp.x86_64 0:0.17-67.el7 will be installed
--> Finished Dependency Resolution
Dependencies Resolved
====================================================================================================================================
Package Arch Version Repository Size
====================================================================================================================================
Installing:
ftp x86_64 0.17-67.el7 local 61 k
Transaction Summary
====================================================================================================================================
Install 1 Package
Total download size: 61 k
Installed size: 96 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : ftp-0.17-67.el7.x86_64 1/1
Verifying : ftp-0.17-67.el7.x86_64 1/1
Installed:
ftp.x86_64 0:0.17-67.el7
Complete!
Linux登录FTP示例
ftp 1xx.xx.xx.93
Connected to 1xx.xx.xx.93 (1xx.xx.xx.93).
220 (vsFTPd 3.0.2)
Name (172.19.18.93:root): bas
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/datacenter"
ftp>
以上是简易FTP服务的搭建过程。如果朋友们在搭建过程中有遇到其他问题,可以留言或者私信。感谢您的阅读。