FTP服務器的配置

FTP服務器

文件傳輸協議(File Transfer Protocol)是用於在網絡上進行文件傳輸的一套標準協議,使用客戶/服務器模式。專門用來傳輸文件的協議

FTP服務器工作原理

FTP是一種在互聯網中進行文件傳輸的協議,基於客戶端/服務器模式,默認使用20、21號端口,其中端口20(數據端口)用於進行數據傳輸,端口21(命令端口)用於接受客戶端發出的相關FTP命令與參數
在這裏插入圖片描述
FTP服務器允許用戶以三種認證模式登錄到FTP服務器上
匿名開放模式:是一種最不安全的認證模式,任何人都可以無需密碼驗證而直接登錄到FTP服務器。
本地用戶模式:是通過Linux系統本地的賬戶密碼信息進行認證的模式,相較於匿名開放模式更安全,配置起來也很簡單。
虛擬用戶模式:是這三種模式中最安全的一種認證模式,它需要爲FTP服務單獨建立用戶數據庫文件,虛擬出用來進行口令驗證的賬戶信息,而這些賬戶信息在服務器系統中實際上是不存在的

FTP服務器協議

FTP服務器是按照FTP協議在互聯網上提供文件存儲和訪問服務的主機,FTP客戶端則是向服務器發送連接請求,以建立數據傳輸鏈路的主機。FTP協議有下面兩種工作模式
主動模式:FTP服務器主動向客戶端發起連接請求
被動模式:FTP服務器等待客戶端發起連接請求(FTP的默認工作模式)

常見的FTP服務器軟件

Serv-U #Windows
FileZilla #Windows
VsFTP #Linux

常見的FTP的客戶端

FileZilla
WinScp
FlashFTP
SecureCRT
CuteFTP

VSFTP服務器的安裝及配置

1.關閉防火牆&Selinux

[root@linux ~]# systemctl stop firewalld
[root@linux ~]# setenforce 0
setenforce: SELinux is disabled
[root@linux ~]# getenforce 
Disabled

2.安裝VsFTP服務器及客戶端

[root@linux ~]# yum install vsftpd ftp -y
Loaded plugins: fastestmirror
c7                                                         | 3.6 kB  00:00:00     
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package ftp.x86_64 0:0.17-67.el7 will be installed
---> Package vsftpd.x86_64 0:3.0.2-22.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

==================================================================================
 Package           Arch              Version                  Repository     Size
==================================================================================
Installing:
 ftp               x86_64            0.17-67.el7              c7             61 k
 vsftpd            x86_64            3.0.2-22.el7             c7            169 k

Transaction Summary
==================================================================================
Install  2 Packages

Total download size: 230 k
Installed size: 444 k
Downloading packages:
----------------------------------------------------------------------------------
Total                                                663 kB/s | 230 kB  00:00     
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : vsftpd-3.0.2-22.el7.x86_64                                     1/2 
  Installing : ftp-0.17-67.el7.x86_64                                         2/2 
  Verifying  : ftp-0.17-67.el7.x86_64                                         1/2 
  Verifying  : vsftpd-3.0.2-22.el7.x86_64                                     2/2 

Installed:
  ftp.x86_64 0:0.17-67.el7              vsftpd.x86_64 0:3.0.2-22.el7             

Complete!

3.匿名訪問模式配置
#開放匿名用戶的上傳、下載文件的權限,以及讓匿名用戶創建、刪除、更名文件的權限。(注:此項不建議在生產環境使用。)

[root@linux ~]# vi /etc/vsftpd/vsftpd.conf
# 在vsftpd.conf文件中添加下面三句(若能在註釋了的文本中找到,也可取消註釋):
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES

[root@linux ~]# systemctl restart vsftpd			#重啓服務
[root@linux ~]# systemctl enable vsftpd
Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.

測試:

