vsftpd+pam+mysql

來源:互聯網
上載者:User

標籤:security   資料庫   mysql   程式   開發   

安裝vsftpd+pam+mysql來實現對虛擬使用者身份認證功能

一、安裝所需要程式

1、事先安裝好開發環境和mysql資料庫;

# yum -y install mysql-server mysql-devel
# yum -y groupinstall "Development Tools" "DevelopmentLibraries"


2.安裝pam_mysql-0.7RC1

# tar zxvf  pam_mysql-0.7RC1.tar.gz
# cd  pam_mysql-0.7RC1
# ./configure --with-mysql=/usr --with-openssl
# make
# make install

 

3.安裝vsftpd

# yum -y install vsftpd
#cp /usr/lib/security/pam_mysql.so  /lib/security/

#這個共用庫在後面的pam檔案中要用到,請務必要確保它的路徑的正確性

 

二、建立虛擬使用者帳號

1.準備資料庫及相關表

首先請確保mysql服務已經正常啟動。而後,按需要建立儲存虛擬使用者的資料庫即可,這裡將其建立為vsftpd資料庫。

mysql> create database vsftpd;

mysql> grant select on vsftpd.* to [email protected] by ‘vsftpd’;
mysql> grant select on vsftpd.* to [email protected] identified by ‘vsftpd’;
mysql> flush privileges;

#這裡使用者vsftpd用處在於將來pam到mysql資料庫中檢索時mysql這個進程的屬主以vsftpd來運行

 

mysql> use vsftpd;
mysql> create table users (
    -> id int AUTO_INCREMENT NOT NULL,
    -> name char(20) binary NOT NULL,
    -> password char(48) binary NOT NULL,
    -> primary key(id)
    -> );

 

2、添加測試的虛擬使用者

mysql> insert into users(name,password) values(‘tom‘,passwd(‘xsl’);

mysql> insert into users(name,password) values(‘boy‘, passwd(‘xsl’);

 

 

三、配置vsftpd

1.建立pam認證所需檔案

#vi /etc/pam.d/vsftpd.mysql
添加如下兩行
auth required /lib/security/pam_mysql.so user=vsftpd passwd=vsftpd host=localhostdb=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2

account required /lib/security/pam_mysql.so user=vsftpdpasswd=vsftpd host=localhost db=vsftpd table=users usercolumn=namepasswdcolumn=password crypt=2 

2.修改vsftpd的設定檔,使其適應mysql認證

建立虛擬使用者映射的系統使用者及對應的目錄
#useradd -s /sbin/nologin vuser
#chmod go+rx /home/vuser

請確保/etc/vsftpd.conf中已經啟用了以下選項
anonymous_enable=YES
local_enable=YES
write_enable=YES
anon_upload_enable=NO
anon_mkdir_write_enable=NO
chroot_local_user=YES

而後添加以下選項
guest_enable=YES
guest_username=vuser

並確保pam_service_name選項的值如下所示
pam_service_name=vsftpd.mysql

#這個檔案名稱一定要與上面所建立的pam檔案名稱相同

 

四、啟動vsftpd服務

# service vsftpd start
# chkconfig vsftpd on

 

使用虛擬使用者登入,驗正配置結果,以下為原生命令方式測試,你也可以在其它Win Box上用IE或者FTP用戶端工具登入驗正
# ftp localhost

如果使用命令方式登入時,如果出現

[[email protected] ~]# ftp localhost

Connected to localhost

220 (vsFTPd 2.0.5)
530 Please login with USER and PASS.
530 Please login with USER and PASS.
KERBEROS_V4 rejected as an authentication type
Name (192.168.0.104:root): tom
331 Please specify the password.
Password:
530 Login incorrect.
Login failed.
ftp>

這樣你的字樣的話,請查看日誌資訊,使用tail  /var/log/secure命令

Oct 19 15:41:42 localhost vsftpd: pam_mysql - MySQL error(Can‘t connect to local MySQL server through socket ‘/var/lib/mysql/mysql.sock‘(2))
Oct 19 15:43:02 localhost vsftpd: pam_mysql - MySQL error (Can‘t connect tolocal MySQL server through socket ‘/var/lib/mysql/mysql.sock‘ (2))

如果出現這樣的字樣,說明pam-mysql模組無法通過mysql的通訊端域mysql建立串連。

解決辦法:

將認證模組中的host=localhost,改為host=127.0.0.1


[[email protected] pub]# ftp 127.0.0.1

Connected to 127.0.0.1 (127.0.0.1).

220 (vsFTPd 2.2.2)

Name (127.0.0.1:root): tom

331 Please specify the password.

Password:

500 OOPS: cannot change directory:/home/vuser

解決辦法:將selinux 設定為關閉,重啟伺服器即可。

 

 

五、配置虛擬使用者具有不同的存取權限

vsftpd可以在設定檔目錄中為每個使用者提供單獨的設定檔以定義其ftp服務存取權限,每個虛擬使用者的設定檔名同虛擬使用者的使用者名稱。設定檔目錄可以是任意未使用目錄,只需要在vsftpd.conf指定其路徑及名稱即可。

1、配置vsftpd為虛擬使用者使用設定檔目錄

# vim vsftpd.conf
添加如下選項
user_config_dir=/etc/vsftpd/vusers 

2、建立所需要目錄,並為虛擬使用者提供設定檔

# mkdir /etc/vsftpd/vusers/
# cd /etc/vsftpd/vusers/
# touch tom boy

 

3、配置虛擬使用者的存取權限

虛擬使用者對vsftpd服務的存取權限是通過匿名使用者的相關指令進行的。比如,如果需要讓tom使用者具有上傳檔案的許可權,可以修改/etc/vsftpd/vusers/tom檔案,在裡面添加如下選項即可。

#上傳許可權

anon_upload_enable=YES

#建立檔案許可權

anon_mkdir_write_enable=YES

#刪除檔案許可權

anon_other_write_enable=YES

 這樣就可以指定虛擬使用者的存取權限了


vsftpd+pam+mysql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.