FTP服务器搭建
本文将介绍ftp安装以及相关配置,并对虚拟用户访问ftp服务器配置进行了简要介绍
FTP概述
FTP
FTP代表文件传输协议(File Transfer Protocol),它是用于在计算机网络上进行文件传输的标准协议之一。FTP允许用户将文件从一个计算机发送到另一个计算机,也可以从服务器下载文件到本地计算机。
-
客户端-服务器模型:FTP基于客户端-服务器模型。客户端是用户的计算机,而服务器是存储文件的远程计算机。客户端通过FTP客户端软件与服务器进行通信。
-
端口:FTP使用两个端口进行通信,一个用于控制连接(默认端口20/21),另一个用于数据传输。控制连接用于发送命令和接收响应,而数据连接用于实际的文件传输。
-
认证:用户需要提供用户名和密码以连接到FTP服务器。有些服务器可能允许匿名访问,这意味着用户可以使用一个通用的用户名(如"anonymous")来连接,但通常还需要提供电子邮件地址作为密码。
-
操作:一旦连接到服务器,用户可以执行各种操作,包括上传文件到服务器、从服务器下载文件、删除服务器上的文件、创建和删除目录等。
-
安全性:FTP最初是作为一种不安全的协议设计的,因为它在传输过程中不加密用户凭据或文件内容。然而,有一些安全扩展,如FTPS(基于SSL/TLS的FTP)和SFTP(SSH文件传输协议),提供了加密和身份验证功能。
-
被动模式和主动模式:FTP可以在两种模式下运行:主动模式和被动模式。在主动模式下,客户端向服务器发起数据连接,而在被动模式下,服务器向客户端发起数据连接。
FTP是一种广泛用于文件传输的协议,尽管它的安全性较低,但在某些情况下仍然非常有用,特别是在需要简单、快速地传输文件时。
vsftpd
vsftpd 是一个流行的开源 FTP 服务器软件,全称为 "Very Secure FTP Daemon"。它的设计目标是提供一个安全、稳定、高性能的 FTP 服务器。
-
安全性:vsftpd 以安全性著称。它实现了许多安全特性,包括支持 SSL/TLS 加密传输,以及支持 IPv6。通过 SSL/TLS 加密传输,vsftpd 可以确保在传输过程中数据的隐私和完整性。
-
轻量级:vsftpd 的设计注重轻量级和性能。它的代码精简,并且在设计时考虑了高性能。这使得 vsftpd 在资源受限的环境下运行效率很高。
-
配置灵活:vsftpd 提供了广泛的配置选项,管理员可以根据需求对其进行灵活配置。这些配置选项允许管理员限制用户的访问权限、配置用户账户和密码策略、限制并发连接数等。
-
虚拟用户支持:vsftpd 支持虚拟用户,这意味着用户可以被映射到系统上不存在的用户。这对于创建 FTP 服务的虚拟环境非常有用,同时也增强了安全性。
-
日志记录:vsftpd 提供了丰富的日志记录功能,允许管理员跟踪用户连接、文件传输等活动。这对于监控和审计 FTP 服务器非常重要。
-
支持限速:vsftpd 允许管理员配置上传和下载速度限制,以控制用户对服务器资源的使用。
vsftpd 是一个功能强大、安全可靠的 FTP 服务器软件,适用于各种规模的部署。它的性能高、配置灵活,并且具有良好的安全特性,因此被广泛应用于企业和个人用户的 FTP 服务中。
FTP安装
执行以下命令即可安装ftp
yum install -y vsftpd
FTP配置
ftp使用20和21端口,须在安全组中放行两端口,同样放行被动模式下ftp服务器创建端口
vsftpd常用配置
配置文件一般在/etc/vsftpd下,共3个文件
- ftpusers 黑名单文件
- user_list 白名单文件(当userlist_deny为NO时,允许名单内用户访问;为YES时,禁止名单内用户访问)
- vsftpd.conf 核心配置文件
初始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.
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=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=YES
pam_service_name=vsftpd
userlist_enable=YES
配置文件中部分参数含义如下:
| 参数 | 含义 |
|---|---|
| anonymous_enable | 是否允许匿名用户访问 |
| local_enable | 是否允许本地用户访问 |
| write_enable | 是否支持写命令 |
| local_umask | 本地用户掩码(实际权限=文件权限-local_umask) |
| anon_upload_enable | 是否允许匿名用户上传 |
| anon_mkdir_write_enable | 是否允许匿名用户创建文件夹 |
| dirmessage_enable | 是否启用显示目录消息 |
| xferlog_enable | 是否启用传输日志记录 |
| connect_from_port_20 | 是否指定ftp使用20端口传输数据 |
| chown_uploads | 是否允许上传文件后改变文件的所有者 |
| chown_username | 指定上传文件后要更改为的所有者用户名 |
| xferlog_file | 传输日志文件的路径 |
| xferlog_std_format | 是否使用标准的 xferlog 格式记录日志 |
| idle_session_timeout | 空闲会话超时时间 |
| data_connection_timeout | 数据连接超时时间 |
| nopriv_user | 指定 vsftpd 运行时使用的非特权用户 |
| async_abor_enable | 是否启用异步 ABOR 支持 |
| ascii_upload_enable | 是否允许以 ASCII 模式上传文件 |
| ascii_download_enable | 是否允许以 ASCII 模式下载文件 |
| ftpd_banner | ftp欢迎信息 |
| deny_email_enable | 是否启用拒绝电子邮件功能 |
| banned_email_file | 指定包含被禁止连接的电子邮件地址列表的文件路径 |
| chroot_local_user | 是否限制用户在家目录中 |
| chroot_list_enable | 是否启用 chroot 列表功能 |
| chroot_list_file | 指定包含 chroot 用户列表的文件路径 |
| ls_recurse_enable | 是否启用递归列表功能 |
| listen | 是否允许监听,如果设置为YES,则vsftpd将以独立模式运行,独立监听和处理IPv4端口的连接请求 |
| listen_ipv6 | 是否监听IPv6socket |
| pam_service_name | pam认证时配置文件名称 |
| userlist_enable | 是否允许名单内用户访问 |
| no_anon_password | 是否不需要匿名用户密码 |
| anon_root | 匿名用户根目录 |
| local_root | 本地用户根目录 |
| file_open_mode | 上传文件的权限(配合umask) |
| anon_other_write_enable | 是否授予匿名用户其它权限(修改删除) |
| tcp_wrappers | 在vsftpd中使用tcp_wrappers远程访问控制机制 |
| guest_enable | 是否允许虚拟用户访问 |
| guest_username | 虚拟用户映射的本地用户名称 |
| pasv_min_port | 被动模式数据传输最小端口号 |
| pasv_max_port | 被动模式数据传输最大端口号 |
| allow_writeable_chroot | 是否允许对chroot目录写 |
详细需要参照ftp配置手册
FTP访问模式
ftp访问模式有三类:
- 匿名模式
- 本地用户模式
- 虚拟用户模式
三者安全性由低到高变化,匿名模式最不安全,虚拟用户模式最安全
本地用户模式即创建本地用户作为ftp用户登录,当用户名密码泄露使得登录者获得完整ftp服务器权限,对ftp服务器造成破坏;
虚拟用户模式同样以本地用户为基础,但是是创建虚拟用户映射到该本地用户,对外使用虚拟用户的用户名和密码登录,登录ftp用户实际并不存在因此称虚拟地,能较好地保护ftp服务器,若是希望提高ftp服务器安全性,建议使用此访问模式
虚拟用户模式配置
进入vsftpd配置目录
配置文件可修改为下:
anonymous_enable=NO
local_enable=YES
local_root=/mnt/oss/ftp
write_enable=NO
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
guest_enable=YES
guest_username=ftpusr
user_config_dir=/etc/vsftpd/vuser_conf
pasv_min_port=50000
pasv_max_port=50010
ftpd_banner=Welcome to William's ftp Server!
userlist_enable=YES
allow_writeable_chroot=NO
chroot_local_user=YES
在安全组中放行设定的被动模式下数据传输端口范围
-
注意当chroot_local_user设置为真,allow_writeable_chroot为假时,chroot不允许在一个不可写目录下写文件,将无法登录ftp服务器,此处是为安全考虑有意设置,届时可以考虑以下方案解决:
- 为每个用户单独设置配置文件,可精细化操作权限
- 将家目录父目录设置为不可写而家目录可写
- 直接设置allow_writeable_chroot为真,允许家目录可写
配置方面比较自由,可根据自身需要完成配置,不必拘泥于上述配置.
准备创建用户数据库文件,可从txt文件构造,每两行保存用户名和密码,示例如下:
vim vusers
ftpuser
ftp123
生成db格式,并更改权限
db_load -T -t hash -f vusers vusers.db
chmod 600 vusers vusers.db
先使用以下命令查找pam模块所在
find / -name pam_userdb.so
确认位置后编辑pam认证文件
vim /etc/pam.d/vsftpd
添加下列配置,注意自己的vsftpd配置所在路径
auth required /usr/lib64/security/pam_userdb.so db=/etc/vsftpd/vusers
account required /usr/lib64/security/pam_userdb.so db=/etc/vsftpd/vusers
- 注意将其它内容注释掉,否则后续登录可能不会使用所创建vusers.db文件,无法通过pam验证
继续创建用于虚拟用户的权限配置文件
mkdir vuser_conf
vim vuser_conf/ftpuser
可配置如下:
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
write_enable=YES
allow_writeable_chroot=YES
注意要根目录需真实存在,请及时创建该目录
因为虚拟用户实际是映射到本地用户,还需创建一本地用户,并验证结果
useradd -s /sbin/nologin ftpusr
tail -1 /etc/passwd
及时创建虚拟用户根目录后,将权限授予ftpusr即ftp本地用户
chown -R ftpusr:ftpusr /mnt/oss/ftp
chmod -R 777 /mnt/oss/ftp
重启vsftpd即可
systemctl restart vsftpd
-
当出现类似无法读写的情况,典型地如返回“226 Transfer done (but failed to open directory)”,一般为权限问题,但可能基于不同原因
-
SELINUX可能会阻止ftp访问,可禁用或打开ftp相关权限.
-
setsebool -P ftpd_full_access 1 -
检查文件权限,可使用以下命令设置权限
-
chown -R xxx /file -
若使用虚拟用户,检查映射的本地用户是否拥有权限
-
本人在使用挂载oss搭建ftp服务器时,虚拟用户总出现权限问题,经排查是用于ftp地用户不具有访问oss挂载点的权限,后设置允许其他用户访问后解决该问题
-