【超详细】阿里云CentOS7.9搭建FTP服务器,mysql+vsftpd虚拟用户实现ftp用户拥有不同权限

需求:

1.搭建ftp服务器
2.创建ftp服务器管理员:ftpadmin,权限为可创建、删除、修改、下载、上传目录和文件
3.创建ftp服务器用户:ftpuser1,权限为仅下载文件、目录
4.创建ftp服务器用户:ftpuser2,权限为仅上传文件、目录

环境:

Centos7.9

实现原理:

创建本地用户的虚拟副本作为访问ftp服务器的虚拟用户,虚拟用户的账号密码单独保存在本机的mysql数据库中,为每个虚拟用户单独配置权限。

具体实现:

1.安装vsftpd
yum install vsftpd -y
systemctl enable vsftpd
2.安装mysql
yum install mariadb* -y
3.安装pam
yum install pam-devel pam -y
4.安装开发工具
yum install gcc gcc-c++ -y
或直接安装开发环境
yum groupinstall "Development Tools" "Server Platform Development" -y
5.安装pam_mysql
yum install pam_mysql -y

若库里没有pam_mysql,则需要手动下载编译:

(1) 下载pam_mysql-0.7RC1.tar.gz

​ https://sourceforge.net/projects/pam-mysql/files/latest/download

(2) 编译安装

​ 复制pam_mysql-0.7RC1.tar.gz到root目录

cd ~
tar xvf pam_mysql-0.7RC1.tar.gz
cd pam_mysql-0.7RC1
./configure --with-pam=/usr --with-mysql=/usr --with-pam-mods-dir=/usr/lib64/security
make && make install
cd /usr/lib64/security
ls | grep pam_mysql.so #如果存在pam_mysql.so,则说明pam_mysql安装成功
6.启动mysql
systemctl start mariadb
systemctl enable mariadb
mysql -u root -p -h localhost #mariadb root密码默认为空
7.创建mysql授权用户、创建表
GRANT ALL ON vsftpd.* TO mysqlftp@'127.0.0.1' IDENTIFIED BY 'Mima123';
FLUSH PRIVILEGES;
exit;
mysql -umysqlftp -pMima123 -h127.0.0.1
CREATE DATABASE vsftpd;
USE vsftpd;
CREATE table users (
	id INT AUTO_INCREMENT NOT NULL,
  username VARCHAR(30) BINARY UNIQUE NOT NULL,
  password VARCHAR(128) BINARY NOT NULL,
  PRIMARY KEY(id)
);
INSERT INTO users(username,password) VALUES ('ftpadmin',PASSWORD('Mima123')),('ftpuser1',PASSWORD('Mima123')),('ftpuser2',PASSWORD('Mima123'));
exit
8.创建ftp仓库根目录,添加ftp用户
mkdir /FTPRepository
useradd -M -d /FTPRepository -s /sbin/nologin vsftp
9.创建pam配置文件/etc/pam.d/vsftpd.vusers,内容为用mysql来做ftp的pam验证
vim /etc/pam.d/vsftpd.mysql
#键入以下内容:
auth required /usr/lib64/security/pam_mysql.so user=mysqlftp passwd=Mima123 host=127.0.0.1 db=vsftpd table=users usercolumn=username passwdcolumn=password crypt=2 
account required /usr/lib64/security/pam_mysql.so user=mysqlftp passwd=Mima123 host=127.0.0.1 db=vsftpd table=users usercolumn=username passwdcolumn=password crypt=2 
10.修改vsftpd的配置文件/etc/vsftpd/vsftpd.conf
anonymous_enable=NO
#virtual_use_local_privs=YES
guest_enable=YES
pam_service_name=vsftpd.mysql
guest_username=vftp
user_config_dir=/etc/vsftpd/virtual_user_conf
11.重启vsftpd服务
systemctl restart vsftpd
12.单独配置虚拟用户的权限
mkdir -p /etc/vsftpd/virtual_user_conf && cd $_
vim ftpadmin
#键入以下内容:
anon_upload_enable=YES #允许上传
anon_mkdir_write_enable=YES #允许创建目录
anon_other_write_enable=YES #允许其他写操作
allow_writeable_chroot=YES #允许切换目录
anon_umask=022 #ftpadmin用户创建的目录权限为755

vim ftpuser1
#键入以下内容
download_enable=YES #允许下载