[root@web ~]# ftp 192.168.130.128
Connected to 192.168.130.128 (192.168.130.128).
220 (vsFTPd 3.0.2)
Name (192.168.130.128:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd pub
250 Directory successfully changed.
ftp> pwd
257 "/pub"
ftp> mkdir ftp_dir
550 Create directory operation failed.
#使用另一臺客戶機進行ftp測試,發現可以匿名訪問,但是使用匿名訪問時無法創建目錄

#此時可以在服務端更改pub目錄的權限
[root@linux ~]# chown -Rf ftp /var/ftp/pub/

#再次測試
[root@web ~]# ftp 192.168.130.128
Connected to 192.168.130.128 (192.168.130.128).
220 (vsFTPd 3.0.2)
Name (192.168.130.128:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd pub
250 Directory successfully changed.
ftp> pwd
257 "/pub"
ftp> mkdir ftp_dir
257 "/pub/ftp_dir" created			#創建成功
ftp> ls
227 Entering Passive Mode (192,168,130,128,77,21).
150 Here comes the directory listing.
drwx------    2 14       50              6 Nov 28 08:01 ftp_dir
226 Directory send OK.			#查詢,創建目錄存在

4.本地用戶模式

[root@linux ~]# vi /etc/vsftpd/vsftpd.conf
anonymous_enable=NO			#更改此項的參數爲NO
[root@linux ~]# systemctl restart vsftpd			#重啓服務

測試:

[root@web ~]# ftp 192.168.130.128
Connected to 192.168.130.128 (192.168.130.128).
220 (vsFTPd 3.0.2)
Name (192.168.130.128:root): root
530 Permission denied.
Login failed.			#測試使用root用戶登錄失敗
#vsftpd服務所在目錄中存放着兩個用戶名單ftpusers,user_list,出於安全原因,root默認不允許登錄,編輯這兩個文件註釋root即可。同時可以創建一個新用戶用於測試。
[root@linux vsftpd]# vi ftpusers
  1 # Users that are not allowed to login via ftp
  2 #root
  3 bin
  4 daemon
  5 adm
  6 lp
  7 sync
  8 shutdown
  9 halt
 10 mail
 11 news
 12 uucp
 13 operator
 14 games
 15 nobody
[root@linux vsftpd]# vi user_list
  1 # vsftpd userlist
  2 # If userlist_deny=NO, only allow users in this file
  3 # If userlist_deny=YES (default), never allow users in this file, and
  4 # do not even prompt for a password.
  5 # Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
  6 # for users that are denied.
  7 #root
  8 bin
  9 daemon
 10 adm
 11 lp
 12 sync
 13 shutdown
 14 halt
 15 mail
 16 news
 17 uucp
 18 operator
 19 games
 20 nobody
[root@linux vsftpd]# systemctl restart vsftpd

5.VsFTP高級應用,虛擬用戶模式
#虛擬用戶模式是這三種模式中最安全的一種認證模式,配置複雜

[root@linux vsftpd]# vi /etc/vsftpd/vuser.list
  1 user1
  2 123456
  3 user2
  4 123456

db_load命令用哈希(hash)算法將原始的明文信息文件轉換成數據庫文件

[root@linux vsftpd]# db_load -T -t hash -f vuser.list vuser.db
[root@linux vsftpd]# file vuser.db 
vuser.db: Berkeley DB (Hash, version 9, native byte-order)
[root@linux vsftpd]# chmod 600 vuser.db
[root@linux vsftpd]# rm -rf vuser.list

創建vsftpd服務程序用於存儲文件的根目錄以及虛擬用戶映射的系統本地用戶。FTP服務用於存儲文件的根目錄指的是當虛擬用戶登錄後所訪問的默認位置。

[root@linux vsftpd]# useradd -d /var/ftproot -s /sbin/nologin virtual
[root@linux vsftpd]# ls -ld /var/ftproot/
drwx------ 2 virtual virtual 62 Nov 28 17:14 /var/ftproot/
[root@linux vsftpd]# chmod -Rf 755 /var/ftproot/

建立用於支持虛擬用戶的PAM文件
#PAM是一組安全機制的模塊,系統管理員可以用來輕易地調整服務程序的認證方式

[root@linux vsftpd]# vi /etc/pam.d/vsftpd.vu 
  1 auth required pam_userdb.so db=/etc/vsftpd/vuser
  2 account required pam_userdb.so db=/etc/vsftpd/vuser
[root@linux vsftpd]# vi /etc/vsftpd/vsftpd.conf
anonymous_enable=No			#更改參數爲NO
guest_enable=YES				#添加以下四條參數
guest_username=virtual
allow_writeable_chroot=YES
pam_service_name=vsftpd.vu

爲虛擬用戶創建不同的權限

[root@linux vsftpd]# mkdir /etc/vsftpd/vusers_dir
[root@linux vsftpd]# cd /etc/vsftpd/vusers_dir/
[root@linux vsftpd]# touch user1
[root@linux vsftpd]# vi user1
  1 anon_upload_enable=YES
  2 anon_mkdir_write_enable=YES
  3 anon_other_write_enable=YES
[root@linux vusers_dir]# vi /etc/vsftpd/vsftpd.conf
user_config_dir=/etc/vsftpd/vusers_dir			#添加本條語句
[root@linux vusers_dir]# systemctl restart vsftpd

用user1和user2分別進行測試

[root@web ~]# ftp 192.168.130.128
Connected to 192.168.130.128 (192.168.130.128).
220 (vsFTPd 3.0.2)
Name (192.168.130.128:root): user1  
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 (192,168,130,128,214,181).
150 Here comes the directory listing.
226 Directory send OK.
ftp> mkdir user1
257 "/user1" created
ftp> touch user1
?Invalid command
ftp> ls
227 Entering Passive Mode (192,168,130,128,166,153).
150 Here comes the directory listing.
drwx------    2 10018    10018           6 Nov 28 09:44 user1
226 Directory send OK.
[root@web ~]# ftp 192.168.130.128
Connected to 192.168.130.128 (192.168.130.128).
220 (vsFTPd 3.0.2)
Name (192.168.130.128:root): user2
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 (192,168,130,128,123,173).
150 Here comes the directory listing.
drwx------    2 10018    10018           6 Nov 28 09:44 user1
226 Directory send OK.
ftp> mkdir user2
257 "/user2" created
ftp> touch user2
?Invalid command
ftp> ls
227 Entering Passive Mode (192,168,130,128,46,98).
150 Here comes the directory listing.
drwx------    2 10018    10018           6 Nov 28 09:44 user1
drwx------    2 10018    10018           6 Nov 28 09:46 user2
226 Directory send OK.
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章