Linux進階-2.安裝FTP服務

FTP是一種在互聯網中進行文件傳輸的協議,基於客戶端/服務器模式,默認使用20、21號端口,其中端口20(數據端口)用於進行數據傳輸,端口21(命令端口)用於接受客戶端發出的相關FTP命令與參數。

1.安裝服務

[root@linux Desktop]# yum install vsftpd -y
已加載插件:langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
正在解決依賴關係
--> 正在檢查事務
---> 軟件包 vsftpd.x86_64.0.3.0.2-9.el7 將被 安裝
--> 解決依賴關係完成

依賴關係解決

================================================================================
 Package          架構             版本                    源              大小
================================================================================
正在安裝:
 vsftpd           x86_64           3.0.2-9.el7             rhel           166 k

事務概要
================================================================================
安裝  1 軟件包

總下載量:166 k
安裝大小:343 k
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在安裝    : vsftpd-3.0.2-9.el7.x86_64                                   1/1 
  驗證中      : vsftpd-3.0.2-9.el7.x86_64                                   1/1 

已安裝:
  vsftpd.x86_64 0:3.0.2-9.el7                                                   

完畢!

2.編輯配置文件

將配置文件中開頭沒有#號的註釋重定向到配置文件中,這樣配置文件中的參數只有短短几行了。

[root@linux Desktop]# grep -v '#' /etc/vsftpd/vsftpd.conf.bak > /etc/vsftpd/vsftpd.conf
[root@linux Desktop]# cat /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

2.1配置匿名訪問模式

匿名訪問模式是最不安全的一種認證模式,任何人都可以無需密碼驗證而直接登錄到FTP服務器,一般用來訪問不重要的公開文件。在配置文件中匿名訪問時默認開啓的。我們需要做的就是開放匿名用戶的上傳、下載文件的權限,以及讓匿名用戶創建、刪除、更名文件的權限。默認共享的/var/ftp/pub目錄。

參數								作用
anonymous_enable=YES			允許匿名訪問模式
anon_umask=022					匿名用戶上傳文件的umask值
anon_upload_enable=YES			允許匿名用戶上傳文件
anon_mkdir_write_enable=YES		允許匿名用戶創建目錄
anon_other_write_enable=YES		允許匿名用戶修改目錄名稱或刪除目錄

2.2將這幾個參數加入到配置文件中。

[root@linux Desktop]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=YES
anon_umask=022
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

2.3重啓服務

[root@linux Desktop]# systemctl restart vsftpd

2.4清空防火牆策略和關閉selinux

[root@linux ftp]# iptables -F
[root@linux ftp]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  確定  ]
[root@linux ftp]# vim /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted

2.5windows客戶端訪問

訪問在資源管理器中輸入ftp://192.168.10.10
在這裏插入圖片描述
在FTP服務器上新建文件錯誤,查看服務器/var/ftp/pub的權限,發現只有root用戶才能寫入,將此目錄所有者改成ftp用戶。
在這裏插入圖片描述

[root@linux ~]# ls -ld /var/ftp/pub
drwxr-xr-x. 2 root root 6 5月  19 17:14 /var/ftp/pub
[root@linux ~]# chown -Rf ftp /var/ftp/pub
[root@linux ~]# ls -ld /var/ftp/pub
drwxr-xr-x. 2 ftp root 6 5月  19 17:14 /var/ftp/pub

客戶端可以正常創建文件了
在這裏插入圖片描述

2.6linux客戶端訪問

首先安裝ftp服務

[root@localhost ~]# yum install vsftpd
Loaded plugins: langpacks, product-id, subscription-manager
This system is not registered to Red Hat Subscription Management. You can use subscription-manager to register.
Resolving Dependencies
--> Running transaction check
---> Package vsftpd.x86_64 0:3.0.2-9.el7 will be installed
--> Finished Dependency Resolution

Dependencies Resolved

================================================================================
 Package          Arch             Version                 Repository      Size
================================================================================
Installing:
 vsftpd           x86_64           3.0.2-9.el7             rhel           166 k

Transaction Summary
================================================================================
Install  1 Package

Total download size: 166 k
Installed size: 343 k
Is this ok [y/d/N]: y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  Installing : vsftpd-3.0.2-9.el7.x86_64                                    1/1 
  Verifying  : vsftpd-3.0.2-9.el7.x86_64                                    1/1 

Installed:
  vsftpd.x86_64 0:3.0.2-9.el7                                                   

Complete!

在終端中輸入ftp 192.168.10.10,輸入用戶名anonymous,密碼爲空。

[root@localhost ~]# ftp 192.168.10.10
Connected to 192.168.10.10 (192.168.10.10).
220 (vsFTPd 3.0.2)
Name (192.168.10.10:root): anonymous
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> mkdir 123
257 "/pub/123" created
ftp> ls
227 Entering Passive Mode (192,168,10,10,79,24).
150 Here comes the directory listing.
drwxr-xr-x    2 14       50              6 May 19 09:52 123
drwxr-xr-x    2 14       50              6 May 19 09:24 新文件夾
226 Directory send OK.

