linux 配置ftp 服務器 與訪問ftp 服務


 

一:配置FTP 服務器,允許匿名訪問

1.1 FTP 服務  

FTP 服務,用來傳輸文件的協議

FTP 服務器默認使用 TCP 協議的20,21 端口

  • 20 端口用於建立數據連接,並傳輸文件數據
  • 21 端口用於建立控制連接,並傳輸FTP 控制命令

 

FTP 數據連接分為2主動模式和被動模式

主動模式: 服務器主動發起數據連接

被動模式: 服務器被動等待數據鏈接 (本章實驗使用被動模式)

 

 1.2  配置FTP 服務器,允許匿名訪問

[root@localhost ~]# yum -y install vsftpd                  #安裝vsftpd 包

root@localhost ~]# cd /etc/vsftpd/  
[root@localhost vsftpd]# ls
ftpusers  user_list  vsftpd.conf  vsftpd_conf_migrate.sh
[root@localhost vsftpd]# cp vsftpd.conf vsftpd.bak          #備份vsftp.conf 文件

[root@localhost vsftpd]# ls
ftpusers  user_list  vsftpd.bak  vsftpd.conf  vsftpd_conf_migrate.sh

[root@localhost vsftpd]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES           #開啟匿名訪問(默認已經開啟)
write_enable=YES               #開放服務器的寫權限(上傳文件需要開啟)默認快開啟
anon_umask=022                 #設置匿名用戶上傳數據的權限掩碼為022 (反掩碼)
anon_upload_enable=YES         #允許匿名用戶上傳文件
anon_mkdir_write_enable=YES    #允許匿名用於創建(上傳)目錄
anon_other_write_enable=YES    #允許其他用戶刪除,重命名,覆蓋等操作
:wq                           #保存退出

[root@localhost vsftpd]# systemctl start vsftpd     #啟動 ftp 服務
[root@localhost vsftpd]# systemctl enable  vsftpd   #開啟 ftp 的自啟
[root@localhost vsftpd]# systemctl stop firewalld   #關閉防火牆
[root@localhost vsftpd]# setenforce 0               #設置selinux 模式

[root@localhost vsftpd]# chmod 777  /var/ftp/pub 
 #為匿名訪問ftp 目錄的根目錄 下的 pub 子目錄 權限設置為最大權限,以便匿名用戶上傳數據

  

 二:windows 訪問測試

連接方式:

在Windows 系統 Win +r  ,然后 輸入 cmd  ,打開命令行輸入。

 

切換windoes 目錄到桌面:

C:\Users\Administrator>cd C:\Users\Administrator\Desktop 

 

連接ftp服務器:   

  ftp   ftp服務器地址

 

 匿名訪問:

   用戶名為 ftp ,密碼為空, 登錄ftp

ftp 操作:

  pwd                  #查看當前所在目錄

  ls                      #查看當前目錄

  cd                     #切換目錄

  get  文件名       #下載文件到windows 本地

  put  文件名       #上傳文件到 ftp目錄

       quit                   #退出

  

C:\Users\Administrator>cd C:\Users\Administrator\Desktop   #切換到桌面
C:\Users\Administrator\Desktop>ftp 192.168.23.10 #連接到ftp服務器
連接到 192.168.23.10。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用戶(192.168.23.10:(none)): ftp #用戶名輸入 ftp
331 Please specify the password.
密碼:                                              #密碼為空
230 Login successful.

ftp> pwd                            #查看當前所在目錄
257 "/"                           #根目錄(匿名用戶根目錄是/var/ftp/)

ftp> ls                           #查看目錄下的內容
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
pub                              #目錄下有pub 目錄
226 Directory send OK.
ftp: 收到 8 字節,用時 0.00秒 8.00千字節/秒。

ftp> cd pub                     #切換到pub 目錄
250 Directory successfully changed.
ftp>
ftp> pwd
257 "/pub"

ftp> ls                         #查看pub 目錄下的內容
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
abc.txt                        #擁有 abc.txt 文件
226 Directory send OK.
ftp: 收到 12 字節,用時 0.00秒 12000.00千字節/秒。

ftp> get abc.txt               #從ftp 服務器下載 abc.txt 到本地
200 PORT command successful. Consider using PASV.
150 Opening BINARY mode data connection for abc.txt (15 bytes).
226 Transfer complete.
ftp: 收到 15 字節,用時 0.00秒 15.00千字節/秒。

ftp> put test.txt              #從本地 上傳 test.txt 到ftp 服務器
200 PORT command successful. Consider using PASV.
150 Ok to send data.
226 Transfer complete.
ftp: 發送 12 字節,用時 0.00秒 12.00千字節/秒。
ftp> quit                     #退出ftp 
221 Goodbye.

