vsftpd server

配置vsftpd

----------------------------------------------------------------------------------------------------

vsftpd的配置文件:

用戶認證配置文件:/etc/pam.d/vsftpd

服務腳本:/etc/rc.d/init.d/vsftpd

配置文件目錄:/etc/vsftpd/

/etc/vsftpd/vsftpd.conf    ###主配置文件

/etc/vsftpd/ftpusers    ###不能訪問FTP的用戶列表

ftpusers列表內的用戶不能訪問FTP:這是vsftpd服務在啓動後已經決定的,只要在這個列表裏面用戶都不能訪問FTP,沒有其它的開關可以控制

/etc/vsftpd/user_list    ###不能訪問FTP的用戶列表

user_list列表內的用戶能否訪問FTP,取決於主配置文件vsftpd.conf內定義的兩個開關

userlist_enable={YES|NO}

userlist_deny={YES|NO}



匿名用戶(映射爲ftp用戶)共享資源位置:/var/ftp

系統用戶通過ftp訪問的資源的位置:用戶自己的家目錄

虛擬用戶通過ftp訪問的資源的位置:給虛擬用戶指定的映射成爲的系統用戶的家目錄



匿名用戶的配置:

anonymous_enable=YES


anon_upload_enable=YES    ###是否允許匿名用戶上傳文件

anon_mkdir_write_enable=YES    ###是否允許匿名用戶創建目錄

anon_ohter_write_enable=YES    ###是否允許匿名用戶刪除文件



系統用戶的配置:

local_enable=YES


write_enable=YES    ###是否允許本地用戶具有寫權限

local_umask=022    ###控制本地用戶上傳文件後的默認權限


禁錮所有的ftp本地用戶於其家目錄中:

chroot_local_user=YES


禁錮文件中指定的ftp本地用戶於其家目錄中:

chroot_list_enable=YES

chroot_list_file=/etc/vsftpd/chroot_list



目錄消息(可在某個目錄下創建.massage文件,填入消息,當用戶切換至此目錄時會出現提示)

dirmessage_enable=YES



日誌:

xferlog_enable=YES    ###是否開啓FTP的傳輸日誌

xferlog_std_format=YES    ###傳輸日誌是否使用標準格式

xferlog_file=/var/log/xferlog    ###指定日誌文件路徑



改變上傳文件的屬主:

chown_uploads=YES    ###是否改變上傳文件的屬主

chown_username=whoever    ###指定用戶名



vsftpd使用pam完成用戶認證,指明其用到的pam配置文件:(/etc/pam.d目錄下)

pam_service_name=vsftpd



是否啓用控制用戶登錄的列表文件

userlist_enable=YES

userlist_deny=YES|NO


文件:/etc/vsftpd/user_list


如果userlist_enable=NO,表示無論此表內或表外的本地用戶均能登錄訪問ftp

如果userlist_deny=NO,表示只允許此表內的本地用戶登錄訪問ftp,默認值爲YES



超時相關:

idle_session_timeout=600    ###空閒會話的超時時長

data_connection_timeout=120    ###數據連接的超時時長



連接限制:

max_clients   ###最大併發連接數

max_per_ip   ###每個IP可同時發起的併發請求數

 

 

傳輸速率:

anon_max_rate   ###匿名用戶的最大傳輸速率, 單位是“字節/秒”

local_max_rate   ###本地用戶的最大傳輸速率, 單位是“字節/秒”

 

 

虛擬用戶:

所有的虛擬用戶會被統一映射爲一個指定的系統賬號,訪問的共享位置即爲此係統賬號的家目錄;

 

各虛擬用戶可被賦予不同的訪問權限;

通過匿名用戶的權限控制參數進行指定;

 

虛擬用戶賬號的存儲方式:

1、文件形式:編輯文件

奇數行爲用戶名

偶數行爲密碼

 

此文件需要被編碼爲hash格式;

 

2、關係型數據庫中的表中:

即時查詢數據庫完成用戶認證;

 

mysql庫,

