1. 程式人生 > >CentOS 6.3 下 vsftpd 匿名使用者訪問配置

CentOS 6.3 下 vsftpd 匿名使用者訪問配置

http://www.jianshu.com/p/f90e0f81134b#

背景:
CentOS 6.3
vsftpd.x86_64 0:2.2.2-13.el6_6.1
從牆角找了一臺能用的舊機器,Pentium D & 1G RAM & 160G HDD,費了一番功夫終於裝上了 CentOS 6.3,才發現物理機安裝跟虛擬機器安裝的過程還是有點差別滴,之前在虛擬機器上配置過 vsftpd,今天在這臺機器上也裝一個試試看.

安裝 vsftpd

yum install vsftpd

/etc/vsftpd/vsftpd.conf 預設設定

[[email protected] ~]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES //匿名訪問 開啟 local_enable=YES //本地實體使用者訪問 開啟 write_enable=YES //允許使用者上傳資料 local_umask=022 //建立新目錄和檔案的許可權 dirmessage_enable=YES //目錄下有 .message 檔案則顯示該檔案的內容 ??? xferlog_enable=YES //日誌檔案記錄 記錄於 /var/log/vferlog connect_from_port_20=YES //支援主動式連線功能 xferlog_std_format=YES //支援 WuFTP 的日誌檔案格式
listen=YES //使用 stand along 方式啟動 vsftpd pam_service_name=vsftpd //支援 PAM 模組的管理 userlist_enable=YES //支援 /etc/vsftpd/user_list 檔案內的登陸賬號控制 tcp_wrappers=YES //支援 TCP Wrappers 的防火牆機制

seriously,有一些選項的意義和作用我也不是很懂,註釋是對著鳥哥伺服器篇敲下來的,OK,下面新增一個選項:

use_localtime=YES    //使用本地時間,vsftpd預設使用GMT格林尼治時間

匿名登入與下載

匿名登入的配置比較簡單,so先搞下這個吧miao~
vsftpd預設的匿名使用者的主目錄是 ftp 使用者的主目錄,試試用 finger ftp

看一下在哪裡咧

[root@DX3906c ~]# finger ftp
-bash: finger: command not found

啊咧,沒裝...好吧,yum install finger之後再看一下

[root@DX3906c ~]# finger ftp
Login: ftp                              Name: FTP User
Directory: /var/ftp    //ftp使用者的主目錄                 Shell: /sbin/nologin //賬戶不能用於登入系統
Never logged in.
No mail.
No Plan.

可以看到ftp使用者的主目錄在 /var/ftp,那也就是說匿名使用者登陸之後的看到的就是這個啦,想讓人家去你的ftp下載東西的話,檔案要放到這裡.那麼我有一個問題,這個目錄可以隨便改嗎?待會配置好了匿名使用者登入再改改試試看

現在開始配置匿名使用者的登入

[[email protected] ~]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES    //匿名訪問 開啟
local_enable=YES    //本地實體使用者訪問 開啟
write_enable=YES    //允許使用者上傳資料
local_umask=022    //建立新目錄和檔案的許可權
dirmessage_enable=YES    //目錄下有 .message 檔案則顯示該檔案的內容 ???    xferlog_enable=YES    //日誌檔案記錄 記錄於 /var/log/vferlog
connect_from_port_20=YES  //支援主動式連線功能
xferlog_std_format=YES    //支援 WuFTP 的日誌檔案格式

在後面新增:

no_anon_password=YES    //匿名登入時,不檢驗密碼
idle_session_timeout=600    //匿名使用者10分鐘無操作則掉線
banner_file=/etc/vsftpd/anon_welcome.txt    //匿名使用者登入後看到的歡迎資訊

然後需要編輯一下 anon_welcome.txt 檔案,不然 ftp 會無法正常連線
配置完之後重啟一下 vsftpd 服務 service vsftpd restart
在本機先測試一下能不能連線成功吧 使用 ftp localhost

[[email protected] ftp]# ftp localhost
Trying ::1...
ftp: connect to address ::1Connection refused
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220-Welcome to Zion's FTP
220-My E-mail:[email protected]
220
Name (localhost:root): anonymous
500 OOPS: cannot change directory:/home/ftp
Login failed.

可以看到歡迎資訊已經顯示出來了,但是匿名使用者登入失敗了,這是為什麼呢?百度了一下,發現是 selinux 引起的,檢視一下當前系統的 selinux 狀態: /usr/sbin/sestatus -v

[[email protected] ftp]# /usr/sbin/sestatus -v
SELinux status:                 enabled
SELinuxfs mount:                /selinux
Current mode:                   enforcing
Mode from config file:          enforcing
Policy version:                 24
Policy from config file:        targeted

臨時關閉 selinux setenforce 0
再看一下 selinux 狀態 getenforce,已經是 Permissive 了,再登入下 ftp 試試

[[email protected] ftp]# ftp localhost
Trying ::1...
ftp: connect to address ::1Connection refused
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220-Welcome to Zion's FTP
220-My E-mail:[email protected]
220
Name (localhost:root): anonymous
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.