###  驗證 ###
C:\Users\Administrator\Desktop>dir  abc.txt    #windows查看 abc.txt 是否存在
2021/06/29  10:25                15 abc.txt
               1 個文件             15 字節
               0 個目錄 725,971,259,392 可用字節
               
[root@localhost pub]# pwd   
/var/ftp/pub
[root@localhost pub]# ls  test.txt            #linux 查看 test 文件是否存在
test.txt
              

  

 

三:設置本地用戶驗證訪問ftp

3.1 修改配置文件 /etc/vsftpd/vsftpd.conf

 設置本地用戶可以登錄,禁止匿名用戶登錄

[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf  
local_enable=YES               #啟用本地用戶
anonymous_enable=NO #關閉匿名用戶訪問(要關閉,而不失注釋)
write_enable=YES               #開啟服務器寫權限
local_umask=077                #設置僅宿主擁有上傳文件的權限(反掩碼)
chroot_local_user=YES          #將訪問禁錮在用戶的宿主目錄中
allow_writeable_chroot=YES     #允許被限制的用戶主目錄具有寫權限
:wq

[root@localhost ~]# systemctl restart vsftpd   #重啟服務

 

 

 3.2 訪問測試

C:\Users\Administrator\Desktop>ftp 192.168.23.10
連接到 192.168.23.10220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用戶(192.168.23.10:(none)): ftp
331 Please specify the password.
密碼:
530 Login incorrect.
登錄失敗。 #ftp 用戶無密碼登錄失敗

C:\Users\Administrator\Desktop>ftp 192.168.23.10
連接到 192.168.23.10220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用戶(192.168.23.10:(none)): test  #使用test 用戶登錄
331 Please specify the password.
密碼:                             #輸入test 用戶密碼
230 Login successful. #使用 普通用戶 test 的賬號登錄成功
ftp>
ftp> pwd 
257 "/" #普通用戶的根目錄是 它的 家目錄
ftp> ls
200 PORT command successful. Consider using PASV.
150 Here comes the directory listing.
下載
公共
圖片
文檔
桌面
模板
視頻
音樂
226 Directory send OK.     #這些是用戶家目錄里的文件
ftp: 收到 67 字節,用時 0.00秒 16.75千字節/秒。

 

 

 3.3 圖形化登錄

(1)  在地址欄 輸入 ftp://ftp 服務器地址 

 

 

(2) 輸入 用戶名 和 密碼 (如果是匿名用戶,點上匿名登錄)

 

 

 (3) windows 和linux 之間可以直接拖拽上傳

從windows 上 拖拽上傳 

 

[test@localhost ~]$ whoami
test
[test@localhost ~]$ pwd
/home/test
[test@localhost ~]$ ls
test.txt  公共  模板  視頻  圖片  文檔  下載  音樂  桌面

  

 四:設置白名單和黑名單

4.1 設置黑名單

配置 文件  /etc/vsftpd/usr_list   ,向文件添加或刪除 用戶

[root@localhost ~]# vim /etc/vsftpd/user_list
test      #添加用戶
 :wq
 
[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf 
 userlist_enable=YES userlist_deny=YES  (默認是此設置)
 #設置黑名單,不允許 user_list 里的用戶登錄
:wq

 [root@localhost ~]# systemctl restart vsftpd   #重啟服務

  

 測試登錄

C:\Users\Administrator\Desktop>ftp 192.168.23.10
連接到 192.168.23.10。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用戶(192.168.23.10:(none)): lisi  
331 Please specify the password.
密碼:
230 Login successful.         #lisi 用戶不在user_list 中,可以登錄
ftp> quit
221 Goodbye.

C:\Users\Administrator\Desktop>ftp 192.168.23.10
連接到 192.168.23.10。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用戶(192.168.23.10:(none)): test
530 Permission denied.      #test 用戶在user_list 中,不可以登錄
登錄失敗。

  

 4.2 設置白名單

 

[root@localhost ~]# vim /etc/vsftpd/vsftpd.conf    
userlist_enable=YES
userlist_deny=NO         
 #將YES 改為 NO,即 設置白名單,僅允許user_list 中用戶登錄
 :wq
 [root@localhost ~]# systemctl restart vsftpd   #重啟服務

  

 測試

C:\Users\Administrator\Desktop>ftp 192.168.23.10
連接到 192.168.23.10。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用戶(192.168.23.10:(none)): lisi
530 Permission denied.
登錄失敗。               #用戶lisi 不在白名單內,登錄失敗
ftp> quit
221 Goodbye.

C:\Users\Administrator\Desktop>ftp 192.168.23.10
連接到 192.168.23.10。
220 (vsFTPd 3.0.2)
200 Always in UTF8 mode.
用戶(192.168.23.10:(none)): test
331 Please specify the password.
密碼:
230 Login successful.      #用戶test 在白名單內,登錄成功
ftp>

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM