FTP服务(二)--虚拟用户

本文详细介绍如何在VSFTPD中配置虚拟用户,包括基于文件和MySQL数据库的两种验证方式,以及如何为不同用户设置独立的访问权限。

一、虚拟用户简介

FTP的虚拟用户是特定服务的专用用户,拥有独立的用户/密码文件。所有虚拟用户会统一映射为一个指定的系统帐号:访问共享位
置,即为此系统帐号的家目录,各虚拟用户可被赋予不同的访问权限,通过匿名用户的权限控制参数进行指定。

虚拟用户帐号有两种存储方式,一种是文件存储方式:编辑文本文件,此文件需要被编码为hash格式奇数行为用户名,偶数行为密码。另一种是数据库存储,存放在关系型数据库的表中,可实时查询数据库完成用户认证。接下来讲解如何详细实现上述这两种方式。

二、实现基于文件验证的vsftpd虚拟用户

1、服务器端创建虚拟用户数据库文件

[root@centos7 vsftpd]# vim vusers.txt

wang
123456
mage
123456

把文本文件转化为数据库文件

[root@centos7 vsftpd]# cd /etc/vsftpd/
[root@centos7 vsftpd]# db_load -T -t hash -f vusers.txt vusers.db
[root@centos7 vsftpd]# chmod 600 vusers.db   ##为了安全起见把vusers.db文件的权限改为600
[root@centos7 vsftpd]# ll -d vusers.db 
-rw------- 1 root root 12288 3月  15 10:53 vusers.db

2、创建系统用户和定义访问FTP目录的权限。(将来虚拟用户登录时将会映射为此用户)

[root@centos7 vsftpd]# useradd -d /data/vuser -s /sbin/nologin vuser
[root@centos7 vsftpd]# ll -d  /data/vuser/
drwx------ 3 vuser vuser 78 3月  15 11:02 /data/vuser/
[root@centos7 vsftpd]# chmod +rx /data/vuser/
[root@centos7 vsftpd]# ll -d  /data/vuser/
drwxr-xr-x 3 vuser vuser 78 3月  15 11:02 /data/vuser/

centos7 还需要执行以下操作:

[root@centos7 vsftpd]# chmod -w /data/vuser/
[root@centos7 vsftpd]# ll -d  /data/vuser/
dr-xr-xr-x 3 vuser vuser 78 3月  15 11:02 /data/vuser/

[root@centos7 vsftpd]# mkdir /data/vuser/upload/
[root@centos7 vsftpd]# setfacl -m u:vuser:rwx /data/vuser/upload/

3、创建pam配置文件

[root@centos7 vsftpd]# vim /etc/pam.d/vsftpd.db

auth required pam_userdb.so db=/etc/vsftpd/vusers
account required pam_userdb.so db=/etc/vsftpd/vusers

4、修改配置文件,并指定pam配置文件

[root@centos7 vsftpd]# vim /etc/vsftpd/vsftpd.conf

guest_enable=yes
guest_username=vuser
pam_service_name=vsftpd.db

5、SELinux设置
禁用SELinux 或者 setsebool -P ftpd_full_access 1
6、虚拟用户建立独立的配置文件
创建配置文件存放的路径

[root@centos7 vsftpd]# mkdir /etc/vsftpd/vusers.d/
[root@centos7 vsftpd]# vim /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/vusers.d/

cd /etc/vsftpd/vusers.d/ 进入此目录,
创建各用户自已的配置文件
wang用户的配置文件
允许wang用户可读写,其它用户只读

[root@centos7 vusers.d]# vim wang

anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

mage用户的配置文件
登录目录改变至指定的目录

[root@centos7 vusers.d]# vim mage

local_root=/data/ftptest

验证:
1、使用wang用户登录,并新建文件。

[root@centos7 data]# ftp 192.168.239.132
Connected to 192.168.239.132 (192.168.239.132).
220 (vsFTPd 3.0.2)
Name (192.168.239.132:root): wang
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/"
ftp> ls
227 Entering Passive Mode (192,168,239,132,201,163).
150 Here comes the directory listing.
drwxrwxr-x    2 0        0               6 Mar 15 04:19 upload
226 Directory send OK.
ftp> cd upload
250 Directory successfully changed.
ftp> mkdir wangdir
257 "/upload/wangdir" created

2、使用mage用户登录,并验证是否在/data/ftptest目录下。(/data/ftptest目录下事先存在ftpfile5这个文件)

[root@centos7 data]# ftp 192.168.239.132
Connected to 192.168.239.132 (192.168.239.132).
220 (vsFTPd 3.0.2)
Name (192.168.239.132:root): mage
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/"
ftp> ls
227 Entering Passive Mode (192,168,239,132,132,164).
150 Here comes the directory listing.
-rw-r--r--    1 0        0              20 Mar 14 14:49 ftpfile5
226 Directory send OK.
ftp> 

二、实现基于MYSQL验证的vsftpd虚拟用户

1、安装所需要包和包组:
安装并启动数据库。

