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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章