vsftpd 虚拟用户实现登陆ftp

配置目的

vsftpd 程序为ftp服务的实现方式之一,vsftpd 程序免费,而且安全性及传输速度都很高。
vsftpd 提供虚拟用户验证方式,安全性很好。
虚拟用户采用已定义好的虚拟账户及密码作为登陆ftp 的用户名和密码,该虚拟用户非服务器本地用户,但会映射到一个服务器本地用户。
虚拟用户可以定义在单独的db文件中,也可以通过数据库软件提供用户名和密码的存储。
本文采用定义的db 文件做讲解。

配置环境

centos 6
vsftpd 2.2.2

配置文件及相关文件

/etc/vsftpd
/etc/vsftpd/vsftpd.conf
/etc/vsftpd/vusers.db
/etc/vsftpd/vusers
/etc/pam.d/vsftpd.vm

配置过程

安装vsftpd

安装部分本文不再赘述。

创建虚拟用户

准备两个虚拟账户admin,share

vim /etc/vsftpd/vusers
echo "amdin" > /etc/vsftpd/vusers
echo "amdin" > /etc/vsftpd/vusers
echo "share" > /etc/vsftpd/vusers
echo "share" > /etc/vsftpd/vusers

一般从规则上来讲,/etc/vsftpd/vusers中的奇数行为虚拟用户名,偶数行为对应的密码。

执行如下命令将明文文件转为db文件,提高安全性。

db_load -T -t hash -f /etc/vsftpd/vusers /etc/vsftpd/vusers.db

创建映射的本地用户

该用户只用于ftp,不需要登陆shell。

useradd -d /mshare -s /sbin/nologin virtual
chmod 755 /mshare

配置pam 虚拟用户验证文件

pam 为用户验证模块,可方便插入服务程序提供不同的验证方式。

cd /etc/pam.d
cat vsftpd.vm
auth required pam_userdb.so db=/etc/vsftpd/vusers
account required pam_userdb.so db=/etc/vsftpd/vusers

vsftpd.vm文件需要自行定义。

配置ftp 文件

anonymous_enable=no
local_enable=yes
write_enable=yes

guest_enable=YES

guest_username=virtual


anon_other_write_enable=no
anon_upload_enable=no
anon_mkdir_write_enable=no
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_file=/var/log/xferlog
xferlog_std_format=YES
chroot_local_user=no
chroot_list=enable=yes
chroot_list_file=/etc/vsftpd/chroot_list
ls_recurse_enable=no
listen=YES

pam_service_name=/etc/pam.d/vsftpd.vm
tcp_wrappers=YES
user_config_dir=/etc/vsftpd/vusers

配置虚拟用户权限

可单独对虚拟用户的权限进行设置,如下以admin 虚拟用户为例

/etc/vsftpd/vusers
[root@linuxde vusers]# cat admin 
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_umask=022
local_root/mshare/admin

/etc/vsftpd/vusers/* 下的文件需要自己定义,文件里设置虚拟用户权限。
该虚拟用户权限应对应使用anon_*的参数。
anon_umask 参数确定虚拟用户上传资源后的权限,该最综权限等于777-value of anon_umask。

配置解决其他因素

关闭防火墙避免不能访问21端口

service iptables stop

暂时禁用selinux 或者打开selinux 对ftp 程序的限制

setenforce 0

or

getsebool -a | grep ftp
allow_ftpd_anon_write --> off
allow_ftpd_full_access --> on
allow_ftpd_use_cifs --> off
allow_ftpd_use_nfs --> off
ftp_home_dir --> off
ftpd_connect_db --> off
ftpd_use_fusefs --> off
ftpd_use_passive_mode --> off
httpd_enable_ftp_server --> off
tftp_anon_write --> off
tftp_use_cifs --> off
tftp_use_nfs --> off

确保allow_ftpd_full_access 为on, 否则执行

setsebool -P ftpd_full_access=on

验证测试

命令行测试

[root@linuxde ~]# ftp 172.16.34.130
Connected to 172.16.34.130 (172.16.34.130).
220 (vsFTPd 2.2.2)
Name (172.16.34.130:root): admin
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (172,16,34,130,40,76).
150 Here comes the directory listing.
drwxr-xr-x    2 2001     2001         4096 Feb 09 07:39 134
226 Directory send OK.
ftp> cd 134
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (172,16,34,130,44,164).
150 Here comes the directory listing.
226 Directory send OK.
ftp> put file1
local: file1 remote: file1
227 Entering Passive Mode (172,16,34,130,107,40).
150 Ok to send data.
226 Transfer complete.
462 bytes sent in 0.000399 secs (1157.89 Kbytes/sec)
ftp> ls
227 Entering Passive Mode (172,16,34,130,194,149).
150 Here comes the directory listing.
-rw-r--r--    1 2001     2001          462 Feb 09 07:52 file1
226 Directory send OK.
ftp> pwd
257 "/134"
ftp> ls
227 Entering Passive Mode (172,16,34,130,40,146).
150 Here comes the directory listing.
-rw-r--r--    1 2001     2001          462 Feb 09 07:52 file1
226 Directory send OK.
ftp> mkdir 156
257 "/134/156" created
ftp> ls
227 Entering Passive Mode (172,16,34,130,189,21).
150 Here comes the directory listing.
drwxr-xr-x    2 2001     2001         4096 Feb 09 07:53 156
-rw-r--r--    1 2001     2001          462 Feb 09 07:52 file1
226 Directory send OK.
ftp> pwd
257 "/134"
ftp> ls
227 Entering Passive Mode (172,16,34,130,78,253).
150 Here comes the directory listing.
drwxr-xr-x    2 2001     2001         4096 Feb 09 07:53 156
-rw-r--r--    1 2001     2001          462 Feb 09 07:52 file1
226 Directory send OK.
ftp> cd ..
250 Directory successfully changed.
ftp> cd ..
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (172,16,34,130,229,75).
150 Here comes the directory listing.
drwxr-xr-x    3 2001     2001         4096 Feb 09 07:53 134
226 Directory send OK.
ftp> cd ..
250 Directory successfully changed.
ftp> pwd
257 "/"
ftp> ls
227 Entering Passive Mode (172,16,34,130,25,90).
150 Here comes the directory listing.
drwxr-xr-x    3 2001     2001         4096 Feb 09 07:53 134
226 Directory send OK.

本地用户模式及匿名模式验证因不安全,企业中很少用,不再说明。

限制admin 用户只在个人目录下,
/etc/vsftpd/vsftpd.conf

chroot_local_user=no
chroot_list_enable=yes
chroot_list_list=/etc/vsftpd/chroot_list

/etc/vsftpd/vusers/admin

...
local_root=/mshare/admin
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章