vim ftpuser2
#键入以下内容
anon_upload_enable=YES #允许上传
anon_umask=022 #ftpuser1用户上传的目录权限为755

注:

虚拟用户ftpadmin、ftpuser1、ftpuser2相当于本地用户vftp的副本,使用虚拟用户创建文件和目录时,该文件和目录的所属用户和所属组为vftp:vftp。

当/FTPRepository的所属用户和所属组为root:root,且权限为755时,即使赋予虚拟用户创建文件和目录的权限,虚拟用户也不能在/FTPRepository 下创建目录和文件,这是因为vftp用户相对于root,属于其他用户,而其他用户的权限仅为r_x。

此时,如果希望虚拟用户拥有创建文件和目录的权限,可以将虚拟用户设置为root的副本。具体操作如下:

8.使用root用户创建目录/FTPRepository,并修改权限为755,不需要新建本地用户vftp
mkdir /FTPRepository 
chmod 755 /FTPRepository 
9.同第9步
10.修改vsftpd的配置文件
anonymous_enable=NO
#virtual_use_local_privs=YES
guest_enable=YES
pam_service_name=vsftpd.mysql
guest_username=root
user_config_dir=/etc/vsftpd/virtual_user_conf
11.重启vsftpd
systemctl restart vsftpd
12.单独配置虚拟用户权限

ftpadmin:

mkdir -p /etc/vsftpd/virtual_user_conf && cd $_
vim ftpadmin
#键入以下内容:
#该虚拟用户登录ftp服务器时,根目录为/FTPRepository 
local_root=/FTPRepository 
#允许上传
anon_upload_enable=YES
#允许创建目录
anon_mkdir_write_enable=YES
#允许其他写操作:改、删
anon_other_write_enable=YES
#允许下载
download_enable=YES
#允许切换目录。
#使用该虚拟用户登录ftp服务器时,理论上默认登录的目录是与虚拟用户同名的目录,这里在此配置文件中将默认目录改为了/FTPRepository ,如果不添加此配置,则使用该虚拟用户登录ftp时会报错:500 OOPS:vsftpd:refusing to run with writeable root inside chroot()
allow_writeable_chroot=YES
#ftpadmin创建的目录权限为755,因为ftpadmin是root用户的副本,此处如果不指定权限,则默认同root权限。可以使用umask命令来查看当前登录用户的权限,如果不是0022,可以在/etc/profile中增加一行umask 0022,然后source /etc/profile,使立即生效。
anon_umask=022

ftpuser1:

vim ftpuser1
#键入以下内容:
#该虚拟用户登录ftp服务器时,根目录为/FTPRepository 
local_root=/FTPRepository 
#允许上传
anon_upload_enable=NO
#不允许创建目录
anon_mkdir_write_enable=NO
#允许下载
download_enable=YES
#允许切换目录。
allow_writeable_chroot=YES
anon_umask=022

ftpuser2:

vim ftpuser2
#键入以下内容:
#该虚拟用户登录ftp服务器时,根目录为/FTPRepository 
local_root=/FTPRepository 
#允许上传
anon_upload_enable=YES
#不允许创建目录
anon_mkdir_write_enable=NO
#不允许下载
download_enable=NO
#允许切换目录。
allow_writeable_chroot=YES
anon_umask=022

阿里云FTP服务器异常:

如果在阿里云上搭建FTP服务器后,在windows资源管理器中使用ftp://ftp_ip访问搭建的服务器时,出现下面两种错误:

打开FTP服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。

详细信息:

200 Switching to ASCII mode.

227 Entering Passive Mode(172,22,47,109,39,18)
打开FTP服务器上的文件夹时发生错误。请检查是否有权限访问该文件夹。

详细信息:

操作超时

需要在配置文件中添加下面几句,并在阿里云安全组中开放10000~10020端口:

pasv_address=ftp服务器公网ip
pasv_enable=YES
pasv_min_port=10000
pasv_max_port=10020

详见:https://developer.aliyun.com/article/601945

附:完整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=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=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
pasv_address=阿里云服务器公网ip

pasv_enable=YES
pasv_min_port=10000
pasv_max_port=10020

userlist_enable=YES
tcp_wrappers=YES

guest_enable=YES
pam_service_name=vsftpd.mysql
guest_username=root
user_config_dir=/etc/vsftpd/virtual_user_conf

猜你喜欢

转载自blog.csdn.net/weixin_43654975/article/details/125955297