pam要依賴於pam_mysql模塊,需要額外安裝

# yum -y install pam_mysql

 

注意:pam_mysql由epel源提供

 

 

 

vsftpd+pam_mysql+MariaDB

----------------------------------------------------------------------------------------------------

一、在vsftp服務器上,安裝pam_mysql,用於支持數據庫用戶認證

# yum -y install pam_mysql

 

 

二、在MariaDB服務器上,準備數據庫及相關表

 

1、創建存儲虛擬用戶的數據庫:

[root@centos7 mysql]# mysql

Welcome to the MariaDB monitor.  Commands end with ; or \g.

Your MariaDB connection id is 4

Server version: 5.5.46-MariaDB-log MariaDB Server

 

Copyright (c) 2000, 2015, Oracle, MariaDB Corporation Ab andothers.

 

Type 'help;' or '\h' for help. Type '\c' to clear the currentinput statement.

 

MariaDB [(none)]> create database vsftpd;    ###創建一個名爲vsftpd的數據庫

Query OK, 1 row affected (0.00 sec)

 

MariaDB [(none)]> show databases;

+--------------------+

| Database           |

+--------------------+

| information_schema |

| mysql              |

| performance_schema |

| test               |

+--------------------+

 

MariaDB [(none)]> use vsftpd;    ###設定vsftpd爲默認庫

Database changed

 

 

2、創建用於管理數據庫的用戶,並授權

MariaDB [vsftpd]> grant select on vsftpd.* tovsftp@'172.16.2.%' identified by 'magelinux';

 

說明:創建用戶vsftp,密碼爲"magelinux",並授權該用戶對vsftpd這個數據庫遠程訪問權限

 

MariaDB [vsftpd]> flush privileges;

Query OK, 0 rows affected (0.01 sec)

 

說明:刷新權限

 

 

3、在數據庫創建表:

MariaDB [vsftpd]> create table users (

-> id INT UNSIGNED NOT NULL AUTO_INCREMENT PRIMARY KEY,

-> name VARCHAR(50) BINARY NOT NULL,

-> password CHAR(48) BINARY NOT NULL );

Query OK, 0 rows affected (0.06 sec)

 

說明:在該數據庫中創建名爲users的表,建立3個字段:

id(無符號,不能爲空,自動增長,把id定義成主鍵)

name(長度20個字符,區分字符大小寫,不能爲空)

passwd(長度48個字符,區分字符大小寫,不能爲空)

 

MariaDB [vsftpd]> show tables from vsftpd;

+------------------+

| Tables_in_vsftpd |

+------------------+

| users            |

+------------------+

1 row in set (0.00 sec)

 

 

MariaDB [vsftpd]> desc users;

+----------+------------------+------+-----+---------+----------------+

| Field    | Type             | Null | Key | Default |Extra          |

+----------+------------------+------+-----+---------+----------------+

| id       | int(10)unsigned | NO   | PRI | NULL    | auto_increment |

| name     |varchar(50)      | NO   |    | NULL    |                |

| password | char(48)        | NO   |     | NULL   |                |

+----------+------------------+------+-----+---------+----------------+

 

 

4、在表中插入用戶

MariaDB [vsftpd]> insert into users(name,password)values('user1',password('abc12345'));

MariaDB [vsftpd]> insert into users(name,password)values('user2',password('abc12345'));

 

說明:在users表中插入用戶數據,分別爲"user1、user2",密碼爲"abc123,",使用passwod加密

 

MariaDB [vsftpd]> select * from users;

+----+-------+-------------------------------------------+

| id | name  |password                                 |

+----+-------+-------------------------------------------+

|  1 | user1 |*25817BCE9A997710F8A129AF52153A903AB9254F |

|  2 | user2 |*25817BCE9A997710F8A129AF52153A903AB9254F |

+----+-------+-------------------------------------------+

2 rows in set (0.00 sec)

 

 

 

 

三、在vsftpd服務器上連接MariaDB服務器測試:

[root@centos6 ~]# mysql -u vsftp -h 172.16.2.16 -p