[root@centos7 vusers.d]# yum –y install mariadb-server
[root@centos7 vsftpd]# systemctl start mariadb.service

在FTP服务器上安装vsftpd和pam_mysql包(centos7系统无对应rpm包,需手动编译安装)

安装开发工具包组
[root@centos7 vsftpd]# yum -y groupinstall "Development Tools"
安装mariadb的开发工具及pam模块的开发工具
[root@centos7 vsftpd]# yum -y install mariadb-devel pam-devel

下载pam_mysql-0.7RC1.tar.gz

[root@centos7 vsftpd]# cd /app
[root@centos7 app]# ls
pam_mysql-0.7RC1.tar.g
[root@centos7 app]# tar xf pam_mysql-0.7RC1.tar.gz 
[root@centos7 app]# ls
pam_mysql-0.7RC1

进入cd pam_mysql-0.7RC1/目录下,开始编译安装,编译安装前先详细阅读一下安文档。

[root@centos7 app]# cd pam_mysql-0.7RC1/
[root@centos7 pam_mysql-0.7RC1]# ls
acinclude.m4  ChangeLog     config.h.in  configure     COPYING  INSTALL     ltmain.sh    Makefile.in  mkinstalldirs  pam_mysql.c     pam_mysql.spec.in  README
aclocal.m4    config.guess  config.sub   configure.in  CREDITS  install-sh  Makefile.am  missing      NEWS           pam_mysql.spec  pkg.m4             stamp-h.in

[root@centos7 pam_mysql-0.7RC1]# ./configure --with-pam=/usr --with-pam-mods-dir=/lib64/security
[root@centos7 pam_mysql-0.7RC1]# make && make install
[root@centos7 pam_mysql-0.7RC1]# ls /lib64/security | grep pam_mysql.so
pam_mysql.so

2、在数据库服务器上创建虚拟用户账号
建立存储虚拟用户数据库和连接的数据库用户

MariaDB [(none)]>  CREATE DATABASE vsftpd;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| hellodb_innodb     |
| mysql              |
| performance_schema |
| test               |
| vsftpd             |
+--------------------+
6 rows in set (0.11 sec)
MariaDB [(none)]> GRANT SELECT ON vsftpd.* TO 
    -> vsftpd@localhost IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]> GRANT SELECT ON vsftpd.* TO  vsftpd@127.0.0.1 IDENTIFIED BY '123456';
Query OK, 0 rows affected (0.00 sec)

MariaDB [(none)]>  FLUSH PRIVILEGES;
Query OK, 0 rows affected (0.00 sec)

准备相关表

MariaDB [(none)]> use vsftpd
Database changed
MariaDB [vsftpd]> show tables;
Empty set (0.00 sec)

MariaDB [vsftpd]> CREATE TABLE users (
    -> id INT AUTO_INCREMENT NOT NULL PRIMARY KEY,
    -> name CHAR(50) BINARY NOT NULL,
    -> password CHAR(48) BINARY NOT NULL);
Query OK, 0 rows affected (0.01 sec)

MariaDB [vsftpd]> DESC users;
+----------+----------+------+-----+---------+----------------+
| Field    | Type     | Null | Key | Default | Extra          |
+----------+----------+------+-----+---------+----------------+
| id       | int(11)  | NO   | PRI | NULL    | auto_increment |
| name     | char(50) | NO   |     | NULL    |                |
| password | char(48) | NO   |     | NULL    |                |
+----------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)

测试连接

[root@centos7 pam_mysql-0.7RC1]# mysql -uvsftpd  -p123456
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 3
Server version: 5.5.56-MariaDB MariaDB Server

Copyright (c) 2000, 2017, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> SHOW DATABASES;
+--------------------+
| Database           |
+--------------------+
| information_schema |
| test               |
| vsftpd             |
+--------------------+
3 rows in set (0.00 sec)

添加虚拟用户
根据需要添加所需要的用户,为了安全应该使用PASSWORD函数加密其密码后存储。

MariaDB [(none)]> use vsftpd
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [vsftpd]> DESC users;
+----------+----------+------+-----+---------+----------------+
| Field    | Type     | Null | Key | Default | Extra          |
+----------+----------+------+-----+---------+----------------+
| id       | int(11)  | NO   | PRI | NULL    | auto_increment |
| name     | char(50) | NO   |     | NULL    |                |
| password | char(48) | NO   |     | NULL    |                |
+----------+----------+------+-----+---------+----------------+
3 rows in set (0.00 sec)
MariaDB [(none)]> use vsftpd
Reading table information for completion of table and column names
You can turn off this feature to get a quicker startup with -A

Database changed
MariaDB [vsftpd]> INSERT INTO users(name,password) values('wang',password('123456'));
Query OK, 1 row affected (0.29 sec)

