■ 河北科技師范學(xué)院 趙學(xué)作
編者按:盡管FTP服務(wù)器在傳輸文件時(shí)非常方便,但其安全性是非常脆弱的,因此打造一個(gè)安全可控的FTP服務(wù)器對(duì)單位網(wǎng)絡(luò)安全是非常有必要的。
FTP是用于在網(wǎng)絡(luò)上進(jìn)行文件傳輸?shù)囊惶讟?biāo)準(zhǔn)協(xié)議,使用客戶/服務(wù)器模式。它屬于網(wǎng)絡(luò)傳輸協(xié)議的應(yīng)用層。
Centos7是我們常用的服務(wù)器系統(tǒng)之一,我們也經(jīng)常需要在這個(gè)系統(tǒng)中安裝并配置安全可控的FTP服務(wù)器。
1.安裝vsftpd服務(wù)器命令。
# yum install vsftpd
2.啟動(dòng)vsftpd并設(shè)置在下次啟動(dòng)時(shí)自動(dòng)啟用。
# systemctl start vsftpd
# systemctl enable vsftpd
3.設(shè)置防火墻,允許外部系統(tǒng)用通過(guò)21端口訪問(wèn)FTP 服務(wù)。
# firewall-cmd--zone=public --permanent--add-port=21/tcp
# firewall-cmd--zone=public --permanent--add-service=ftp
# firewall-cmd--reload
1.修改配置文件。
配置前先備份一下原始配置文件 /etc/vsftpd/vsftpd.conf
# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.orig
然后修改這個(gè)文件,并將下面的選項(xiàng)設(shè)置相關(guān)的值:
# vi /etc/vsftpd/vsftpd.conf
anonymous_enable=NO### 禁用匿名登錄
write_enable=YES### 允許對(duì)文件系統(tǒng)做改動(dòng)的 FTP 命令
local_enable=YES### 允許本地用戶登錄
local_umask=022### 本地用戶創(chuàng)建文件所用的 umask 值
dirmessage_enable=YES### 當(dāng)用戶首次進(jìn)入一個(gè)新目錄時(shí)顯示一個(gè)消息
xferlog_enable=YES### 用于記錄上傳、下載細(xì)節(jié)的日志文件
connect_from_port_20=YES ### 使用端口 20 (ftp-data)用于PORT 風(fēng)格的連接
xferlog_std_format=YES ### 使用標(biāo)準(zhǔn)的日志格式
listen=NO### 不要讓 vsftpd 運(yùn)行在獨(dú)立模式
listen_ipv6=YES### vsftpd 將監(jiān)聽(tīng) IPv6而不是 IPv4
pam_service_name=vsftpd ### vsftpd使用的 PAM 服務(wù)名
userlist_enable=YES### vsftpd 支持載入用戶列表
tcp_wrappers=YES### 使用 tcp wrappers
chroot_local_user=YES###禁止切換根目錄
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list###如果沒(méi)有此文件則創(chuàng)建此文件,不需要添加內(nèi)容
allow_writeable_chroot=YES
local_root=/home/ftp/pub ###限定主目錄
2.創(chuàng)建FTP用戶。
# useradd qhdedu
3.為用戶qhdedu設(shè)置密碼qhd123
# echo "qhd123" |passwd qhdedu --stdin
4.限制這個(gè)新創(chuàng)建的用戶qhdedu只能通過(guò)FTP訪問(wèn)。
# usermod -s /sbin/nologin qhdedu
5.為用戶分配主目錄。
創(chuàng)建目錄:
# mkdir -p /home/ftp/qhdedu
創(chuàng)建歡迎文件
echo "歡迎登錄FTP"> /home/ftp/qhdedu/.message
圖1 提示登錄成功
設(shè)置該目錄的訪問(wèn)權(quán)限
# chmod a-w /home/ftp/qhdedu && chmod 777-R /home/ftp/qhdedu
設(shè)置為用戶主目錄
# usermod -d /home/ftp/qhdedu qhdedu
6.基于用戶列表文件 /etc/vsftpd/userlist來(lái)配置FTP來(lái)允許/拒絕用戶的訪問(wèn)。
默認(rèn)情況下,如果設(shè)置了userlist_enable=YES,當(dāng)userlist_deny選項(xiàng)設(shè)置為YES的 時(shí) 候,userlist_file=/etc/vsftpd/userlist中列出的用戶被拒絕登錄。如果配置為userlist_deny=NO,意 味 著只有在 userlist_file=/etc/vsftpd/userlist顯式指定的用戶才允許登錄。
# vi /etc/vsftpd/vsftpd.conf
#### vsftpd 支持載入用戶列表的的配置
userlist_enable=YES#### vsftpd 將 從userlist_file 給出的文件中載入用戶名列表
userlist_deny=NO
userlist_file=/etc/vsftpd/userlist ####存儲(chǔ)用戶名的文件,一行一個(gè)用戶名
7.將FTP服務(wù)器開(kāi)啟被動(dòng)模式。
# vi /etc/vsftpd/vsftpd.conf #修改配置文件,在末尾添加以下內(nèi)容:
pasv_enable=YES
pasv_min_port=30000
pasv_max_port=31000
8.保存后重啟vsftpd。
# service vsftpd restart
或systemctl start vsftpd
我們可以用systemctl status vsftpd可查看當(dāng)前狀態(tài)。
1.用qhdedu用戶身份登錄。
ftp://192.168.1.18
輸入用戶名:qhdedu
輸入密碼:qhd123
會(huì)顯示登錄成功,并顯示歡迎內(nèi)容。如圖1所示。
2.用FTP匿名登錄會(huì)提示失敗。如圖2所示。
圖2 提示登錄失敗
vsftpd.conf文件默認(rèn)支持匿名登錄,修改vsftpd.conf可更改登錄方式。
anonymous_enable=YES # YES為匿名可以登錄 NO為匿名不可以登錄
write_enable=YES
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_other_write_enable=YES
anon_world_readable_only=NO
anon_root=/home/ftp/pub
配置完再給相關(guān)目錄賦權(quán):
# chown root:ftp /home/ftp/pub
# chmod 755 /home/ftp/pub
圖3 出現(xiàn)錯(cuò)誤提示
圖4登錄成功
這句非常關(guān)鍵,也決定著是否能登錄,權(quán)限必須為755,而不能是 777,如果是777,則登錄時(shí)出現(xiàn)錯(cuò)誤提示:
500 OOPS: vsftpd:refusing to run with writable root inside chroot ()
如圖3所示。
如果想給匿名賬號(hào)有寫(xiě)入權(quán)限(上傳權(quán)限),可以建立一個(gè)子目錄,并賦于權(quán)限,如:
mkdir -p /home/ftp/pub/ftp
chmod 777 /home/ftp/pub/ftp
也就是說(shuō),要在anon_root=/home/ftp/pub中的目錄/home/ftp/pub建立一個(gè)子目錄(即兩個(gè)目錄不能是同一目錄)。
如果SELinux是啟動(dòng)狀態(tài),還要放
寬SELinux的控制權(quán)限,即更改SELinux為Permissive模式:
# vi /etc/selinux/config
將文件中SELINUX=enforcing一 句 改 成SELINUX=permissive重啟動(dòng)后再getenforce時(shí)應(yīng)該顯示permissive。當(dāng)然也可用setenforce 0直接暫時(shí)關(guān)閉SELinux,但不推薦此方法。
這時(shí)再測(cè)試匿名登錄:
ftp://192.168.1.18
ftp
密碼回車(密碼為空)
cd ftp
看到可以登錄成功,并且可以上傳文件(如圖4所示):
put E:a.txt #將E:a.txt這個(gè)文件上傳至/home/ftp/pub/ftp。