Enter password:

Welcome to the MySQL monitor. Commands end with ; or \g.

Your MySQL connection id is 7

Server version: 5.5.46-MariaDB-log MariaDB Server

 

Copyright (c) 2000, 2013, Oracle and/or its affiliates. Allrights reserved.

 

Oracle is a registered trademark of Oracle Corporation and/orits

affiliates. Other names may be trademarks of their respective

owners.

 

Type 'help;' or '\h' for help. Type '\c' to clear the currentinput statement.

 

已連接成功!

 

 

四、配置vsftpd

1、建立pam認證所需文件

[root@centos6 ~]# vim /etc/pam.d/vsftpd.mysql

添加如下兩行:

authrequired pam_mysql.so user=vsftp passwd=magelinux host=172.16.2.16 db=vsftpdtable=users usercolumn=name passwdcolumn=password crypt=2

accountrequired pam_mysql.so user=vsftp passwd=magelinux host=172.16.2.16 db=vsftpdtable=users usercolumn=name passwdcolumn=password crypt=2

 

2、建立虛擬用戶映射的系統用戶及對應的目錄

[root@centos6 ~]# useradd -s /sbin/nologin -d/var/ftp/ftproot vftpuser

 

修改該目錄權限,爲了讓其他用戶能登錄後進入該目錄並具有讀權限

[root@centos6 ~]# chmod go+rx /var/ftp/ftproot

[root@centos6 ~]# ll -d /var/ftp/ftproot

drwxr-xr-x. 3 vftpuser vftpuser 4096 Oct 28 09:51/var/ftp/ftproot

 

 

五、請確保/etc/vsftpd/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=vftpuser

 

並確保pam_service_name選項的值如下所示

pam_service_name=vsftpd.mysql

 

 

六、重啓vsftpd服務,並把vsftpd服務設爲開啓自啓動

[root@centos6 ~]# service vsftpd restart

Shutting down vsftpd:                                      [  OK  ]

Starting vsftpd for vsftpd:                                [  OK  ]

[root@centos6 ~]# chkconfig vsftpd on

 

 

此時,在數據庫vsftpd中users表中的用戶均能登錄訪問了

但是,它們被默認映射爲匿名賬號,因此無法上傳數據

     

wKiom1YwVRLiaSW5AAIKpD-E8Rw557.jpg

wKioL1YwVUbR8vM-AAGKbHF5Dgk135.jpg

wKioL1YwVUfRTDGvAAGMIhneTmY806.jpg

 

 

七、爲每個用戶啓用不同的權限

vsftpd可以在配置文件目錄中爲每個用戶提供單獨的配置文件以定義其ftp服務訪問權限,每個虛擬用戶的配置文件名同虛擬用戶的用戶名。配置文件目錄可以是任意未使用目錄,只需要在vsftpd.conf指定其路徑及名稱即可。

 

1、配置vsftpd爲虛擬用戶使用配置文件目錄

編輯主配置文件,添加如下選項:

[root@centos6 ~]# vim /etc/vsftpd/vsftpd.conf

user_config_dir=/etc/vsftpd/vusers_config

 

2、創建所需要目錄,併爲虛擬用戶提供配置文件

[root@centos6 ~]# mkdir /etc/vsftpd/vusers_config/

[root@centos6 ~]# cd /etc/vsftpd/vusers_config/

[root@centos6 vusers_config]# touch user1 user2

 

3、配置虛擬用戶的訪問權限

虛擬用戶對vsftpd服務的訪問權限是通過匿名用戶的相關指令進行的

比如,如果需要讓tom用戶具有上傳文件的權限,可以修改/etc/vsftpd/vusers_config/tom文件,在裏面添加如下選項即可。

[root@centos6 vusers_config]# vim user1

anon_upload_enable=YES

anon_mkdir_write_enable=YES

anon_other_write_enable=YES

 

 

重啓vsftpd服務,重新登錄用戶驗證,此時權限已生效


wKiom1YwVRPDYSIcAAIy570qVdg984.jpg

 


 

 


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