3.配置本地用戶訪問模式

相較於匿名開放模式,本地用戶模式要更安全,使用Linux中用戶的賬戶登錄,默認共享所登錄用戶的家目錄。

3.1將這幾個參數加入到配置文件中。

[root@linux Desktop]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
local_enable=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

3.2重啓服務

[root@linux ~]# systemctl restart vsftpd

3.3清空防火牆策略和關閉selinux

[root@linux ftp]# iptables -F
[root@linux ftp]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  確定  ]
[root@linux ftp]# vim /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted

3.4刪除“黑名單”

若用戶名存在ftpusers和user_list兩個文件中,是不允許使用用戶賬號登錄FTP的,所以登陸之前先查看有沒有用戶。

[root@linux ~]# vim /etc/vsftpd/user_list 
刪除需要登錄的用戶...
[root@linux ~]# vim /etc/vsftpd/ftpusers 
刪除需要登錄的用戶...

3.5客戶端訪問

登錄方法和匿名用戶登錄一樣,只是需要輸入用戶名和密碼,使用服務器本地用戶登錄就可以訪問登錄用戶的家目錄了。
在這裏插入圖片描述
在這裏插入圖片描述

4.虛擬用戶模式

4.1創建FTP用戶

在/etc/vsftpd/下創建一個文件,在文件中奇數行寫入FTP用戶名,偶數行寫入上一行用戶名的密碼。使用db_load命令用hash算法將原始的明文信息文件轉換成數據庫文件,降低數據庫文件的權限只讓root用戶可以讀寫,最後再把原始的明文信息文件刪除。

[root@linux Desktop]# vim /etc/vsftpd/user
shijie2
shijie2
shijie3
shijie3

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

4.2創建訪問目錄

創建一個可以映射到虛擬用戶的系統本地用戶,讓虛擬用戶默認登錄到與之有映射關係的這個系統本地用戶的家目錄中,虛擬用戶創建的文件的屬性也都歸屬於這個系統本地用戶,從而避免Linux系統無法處理虛擬用戶所創建文件的屬性權限。

[root@linux Desktop]# useradd -d /var/ftpfile -s /sbin/nologin virtual
[root@linux Desktop]# ls -ld /var/ftpfile/
drwx------. 3 virtual virtual 74 5月  19 20:37 /var/ftpfile/
[root@linux Desktop]# chmod -Rf 755 /var/ftpfile/

4.3虛擬用戶認證

新建一個用於虛擬用戶認證的PAM文件vsftpd.vu,其中PAM文件內的“db=”參數爲使用db_load命令生成的賬戶密碼數據庫文件的路徑,但不用寫數據庫文件的後綴。

[root@linux Desktop]# vim /etc/pam.d/vsftpd.vu
auth       required     pam_userdb.so db=/etc/vsftpd/user
account    required     pam_userdb.so db=/etc/vsftpd/user

4.4編輯配置文件

anonymous_enable=NO			禁止匿名開放模式
local_enable=YES			允許本地用戶模式
guest_enable=YES			開啓虛擬用戶模式
guest_username=virtual		指定虛擬用戶賬戶
pam_service_name=vsftpd.vu	指定PAM文件
allow_writeable_chroot=YES	允許對禁錮的FTP根目錄執行寫入操作,而且不拒絕用戶的登錄請求
[root@linux Desktop]# vim /etc/vsftpd/vsftpd.conf
anonymous_enable=NO
local_enable=YES
guest_enable=YES
guest_username=virtual
pam_service_name=vsftpd.vu
allow_writeable_chroot=YES
write_enable=YES
local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=NO
listen_ipv6=YES
userlist_enable=YES
tcp_wrappers=YES

4.5修改虛擬用戶權限

新建一個目錄,在裏面分別創建以用戶名命名的文件,使用匿名用戶的參數在名爲shijie2的文件中寫入允許的相關權限。並使用參數user_config_dir將這個目錄添加進配置文件。

[root@linux Desktop]# vim /etc/vsftpd/userdir/shijie2
anonymous_enable=NO
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
[root@linux Desktop]# echo 'user_config_dir=/etc/vsftpd/userdir'>> /etc/vsftpd/vsftpd.conf

4.6重啓服務

[root@linux ~]# systemctl restart vsftpd
[root@linux ~]# systemctl enable vsftpd
[root@linux ftp]# iptables -F
[root@linux ftp]# service iptables save
iptables: Saving firewall rules to /etc/sysconfig/iptables:[  確定  ]
[root@linux ftp]# vim /etc/selinux/config 
SELINUX=disabled
SELINUXTYPE=targeted

4.7客戶端訪問

使用虛擬用戶shijie2和shijie3訪問服務器。
在這裏插入圖片描述
shijie2用戶可以新建、修改文件。
在這裏插入圖片描述
而shijie3用戶只有訪問權限。
在這裏插入圖片描述

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