OK,登陸成功了,聽說不關閉 selinux 的話,也可以修改 selinux 的一些配置來處理這個問題,這個就容後再議吧,現在selinux只是臨時關閉,永久關閉需要修改 /etc/selinux/config 檔案,將SELINUX=enforcing改為SELINUX=disabled,然後重啟即可.

然後來試試在遠端計算機上能不能登入,之前在虛擬機器上安裝的時候發現本機能登,但是遠端登不上,後來發現是防火牆的問題,iptables的配置我還不太清楚,所以就抄近路直接在GUI下打開了FTP的21埠,現在也如法炮製好了.我使用Chrome登入然後Chrome直接崩潰了miao~,但是Windows資源管理器可以成功匿名登入,並且可以下載檔案,那說明還是比較成功滴.

番外篇

下面研究一下剛才的問題,ftp 的預設目錄可以改嗎?來試試看.

[root@DX3906c ~]# vim /etc/passwd
......
ftp:x:14:50:FTP User:/var/ftp:/sbin/nologin
......

改成

ftp:x:14:50:FTP User:/home/ftp:/sbin/nologin

finger一下看看已經改了,

[root@DX3906c ~]# finger ftp
Login: ftp                              Name: FTP User    
Directory: /home/ftp                    Shell: /sbin/nologin

再重啟一下 vsftd 服務 service vsftpd restart
現在在 /home/ftp 下建一個檔案看看

[root@DX3906c ~]# touch /home/ftp/ Hello_FTP.txt

登入ftp

[[email protected] ftp]# ftp localhost
Trying ::1...
ftp: connect to address ::1Connection refused
Trying 127.0.0.1...
Connected to localhost (127.0.0.1).
220-Welcome to Zion's FTP
220-My E-mail:[email protected]
220
Name (localhost:root): anonymous
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> dir    //列出當前目錄下的檔案,ls也可以
227 Entering Passive Mode (127,0,0,1,145,180).
150 Here comes the directory listing.
-rw-r--r--    1 0        0               0 Dec 09 19:09 Hello_FTP.txt
226 Directory send OK.

列出了 Hello_FTP.txt,說明目錄修改成功啦.

匿名使用者上傳許可權的配置

在 /etc/vsftpd/vsftpd.conf 中新增以下內容:

anon_mkdir_write_enable=YES    //匿名使用者建立目錄的許可權
anon_upload_enable=YES    //匿名使用者上傳檔案的許可權
#anon_other_write_enable=YES    //匿名使用者刪除和重新命名檔案的許可權,還是關掉比較好miao~

老規矩,重啟一下 vsftpd 服務 /etc/init.d/vsftpd restart
登陸看看能不能上傳檔案咧

touch upload_test.txt

登入到 ftp 之後使用 put命令上傳檔案

ftp> put upload_test.txt
local: upload_test.txt remote: upload_test.txt
227 Entering Passive Mode (127,0,0,1,93,233).
553 Could not create file.

提示 553 Could not create file.似乎是許可權問題 查查 vsftpd.conf 的 manual

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.

anon_mkdir_write_enable
If set to YES, anonymous users will be permitted to create new directories under certain conditions. For this to work, the
option write_enable must be activated, and the anonymous ftp user must have write permission on the parent directory.

看一下ftp目錄的許可權:

[[email protected] ~]# ll /home/
total 24
drwxr-xr-x.  3 root root  4096 Dec  9 20:26 ftp

屬主是root,ftp沒有寫許可權,怪不得不能上傳,改一下屬主再上傳下試試:

[[email protected] ~]# chown ftp /home/ftp
......
Name (localhost:root): anonymous
500 OOPS: vsftpd: refusing to run with writable anonymous root
Login failed.

這下登都不上去了...錯誤提示似乎是說匿名使用者的根目錄不能是可寫的,那再改下許可權試試:

[[email protected] home]# mkdir ./ftp/uploads/
[[email protected] home]# chown ftp ./ftp/uploads
[[email protected] home]# chown root ./ftp
[[email protected] home]# /etc/init.d/vsftpd restart
......
ftp> cd uploads
250 Directory successfully changed.
ftp> put upload_test.txt
local: upload_test.txt remote: upload_test.txt
227 Entering Passive Mode (127,0,0,1,193,252).
150 Ok to send data.
226 Transfer complete.

可以上傳了,試試遠端也同樣可以上傳檔案,不錯不錯.

匿名使用者不可下載匿名使用者上傳的檔案

這個功能的描述比較繞口啊,其實就是用在比如:[學生交作業(只可以上交不可以看別人的作業)/上傳資源需要稽核]等場景,具體的實現原理就是匿名使用者上傳檔案之後將檔案的屬主改為其他使用者,那麼以匿名使用者繼承的ftp使用者的許可權就不能下載這些檔案了.這個地方我還不是很理解,先放一下吧,等到搞定了實體賬號的配置之後再回過頭來搞這個吧乜乜達...



文/Zi0n42(簡書作者)
原文連結:http://www.jianshu.com/p/f90e0f81134b
著作權歸作者所有,轉載請聯絡作者獲得授權,並標註“簡書作者”。