摘要:
手把手教你搭建vsftpd服务器,实现了基于db文件和MySQL数据库文件进行虚拟用户认证,当然了也本文章也包含搭建过程中问题的排查啦,哈哈哈。
另外,我在CentOS 7.x上也进行了搭建测试,步骤一致,个别命令会有不同,但不影响测试效果。
服务器部署需求分析(实现效果):
1
2
3
1、登录、传输全程进行加密处理。
2、记录用户上传、下载文件日志。
3、针对每个用户有公共文件夹(大家都可以看到和下载其中的文件)和私有文件夹(只有自己可以看到,并且可上传下载文件)。
一、vsftpd服务安装:
1
1、搜索yum源中是否有vsftpd的安装包:
1
2
3
4
5
6
[iyunv@dba ~]# cat /etc/centos-release
CentOS release 6.5 (Final)
[iyunv@dba ~]# yum list | grep -i "vsftpd"
vsftpd.x86_64 2.2.2-24.el6 base
You have new mail in /var/spool/mail/root
2、安装vsftpd服务程序:
1
2
3
4
[iyunv@dba ~]# yum list | grep -i "vsftpd" && yum -y install vsftpd
[iyunv@dba ~]# whereis vsftpd
vsftpd: /usr/sbin/vsftpd /etc/vsftpd /usr/share/man/man8/vsftpd.8.gz
3、启动vsftpd服务程序:
1
2
3
4
5
[iyunv@dba ~]# /etc/init.d/vsftpd start
为 vsftpd 启动 vsftpd: [确定]
[iyunv@dba ~]# netstat -lntp | grep "21"
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 1677/vsftpd
4、访问vsftpd服务:
二、vsftpd服务配置详解:
1、备份配置文件及用户黑白名单配置:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
[iyunv@dba ~]# cd /etc/vsftpd/
[iyunv@dba vsftpd]# ll
总用量 20
-rw------- 1 root root 125 3月 22 20:14 ftpusers
-rw------- 1 root root 361 3月 22 20:14 user_list
-rw------- 1 root root 4599 3月 22 20:14 vsftpd.conf
-rwxr--r-- 1 root root 338 3月 22 20:14 vsftpd_conf_migrate.sh
[iyunv@dba vsftpd]# cp vsftpd.conf vsftpd.conf_bak
[iyunv@dba vsftpd]# ll
总用量 28
-rw------- 1 root root 125 3月 22 20:14 ftpusers
-rw------- 1 root root 361 3月 22 20:14 user_list
-rw------- 1 root root 4599 3月 22 20:14 vsftpd.conf
-rw------- 1 root root 4599 8月 18 23:21 vsftpd.conf_bak
-rwxr--r-- 1 root root 338 3月 22 20:14 vsftpd_conf_migrate.sh
ftpusers:保存ftp登录用户黑名单,该列表中用户登录时需要输入用户名跟密码,但是不能登录成功。
user_list:用户白名单(黑名单)列表,白名单或黑名单由vsftpd.conf中参数进行控制。
白名单:
userlist_enable=YES #启用用户列表功能
userlist_deny=NO #配置不禁用user_list中的用户登录
userlist_file=/etc/vsftpd/user_list #指定用户列表文件的路径和文件名
userlist_log=YES #基于user_list被阻塞的用户记录日志
黑名单:
userlist_enable=YES #启用用户列表功能
userlist_deny=YES #配置禁用user_list中的用户登录,在列表中的用户输入登录名后不需要输入密码就会被阻塞,相比ftpusers更为安全
userlist_file=/etc/vsftpd/user_list #指定用户列表文件的路径和文件名
userlist_log=YES
2、其他重要配置项介绍:
1
配置项语法格式:directive=value
1
2
3
4
5
6
7
8
9
10
11
12
匿名用户:
匿名用户是被映射成ftp用户的,属主有写权限就行;
是否允许匿名用户登录:
anonymous_enable=YES
匿名用户的上传操作;生效要依赖于write_enable=YES
anon_upload_enable=NO
workdirrite_enable=YES
属主有写权限就行;
anon_mkdir_write_enable=NO
匿名用户创建目录的权限;
anon_other_write_enable=NO
匿名用户删除、重命名操作权限;
1
2
3
4
5
本地用户:
local_enable
所有的非匿名用户的生效,都依赖此指令;
local_umask
本地用户上传文件的权限掩码;
1
2
3
4
目录消息:
dirmessage_enable
用户第一次进入目录时,vsftpd会查看.message文件,并将其内容显示给用户,messag_file指定文件路径,而不使用默认的.message;
messag_file
1
2
3
4
5
数据传输日志:
xferlog_enable
xferlog_std_format
xferlog_file=/var/log/vsftpd.log
vsftpd_log_file
1
2
数据传输模式:
connect_from_port_20;是否启用PORT模式,默认是NO;
1
2
3
4
修改匿名用户上传的文件的属主:
chown_uploads:是否修改;
chown_username:启用chown_uploads指令时,将文件属主修改为此指令指定的用户;默认为root;
chown_upload_mode:设定匿名用户上传文件的权限;默认为600;
1
2
3
4
设定会话的超时时间:
idle_session_timeout,空闲会话的超时时长;
connect_timeout:PORT模式下,服务器连接客户端的超时时长;
data_connection_timeout:数据传输的超时时长;
1
2
命令连接的监听端口:
listen_port=20
1
2
3
4
5
设定连接及端口速率:
local_max_rate:本地用户的传输速率,单位为字节;默认为0,表示无限制;
anon_max_rate:匿名用户的最大传输速率;
max_clients:最大的并发连接数,对服务器端;
max_per_ip:每个客户端同时发起的最大连接数;
1
2
登录欢迎信息:
ftp_banner="Welcome"
1
2
3
4
5
6
7
禁锢本地用户:
chroot_local_user=YES;禁锢所有本地用户;
注意:要求用户不能对家目录有写权限;
chroot_list_enable=YES;
chroot_list_file=/etc/vsftpd/chroot_list;
禁锢指定用户于家目录中;
1
2
3
4
5
6
7
8
9
10
11
虚拟用户配置:
基于db文件:
/etc/vsftpd/vusers.txt
奇数行:用户名
偶数行:密码
基于mysql数据库:
用户名、密码可以通过SQL语句添加到数据库中,vsftpd通过pam接口到与mysql数据库联动实现用户认证的操作。
注意:虚拟用户和匿名用户登录ftp服务器时,是映射到本地用户列表中到某用户到,默认为ftp,但也可以手动指定!
[iyunv@dba vsftpd]# cat /etc/passwd | grep ftp
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
三、vsftpd典型配置案例:
1、基于db_load实现虚拟用户认证:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
#1.1 修改vsftpd.conf中相关配置项:
anonymous_enable=NO
chroot_local_user=YES
pam_service_name=vsftpd #开启pam认证功能,对应的pam配置文件名为vsftpd
guest_enable=YES
guest_username=ftp
user_config_dir=/etc/vsftpd/user_conf
#1.2 创建用户配置文件目录:
[iyunv@dba vsftpd]# install -d /etc/vsftpd/user_conf
[iyunv@dba vsftpd]# ll
总用量 28
-rw------- 1 root root 125 3月 22 20:14 ftpusers
drwxr-xr-x 2 root root 4096 8月 19 00:08 user_conf
-rw------- 1 root root 361 3月 22 20:14 user_list
-rw------- 1 root root 894 8月 19 00:08 vsftpd.conf
-rw------- 1 root root 4599 8月 18 23:21 vsftpd.conf_bak
-rwxr--r-- 1 root root 338 3月 22 20:14 vsftpd_conf
#1.3 创建ftp用户名密码源文件和对应的数据库文件:
[iyunv@dba vsftpd]# touch vusers
[iyunv@dba vsftpd]# echo -e "user01\nuser01" vusers
[iyunv@dba vsftpd]# echo -e "user02\nuser02" >> vusers
[iyunv@dba vsftpd]# more vusers
user01
user01
user02
user02
[iyunv@dba vsftpd]# db_load -T -t hash -f /etc/vsftpd/vusers /etc/vsftpd/login.db
[iyunv@dba vsftpd]# ll
总用量 44
-rw-r--r-- 1 root root 12288 8月 19 00:20 login.db
-rw-r--r-- 1 root root 28 8月 19 00:17 vusers
#1.4 创建每个用户的配置文件:
[iyunv@dba vsftpd]# cd user_conf/
[iyunv@dba user_conf]# more user02
# 使用local_root为虚拟用户指定家目录(ftp登录成功后默认在的目录)时,可以在用户单独配置文件中指定,也可以adduser新建用户,用-d选项指定用户家目录。
local_root=/ftp/user02
write_enable=YES
anon_umask=022
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
#1.5 配置pam认证文件,将db文件中用户跟vsftpd服务关联起来:
[iyunv@dba pam.d]# ll vsftpd*
-rw-r--r-- 1 root root 101 8月 19 00:28 vsftpd
-rw-r--r-- 1 root root 335 8月 19 00:27 vsftpd.bak
[iyunv@dba pam.d]# more vsftpd
auth required pam_userdb.so db=/etc/vsftpd/login
account required pam_userdb.so db=/etc/vsftpd/login
#1.6 创建用户对应的ftp主目录:
[iyunv@dba user_conf]# mkdir -p /ftp/user01/upload
[iyunv@dba user_conf]# mkdir -p /ftp/user02/upload
[iyunv@dba user_conf]# ls -ld /ftp/*
drwxr-xr-x 3 root root 4096 8月 19 00:31 /ftp/user01
drwxr-xr-x 3 root root 4096 8月 19 00:31 /ftp/user02
因为上面创建的用户user01、user02登录ftp服务器之后,是映射成本地用户ftp进行创建文件、目录的操作的,所以要机遇用户主目录属主为用户:ftp
[iyunv@dba ftp]# chown ftp:root user01
[iyunv@dba ftp]# chown ftp:root user02
[iyunv@dba ftp]# ls -ld *
drwxr-xr-x 3 ftp root 4096 8月 19 00:31 user01
drwxr-xr-x 3 ftp root 4096 8月 19 00:31 user02
注意:为用户家目录授权的时候要注意,有时候家目录存在写权限时,登录或上传文件时会报错。
#1.7 问题解决:
执行以上操作之后,从客户端可以正常登录到ftp服务器,但是上传文件到用户upload目录下时报错:
响应: 553 Could not create file.
错误: 严重文件传输错误
故查看upload目录写权限发现,upload目录属主权限不对,修改后上传文件正常。
[iyunv@dba ftp]# ls -ld user01/upload
drwxr-xr-x 2 root root 4096 8月 19 00:31 user01/upload
[iyunv@dba ftp]# ls -ld user02/upload
drwxr-xr-x 2 root root 4096 8月 19 00:31 user02/upload
[iyunv@dba ftp]# chown -R ftp:root user01/upload
[iyunv@dba ftp]# chown -R ftp:root user02/upload
[iyunv@dba ftp]# ls -ld user01/upload
drwxr-xr-x 2 ftp root 4096 8月 19 00:31 user01/upload
[iyunv@dba ftp]# ls -ld user02/upload
drwxr-xr-x 2 ftp root 4096 8月 19 00:31 user02/upload
#1.8 ftp协议因为历史原因,默认采用非加密方式进行认证和数据传输,要想使认证和传输过程加密暂时有两种方案可供选择:
方案一:采用openssl内建ftp服务器来进行加密传输,具体配置案例可参考链接:http://blog.csdn.net/xinxin19881112/article/details/46831311
方案二:采用vsftpd服务器端程序,修改配置实现对认证和传输过程加密,具体配置如下:
首先,生成加密使用的pem证书文件:
[iyunv@dba ftp]# openssl req -x509 -nodes -days 365 -newkey rsa:1024 -keyout /etc/vsftpd/vsftpd.pem -out /etc/vsftpd/vsftpd.pem
Generating a 1024 bit RSA private key
.++++++
........++++++
writing new private key to '/etc/vsftpd/vsftpd.pem'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Country Name (2 letter code) [XX]:CN
State or Province Name (full name) []:BeiJing
Locality Name (eg, city) [Default City]:BJ
Organization Name (eg, company) [Default Company Ltd]:360
Organizational Unit Name (eg, section) []:Security
Common Name (eg, your name or your server's hostname) []:anonymous.org
Email Address []:You have new mail in /var/spool/mail/root
生成的pem文件如下:
[iyunv@dba ~]# ll /etc/vsftpd/vsftpd.pem
-rw-r--r-- 1 root root 1876 8月 19 00:52 /etc/vsftpd/vsftpd.pem
然后,修改vsftpd.conf配置文件,开启加密:
# 启用TLS/SSL
ssl_enable=YES
allow_anon_ssl=NO
force_local_data_ssl=YES
force_local_logins_ssl=YES
ssl_tlsv1=YES
ssl_sslv2=NO
ssl_sslv3=NO
require_ssl_reuse=NO
ssl_ciphers=HIGH
rsa_cert_file=/etc/vsftpd/vsftpd.pem
rsa_private_key_file=/etc/vsftpd/vsftpd.pem
# 为被动模式下的连接定义端口范围
pasv_max_port=65535
pasv_min_port=64000
重启vsftpd服务:
### CentOS 6.x
[iyunv@dba vsftpd]# /etc/init.d/vsftpd restart
关闭 vsftpd: [确定]
为 vsftpd 启动 vsftpd: [确定]
### CentOS 7.x
[iyunv@dba vsftpd]# systemctl start vsftpd
[iyunv@dba vsftpd]# netstat -lntp | grep 21
tcp 0 0 0.0.0.0:21 0.0.0.0:* LISTEN 2951/vsftpd
重启之后,使用FileZilla客户端连接vsFTPd服务器,可以正常上传下载文件,么么哒。。。
2、基于pam_mysql实现匿名用户认证:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
[iyunv@dba pam.d]# uname -a
Linux dba.com 2.6.32-431.el6.x86_64 #1 SMP Fri Nov 22 03:15:09 UTC 2013 x86_64 x86_64 x86_64 GNU/Linux
# 2.1安装MySQL数据库,并配置vsftpd登录的虚拟用户名和密码:
[iyunv@dba pam.d]# yum -y install mysql-server
[iyunv@dba pam.d]# yum grouplist | grep -i "develop"
Additional Development
Desktop Platform Development
Development tools
Server Platform Development
[iyunv@dba ~]# mysql -u root -p123456
mysql> create database vsftpd;
mysql> use vsftpd;
mysql> create table users (
-> id int AUTO_INCREMENT NOT NULL,
-> name char(16) binary NOT NULL,
-> passwd char(48) binary NOT NULL,
-> primary key(id)
-> );
mysql> create table logs (msg varchar(255),
-> user char(16),
-> pid int,
-> host char(32),
-> rhost char(32),
-> logtime timestamp
-> );
mysql> desc users;
+--------+----------+------+-----+---------+----------------+
| Field | Type | Null | Key | Default | Extra |
+--------+----------+------+-----+---------+----------------+
| id | int(11) | NO | PRI | NULL | auto_increment |
| name | char(16) | NO | | NULL | |
| passwd | char(48) | NO | | NULL | |
+--------+----------+------+-----+---------+----------------+
3 rows in set (0.03 sec)
mysql> insert into users(name, passwd) values ('test', password('test'));
Query OK, 1 row affected (0.08 sec)
mysql> select * from users;
+----+------+-------------------------------------------+
| id | name | passwd |
+----+------+-------------------------------------------+
| 1 | test | *94BDCEBE19083CE2A1F959FD02F964C7AF4CFC29 |
+----+------+-------------------------------------------+
1 row in set (0.00 sec)
# 2.2下载安装pam_mysql插件:
[iyunv@dba ~]# yum install openssl-devel
[iyunv@dba ~]# yum install mysql-devel
[iyunv@dba ~]# wget
1
2
3
4
5
6
7
8
[iyunv@dba ~]# tar -zxf pam_mysql-0.7RC1.tar.gz
[iyunv@dba ~]# cd pam_mysql-0.7RC1
[iyunv@dba ~]# ./configure --with-mysql=/usr --with-openssl=/usr --with-pam-mods-dir=/lib64/security
[iyunv@dba ~]# make && make install
[iyunv@dba ~]# ll /lib64/security/pam_mysql.*
-rwxr-xr-x 1 root root 873 Aug 19 12:27 /lib64/security/pam_mysql.la
-rwxr-xr-x 1 root root 133356 Aug 19 12:27 /lib64/security/pam_mysql.so
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
# 2.3修改配置vsftpd.conf and /etc/pam.d/vsftpd.mysql
#pam_service_name=vsftpd
pam_service_name=vsftpd.mysql
[iyunv@dba pam.d]# more vsftpd.mysql
auth required /lib64/security/pam_mysql.so db=vsftpd user=root passwd=123456 host=localhost table=users usercolumn=name passwdcolumn=passwd crypt=2
account required /lib64/security/pam_mysql.so db=vsftpd user=root passwd=123456 host=localhost table=users usercolumn=name passwdcolumn=passwd crypt=2
### crypt参数介绍:
[iyunv@dba pam_mysql-0.7RC1]# less README
crypt (plain)
The method to encrypt the user's password:
0 (or "plain") = No encryption. Passwords stored in plaintext.
HIGHLY DISCOURAGED.
1 (or "Y") = Use crypt(3) function.
2 (or "mysql") = Use MySQL PASSWORD() function. It is possible
that the encryption function used by PAM-MySQL
is different from that of the MySQL server, as
PAM-MySQL uses the function defined in MySQL's
C-client API instead of using PASSWORD() SQL function
in the query.
3 (or "md5") = Use plain hex MD5.
4 (or "sha1") = Use plain hex SHA1.
[iyunv@dba pam.d]# /etc/init.d/vsftpd restart
Shutting down vsftpd: [ OK ]
Starting vsftpd for vsftpd: [ OK ]
以上,vsftpd服务器基于mysql进行虚拟用户认证的环境已搭建完毕,下一步开始测试啦。。。
GG,上传文件再次报错。。。
响应: 550 Permission denied.
错误: 严重文件传输错误
报错与之前的一致,可能是权限配置的问题吧,呵呵
"test"账号登录成功后,是映射成ftp用户对文件和目录进行操作的,所以进入了/var/ftp/目录,可以查看配置文件和ftp用户确定。
guest_enable=YES
guest_username=ftp
user_config_dir=/etc/vsftpd/user_conf
[iyunv@dba vsftpd]# cat /etc/passwd | grep ftp
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
[iyunv@dba vsftpd]# cd /var/ftp/
[iyunv@dba ftp]# ls -ld ../* | grep ftp
drwxr-xr-x 3 root root 4096 Aug 18 22:53 ../ftp
You have new mail in /var/spool/mail/root
[iyunv@dba ftp]# ls -ld *
drwxr-xr-x 2 root root 4096 Mar 22 20:14 pub
可以看到上面的ftp与upload目录,ftp用户都是没有写权限的,可以修改方式就是修改upload目录的权限后重启vsftpd就可以上传,Ok,那就开始干!
[iyunv@dba ftp]# chown ftp pub
[iyunv@dba ftp]# ll
total 4
drwxr-xr-x 2 ftp root 4096 Mar 22 20:14 pub
这样修改之后应该就没问题了吧,哈哈哈,结果一测试果然又GG了。。。所以逐步进行排查:
1、目录权限设置肯定没问题了
2、那估计就是vsftpd配置文件读写权限设置的问题了吧,因为我的ftp用户是存在单独目录下与用户同名的配置文件中的,故新建名为test的配置文件,配置贴上来:
[iyunv@dba user_conf]# pwd
/etc/vsftpd/user_conf
You have new mail in /var/spool/mail/root
[iyunv@dba user_conf]# more test
local_root=/var/ftp
write_enable=YES
anon_umask=022
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
Ok,重启vsftpd测试,终于可以上传文件了,搞定。。。
运维网声明
1、欢迎大家加入本站运维交流群:群②:261659950 群⑤:202807635 群⑦870801961 群⑧679858003
2、本站所有主题由该帖子作者发表,该帖子作者与运维网 享有帖子相关版权
3、所有作品的著作权均归原作者享有,请您和我们一样尊重他人的著作权等合法权益。如果您对作品感到满意,请购买正版
4、禁止制作、复制、发布和传播具有反动、淫秽、色情、暴力、凶杀等内容的信息,一经发现立即删除。若您因此触犯法律,一切后果自负,我们对此不承担任何责任
5、所有资源均系网友上传或者通过网络收集,我们仅提供一个展示、介绍、观摩学习的平台,我们不对其内容的准确性、可靠性、正当性、安全性、合法性等负责,亦不承担任何法律责任
6、所有作品仅供您个人学习、研究或欣赏,不得用于商业或者其他用途,否则,一切后果均由您自己承担,我们对此不承担任何法律责任
7、如涉及侵犯版权等问题,请您及时通知我们,我们将立即采取措施予以解决
8、联系人Email:admin@iyunv.com 网址:www.yunweiku.com