歡迎您光臨本站 註冊首頁

vsftp的虛擬用戶配置

←手機掃碼閱讀     火星人 @ 2014-03-04 , reply:0

vsftp的虛擬用戶配置

先查看vsftp是否支持pam模塊
?
# ldd /usr/sbin/vsftpd
libpam.so.0 => /lib/libpam.so.0 (0x009e3000)     # 有這個輸出就對了

添加虛擬賬戶映射的本地賬戶(因為 虛擬賬戶要操作本地的文件,所使用的許可權就是本地賬戶的許可權,所以要把虛擬賬戶映射成本地賬戶)
?
# useradd -s /sbin/nologin  vuser

下面是vsftpd.conf的配置文件
?
anonymous_enable=NO
local_enable=YES  #因為要映射成本地賬戶,所以這裡必須為yes
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_file=/var/log/vsftpd.log
xferlog_std_format=YES
chroot_local_user=YES  #把本地賬戶限制在設定的目錄中
ls_recurse_enable=NO  #禁止 ls -R 遞歸顯示子目錄
listen=YES
pam_service_name=vsftpd  #修改/etc/pam.d/vsftpd
userlist_enable=YES
tcp_wrappers=YES
user_config_dir=/etc/vsftpd/vuser_conf  #虛擬賬戶的配置目錄,在此目錄可以單獨對每個虛擬賬戶做特殊設定,會覆蓋vsftpd.conf的配置
virtual_use_local_privs=YES  #讓虛擬賬戶擁有本地映射賬戶的許可權

下面是虛擬賬戶的配置文件(在/etc/vsftpd/vuser_conf)
?
#cat /etc/vsftpd/vuser_conf/web_user

guest_enable=YES  #啟用虛擬賬戶支持
guest_username=vuser  #映射的本地賬戶名稱
local_root=/var/ftp  #此虛擬賬戶對應的ftp根目錄,可以對每個虛擬賬戶設定不同的ftp根目錄
write_enable=yes  #此設定是下面幾個設定的基礎
anon_world_readable_only=YES  #只允許下載文件
anon_upload_enable=YES    #可以上傳文件
anon_mkdir_write_enable=YES  #可以建立目錄,但不能改名和刪除
anon_other_write_enable=YES  #可以改名和刪除



ps: 詳細說明可以 man 5 vsftpd.conf

看看這個選項的說明:

anon_upload_enable
              If  set  to  YES, anonymous users will be permitted to upload files under certain           conditions. For this to work, the option
              write_enable must be activated, and the anonymous ftp user must have write permission on desired  upload  locations.  This
              setting is also required for virtual users to upload; by default, virtual users are treated with anonymous (i.e. maximally
              restricted) privilege.

              Default: NO




修改/etc/pam.d/vsftpd
?
#%PAM-1.0
#session    optional     pam_keyinit.so    force revoke
#auth       required    pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
#auth       required    pam_shells.so
#auth       include password-auth
#account    include password-auth
#session    required     pam_loginuid.so
#session    include password-auth
auth    sufficient  /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
account sufficient  /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login

db=/etc/vsftpd/vsftpd_login 這個文件就是我們稍候建立的虛擬賬戶文件

添加虛擬賬戶  

文件格式是:一行用戶名,一行密碼
?
# cat vsftpd_login.txt
web_user
123456
web_test
654321

還要處理成pam識別的Berkeley DB文件格式
?
db_load -T -t hash -f vsftpd_login.txt vsftpd_login.db

db_load 由 db4-utils 軟體包提供.

這裡注意生成的文件名是 vsftpd_login.db,我們寫到/etc/pam.d/vsftpd里是不寫後綴名的!

下面是我們建立的vsftpd的配置文件清單
?
# ll /etc/vsftpd/
總用量 40
-rw------- 1 root root   125  3月  4 2011 ftpusers
-rw------- 1 root root   361  3月  4 2011 user_list
-rw------- 1 root root  4708  9月  7 00:01 vsftpd.conf
-rwxr--r-- 1 root root   338  3月  4 2011 vsftpd_conf_migrate.sh
-rw-r--r-- 1 root root 12288  9月  6 23:11 vsftpd_login.db
-rw-r--r-- 1 root root    26  9月  6 23:10 vsftpd_login.txt
drwxr-xr-x 2 root root  4096  9月  7 00:20 vuser_conf

注意事項:

1. 關閉selinux,修改後要重啟系統.

cat /etc/sysconfig/selinux
?
# This file controls the state of SELinux on the system.
# SELINUX= can take one of these three values:
#     enforcing - SELinux security policy is enforced.
#     permissive - SELinux prints warnings instead of enforcing.
#     disabled - No SELinux policy is loaded.
SELINUX=disabled
# SELINUXTYPE= can take one of these two values:
#     targeted - Targeted processes are protected,
#     mls - Multi Level Security protection.
SELINUXTYPE=targeted

2. vuser_conf目錄下的虛擬用戶配置文件,要以用戶命名.即你的vsftpd_login.txt裡面有xiaoming這個賬戶,那麼vuser_conf目錄下就要有xiaoming這個文件

3. 你的本地映射賬戶是vuser,那麼你建立這個賬戶的時候不能用 useradd -M  這樣/home下沒有vuser的家目錄,也是無法登陸的

4. 虛擬用戶的許可權跟vuser的許可權一樣,所以你對虛擬用戶設置的local_root=/dir 那麼vuser用戶也要對這個/dir目錄有相應的許可權才可以

5. 記得看下伺服器的cpu架構,用arch命令
?
x86_64

如果系統是64位的,那麼/etc/pam.d/vsftpd 這個文件也要修改
?
#%PAM-1.0
#session    optional     pam_keyinit.so    force revoke
#auth       required    pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
#auth       required    pam_shells.so
#auth       include     password-auth
#account    include     password-auth
#session    required     pam_loginuid.so
#session    include     password-auth
auth    sufficient  /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login
account sufficient  /lib64/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login

補充:

怎麼樣讓我們的網站文件目錄許可權更安全呢,要堅決杜絕777的許可權設置,下面說下我的方法

讓web server(apache/nginx)和php以nobody的身份運行,配置vsftp以虛擬用戶vuser

然後設置網站目錄和文件的屬主屬組為vuser:nobody,目錄許可權為750,普通文件為640.web server和php只對文件有讀的許可權。

還漏下一點,還要修改vuser的屬組,不然虛擬用戶上傳的文件和目錄屬主和屬組都是vuser,不符合我們的要求

usermod -g nobody vuser

如果按照上面vsftp的設置那麼虛擬用戶上傳的文件和目錄許可權是這樣的

-rw-rw-r-- 1 vuser nobody   2894  2月 24 19:48 1.txt

drwxrwxr-x 2 vuser nobody   4096  2月 24 19:49 aa

修改vsftpd.conf

local_umask=027

這樣就符合要求了

drwxr-x--- 2 vuser nobody   4096  2月 24 20:04 dd
-rw-r----- 1 vuser nobody   3634  2月 24 19:56 2.txt

ok.

不對的地方歡迎指正

http://www.cnblogs.com/txwsqk/

[火星人 ] vsftp的虛擬用戶配置已經有704次圍觀

http://coctec.com/docs/service/show-post-12946.html