MariaDB [vsftpd]> INSERT INTO users(name,password) values('mage',password('123456'));
Query OK, 1 row affected (0.00 sec)
MariaDB [vsftpd]> SELECT * FROM users;
+----+------+-------------------------------------------+
| id | name | password                                  |
+----+------+-------------------------------------------+
|  1 | wang | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
|  2 | mage | *6BB4837EB74329105EE4568DDA7DC67ED2CA2AD9 |
+----+------+-------------------------------------------+
2 rows in set (0.00 sec)

3、在FTP服务器上配置vsftpd服务
在FTP服务器上建立pam认证所需文件(可以参考./pam_mysql-0.7RC1目录中的README文档,选择正确的加密方式)

[root@centos7 pam_mysql-0.7RC1]# vim /etc/pam.d/vsftpd.mysql

auth required pam_mysql.so user=vsftpd passwd=magedu host=mysqlserver db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required pam_mysql.so user=vsftpd passwd=magedu host=mysqlserver db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2

解释:
crypt是加密方式,0表示不加密,1表示crypt(3)加密,2表示使用mysql password()函数加密,3表示md5加密,4表示sha1加密。
auth 表示认证
account 验证账号密码正常使用
required 表示认证要通过
pam_mysql.so模块是默认的相对路径,是相对/lib64/security/路径而言,也可以写绝对路径;后面为给此模块传递的参数
user=vsftpd为登录mysql的用户
passwd=magedu 登录mysql的的密码
host=mysqlserver mysql服务器的主机名或ip地址
db=vsftpd 指定连接msyql的数据库名称
table=users 指定连接数据库中的表名
usercolumn=name 当做用户名的字段
passwdcolumn=password 当做用户名字段的密码
crypt=2 密码的加密方式为mysql password()函数加密

建立相应用户和修改vsftpd配置文件,使其适应mysql认证
建立虚拟用户映射的系统用户及对应的目录

[root@centos7 vsftpd]# useradd -d /data/vuser -s /sbin/nologin vuser
[root@centos7 vsftpd]# ll -d  /data/vuser/
drwx------ 3 vuser vuser 78 3月  15 11:02 /data/vuser/
[root@centos7 vsftpd]# chmod +rx /data/vuser/
[root@centos7 vsftpd]# ll -d  /data/vuser/
drwxr-xr-x 3 vuser vuser 78 3月  15 11:02 /data/vuser/

centos7 还需要执行以下操作:

[root@centos7 vsftpd]# chmod -w /data/vuser/
[root@centos7 vsftpd]# ll -d  /data/vuser/
dr-xr-xr-x 3 vuser vuser 78 3月  15 11:02 /data/vuser/

[root@centos7 vsftpd]# mkdir /data/vuser/upload/
[root@centos7 vsftpd]# setfacl -m u:vuser:rwx /data/vuser/upload/

确保/etc/vsftpd.conf中已经启用了以下选项anonymous_enable=YES

[root@centos7 pam_mysql-0.7RC1]# cat /etc/vsftpd/vsftpd.conf| grep anonymous_enable
anonymous_enable=YES

添加下面两项
guest_enable=YES
guest_username=vuser

guest_enable=YES
guest_username=vuser

修改下面一项,此时原有的系统用户无法登录
pam_service_name=vsftpd.mysql

pam_service_name=vsftpd.mysql

4、启动vsftpd服务

[root@centos7 pam_mysql-0.7RC1]# systemctl restart vsftpd         
[root@centos7 pam_mysql-0.7RC1]# ss -ntl | grep 21
LISTEN     0      32          :::21                      :::*                  

5、Selinux相关设置:在FTP服务器上执行
关闭selinux

[root@centos7 pam_mysql-0.7RC1]# getenforce
Disabled

6、在FTP服务器上配置虚拟用户具有不同的访问权限vsftpd可以在配置文件目录中为每个用户提供单独的配置文件以定义其ftp服务访问权限,每个虚拟用户的配置文件名同虚拟用户的用户名。配置文件目录可以是任意未使用目录,只需要在vsftpd.conf指定其路径及名称即可
配置vsftpd为虚拟用户使用配置文件目录
添加如下选项
user_config_dir=/etc/vsftpd/vusers.d/

user_config_dir=/etc/vsftpd/vusers.d/

创建所需要目录,并为虚拟用户提供配置文件,分别创建wang用户和mage用户
虚拟用户对vsftpd服务的访问权限是通过匿名用户的相关指令进行的。如果需要让用户wang具有上传文件的权限,可以修改/etc/vsftpd/vusers.d/wang文件,在里面添加如下选项并设置为YES即可,只读则设为NO
注意:需确保对应的映射用户对于文件系统有写权限
anon_upload_enable={YES|NO}
anon_mkdir_write_enable={YES|NO}
anon_other_write_enable={YES|NO}
local_root=/data/ftptest 登录目录改变至指定的目录

[root@centos7 vsftpd]# mkdir /etc/vsftpd/vusers.d/
[root@centos7 vusers.d]# cd /etc/vsftpd/vusers.d/
[root@centos7 vusers.d]# vim wang 
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
[root@centos7 vusers.d]# vim mage 
local_root=/data/ftptest                       
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值