1 概述
工作中,需要搭建ftp服務(wù)器來(lái)實(shí)現(xiàn)資源的共享。本文將通過(guò)腳本實(shí)現(xiàn)自動(dòng)化安裝ftp服務(wù)器,并進(jìn)行相關(guān)配置,同時(shí)創(chuàng)建ftp登錄賬戶,以及創(chuàng)建兩個(gè)文件夾,作為公共的文件夾,為所有用戶提供共同的訪問(wèn)路徑
2 概念解釋和注意事項(xiàng)
2.1 chroot_list說(shuō)明
chroot_list_file=/etc/vsftpd/chroot_list
如果設(shè)置為
chroot_local_user=Y(jié)ES
chroot_list_enable=YES(這行可以沒(méi)有, 也可以有)
chroot_list_file=/etc/vsftpd.chroot_list
那么, 凡是加在文件vsftpd.chroot_list中的用戶都是不受限止的用戶
即可以瀏覽其主目錄的上級(jí)目錄。
所以, 如果不希望某用戶能夠?yàn)g覽其主目錄上級(jí)目錄中的內(nèi)容,可以如上設(shè)置, 然后在文件vsftpd.chroot_list中不添加該用戶即可(此時(shí), 在該文件中的用戶都是可以瀏覽其主目錄之外的目錄的).
或者, 設(shè)置如下
chroot_local_user=NO
chroot_list_enable=YES(這行必須要有, 否則文件vsftpd.chroot_list不會(huì)起作用)
chroot_list_file=/etc/vsftpd.chroot_list
然后把所有不希望有這種瀏覽其主目錄之上的各目錄權(quán)限的用戶添加到文件vsftpd.chroot_list(此時(shí), 在該文件中的用戶都是不可以瀏覽其主目錄之外的目錄的)中即可(一行一個(gè)用戶名).
2.2 selinux
如果服務(wù)已經(jīng)安裝,防火墻端口21已經(jīng)打開,但是還是不能訪問(wèn),可能是selinux的影響,建議打開如下的選項(xiàng)
getsebool -a | grep ftp
setsebool -P tftp_home_dir=1
2.3 監(jiān)聽(tīng)端口問(wèn)題
如果出現(xiàn)在服務(wù)器上可以用ftp localhost登錄,但是在window電腦上不能通過(guò)資源管理器來(lái)訪問(wèn),即ftp ip的方式訪問(wèn)不了。但是centOS7上服務(wù)正常。配置文件有問(wèn)題導(dǎo)致,可能是同時(shí)監(jiān)聽(tīng)了ipv4和ipv6端口,導(dǎo)致服務(wù)異常建議配置文件里只寫如監(jiān)聽(tīng)ipv4端口,不要寫ipv6端口,在配置文件里同時(shí)監(jiān)聽(tīng)ipv4和ipv6是上不支持
#listen_ipv6=YES這個(gè)語(yǔ)句不要配置
同時(shí)配置監(jiān)聽(tīng)ipv4和ipv6的重啟服務(wù)報(bào)錯(cuò)如下
[root@localhost ~]#service vsftpd restart
Shutting down vsftpd:? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? [? OK? ]
Starting vsftpd for vsftpd: 500 OOPS: run two copies of vsftpd for IPv4 and IPv6
[FAILED]
2.4 vsftpd的配置文件說(shuō)明
vsftpd.ftpusers:位于/etc目錄下。它指定了哪些用戶賬戶不能訪問(wèn)FTP服務(wù)器,例如root等。
vsftpd.user_list:位于/etc目錄下。該文件里的用戶賬戶在默認(rèn)情況下也不能訪問(wèn)FTP服務(wù)器,僅當(dāng)vsftpd .conf配置文件里啟用userlist_enable=NO選項(xiàng)時(shí)才允許訪問(wèn)。
vsftpd.conf:位于/etc/vsftpd目錄下。來(lái)自定義用戶登錄控制、用戶權(quán)限控制、超時(shí)設(shè)置、服務(wù)器功能選項(xiàng)、服務(wù)器性能選項(xiàng)、服務(wù)器響應(yīng)消息等FTP服務(wù)器的配置。
(1)用戶登錄控制
anonymous_enable=YES,允許匿名用戶登錄。
no_anon_password=YES,匿名用戶登錄時(shí)不需要輸入密碼。
local_enable=YES,允許本地用戶登錄。
deny_email_enable=YES,可以創(chuàng)建一個(gè)文件保存某些匿名電子郵件的黑名單,以防止這些人使用Dos攻擊。
banned_email_file=/etc/vsftpd.banned_emails,當(dāng)啟用deny_email_enable功能時(shí),所需的電子郵件黑名單保存路徑(默認(rèn)為/etc/vsftpd.banned_emails)。
(2)用戶權(quán)限控制
write_enable=YES,開啟全局上傳權(quán)限。
local_umask=022,本地用戶的上傳文件的umask設(shè)為022(系統(tǒng)默認(rèn)是077,一般都可以改為022)。
anon_upload_enable=YES,允許匿名用戶具有上傳權(quán)限,很明顯,必須啟用write_enable=YES,才可以使用此項(xiàng)。同時(shí)我們還必須建立一個(gè)允許ftp用戶可以讀寫的目錄(前面說(shuō)過(guò),ftp是匿名用戶的映射用戶賬號(hào))。
anon_mkdir_write_enable=YES,允許匿名用戶有創(chuàng)建目錄的權(quán)利。
chown_uploads=YES,啟用此項(xiàng),匿名上傳文件的屬主用戶將改為別的用戶賬戶,注意,這里建議不要指定root賬號(hào)為匿名上傳文件的屬主用戶!
chown_username=whoever,當(dāng)啟用chown_uploads=YES時(shí),所指定的屬主用戶賬號(hào),此處的whoever自然要用合適的用戶賬號(hào)來(lái)代替。
chroot_list_enable=YES,可以用一個(gè)列表限定哪些本地用戶只能在自己目錄下活動(dòng),如果chroot_local_user=YES,那么這個(gè)列表里指定的用戶是不受限制的。
chroot_list_file=/etc/vsftpd.chroot_list,如果chroot_local_user=YES,則指定該列表(chroot_local_user)的保存路徑(默認(rèn)是/etc/vsftpd.chroot_list)。
nopriv_user=ftpsecure,指定一個(gè)安全用戶賬號(hào),讓FTP服務(wù)器用作完全隔離和沒(méi)有特權(quán)的獨(dú)立用戶。這是vsftpd系統(tǒng)推薦選項(xiàng)。
async_abor_enable=YES,強(qiáng)烈建議不要啟用該選項(xiàng),否則將可能導(dǎo)致出錯(cuò)!
ascii_upload_enable=YES;ascii_download_enable=YES,默認(rèn)情況下服務(wù)器會(huì)假裝接受ASCⅡ模式請(qǐng)求但實(shí)際上是忽略這樣的請(qǐng)求,啟用上述的兩個(gè)選項(xiàng)可以讓服務(wù)器真正實(shí)現(xiàn)ASCⅡ模式的傳輸。
注意:?jiǎn)⒂胊scii_download_enable選項(xiàng)會(huì)讓惡意遠(yuǎn)程用戶們?cè)贏SCⅡ模式下用“SIZE/big/file”這樣的指令大量消耗FTP服務(wù)器的I/O資源。
這些ASCⅡ模式的設(shè)置選項(xiàng)分成上傳和下載兩個(gè),這樣我們就可以允許ASCⅡ模式的上傳(可以防止上傳腳本等惡意文件而導(dǎo)致崩潰),而不會(huì)遭受拒絕服務(wù)攻擊的危險(xiǎn)。
(3)用戶連接和超時(shí)選項(xiàng)
idle_session_timeout=600,可以設(shè)定默認(rèn)的空閑超時(shí)時(shí)間,用戶超過(guò)這段時(shí)間不動(dòng)作將被服務(wù)器踢出。
data_connection_timeout=120,設(shè)定默認(rèn)的數(shù)據(jù)連接超時(shí)時(shí)間。
(4)服務(wù)器日志和歡迎信息
dirmessage_enable=YES,允許為目錄配置顯示信息,顯示每個(gè)目錄下面的message_file文件的內(nèi)容。
ftpd_banner=Welcome to blah FTP service,可以自定義FTP用戶登錄到服務(wù)器所看到的歡迎信息。
xferlog_enable=YES,啟用記錄上傳/下載活動(dòng)日志功能。
xferlog_file=/var/log/vsftpd.log,可以自定義日志文件的保存路徑和文件名,默認(rèn)是/var/log/vsftpd.log。
anonymous_enable=YES 允許匿名登錄local_enable=YES 允許本地用戶登錄
write_enable=YES 開放本地用戶寫權(quán)限
local_umask=022 設(shè)置本地用戶生成文件的掩碼為022
#anon_upload_enable=YES 此項(xiàng)設(shè)置允許匿名用戶上傳文件
#anon_mkdir_write_enable=YES 開啟匿名用戶的寫和創(chuàng)建目錄的權(quán)限
dirmessage_enable=YES 當(dāng)切換到目錄時(shí),顯示該目錄下的.message隱藏文件的內(nèi)容
xferlog_enable=YES 激活上傳和下載日志
connect_from_port_20=YES 啟用FTP數(shù)據(jù)端口的連接請(qǐng)求
#chown_uploads=YES 是否具有上傳權(quán)限. 用戶由chown_username參數(shù)指定。
#chown_username=whoever 指定擁有上傳文件權(quán)限的用戶。此參數(shù)與chown_uploads聯(lián)用。
#xferlog_file=/var/log/vsftpd.log
xferlog_std_format=YES 使用標(biāo)準(zhǔn)的ftpd xferlog日志格式
#idle_session_timeout=600 此設(shè)置將在用戶會(huì)話空閑10分鐘后被中斷
#data_connection_timeout=120 將在數(shù)據(jù)連接空閑2分鐘后被中斷
#ascii_upload_enable=YES 啟用上傳的ASCII傳輸方式
#ascii_download_enable=YES 啟用下載的ASCII傳輸方式
#ftpd_banner=Welcome to blah FTP service 設(shè)置用戶連接服務(wù)器后顯示消息
#deny_email_enable=NO 此參數(shù)默認(rèn)值為NO。當(dāng)值為YES時(shí),拒絕使用banned_email_file參數(shù)指定文件中所列出的e-mail地址用戶登錄。
#banned_email_file=/etc/vsftpd.banned_emails 指定包含拒絕的e-mail地址的文件.
#chroot_list_enable=YES 設(shè)置本地用戶登錄后不能切換到自家目錄以外的別的目錄
#chroot_list_file=/etc/vsftpd.chroot_list
#ls_recurse_enable=YES
pam_service_name=vsftpd 設(shè)置PAM認(rèn)證服務(wù)的配置文件名稱,該文件存放在/etc/pam.d/
userlist_enable=YES 此項(xiàng)配置/etc/vsftpd.user_list中指定的用戶也不能訪問(wèn)服務(wù)器,若添加userlist_deny=No,則僅僅/etc/vsftpd.user_list文件中的用戶可以訪問(wèn),其他用戶都不可以訪問(wèn)服務(wù)器。如過(guò)userlist_enable=NO,userlist_deny=YES,則指定使文件/etc/vsftpd.user_list中指定的用戶不可以訪問(wèn)服務(wù)器,其他本地用戶可以訪問(wèn)服務(wù)器。
listen=YES 指明VSFTPD以獨(dú)立運(yùn)行方式啟動(dòng)
tcp_wrappers=YES 在VSFTPD中使用TCP_Wrappers遠(yuǎn)程訪問(wèn)控制機(jī)制,默認(rèn)值為YES
3 一鍵化安裝腳本
?#!/bin/bash
#
#******************************************************************************
#Author:? ? ? ? ? ? ? Sunny
#Date:? ? ? ? ? ? ? ? 2017-09-11
#FileName:? ? ? ? ? ? install_ftp.sh
#version:? ? ? ? ? ? ? 1.0
#Your change info:
#Description:? ? ? ? ? For auto install ftp
#Copyright(C):? ? ? ? 2017? All rihts reserved
#*****************************************************************************
#ftp service dir is relative to system dir attribute.for example,if you are use anonmous to login ftp,if you upload some file to ftp,it use defaults user ftp to upload new file,and the new file permisson is 600,so other people could not download the file unless you change the file attribute about? other with r permission
time=`date +%Y%m%d%H%M`
#the function is used to install vsftpd? generate a new config file and create chroot_list and welcome.txt? file
#install ftp server
rpm -q? vsftpd &>/dev/null || yum -y install vsftpd &>/dev/null;
#install ftp? client,not necessary,but suggest
rpm -q ftp &>/dev/null || yum -y install ftp &>/dev/null;
[ -e /etc/vsftpd/vsftpd.conf ] && mv /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf."$time".bak
[ -e /etc/vsftpd/welcome.txt ] && mv? /etc/vsftpd/welcome.txt? /etc/vsftpd/welcome.txt."$time".bak
cat >>/etc/vsftpd/vsftpd.conf<
#should not listen both ipv4 and ipv6 port.suggest just only listen ipv4,so listen_ipv6 does not need to config.
#about anonymous
anonymous_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_upload_enable=YES
#about entity user
local_enable=YES
write_enable=YES
local_umask=022
userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd/user_list
chroot_local_user=YES
chroot_list_enable=YES
chroot_list_file=/etc/vsftpd/chroot_list
#about server env
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES
listen=YES
pam_service_name=vsftpd
tcp_wrappers=YES
use_localtime=yes
#/etc/vsftpd/welcom.txt? need be created by manual
banner_file=/etc/vsftpd/welcome.txt
eof
[ -e /etc/vsftpd/chroot_list ] || touch /etc/vsftpd/chroot_list
cat >>/etc/vsftpd/welcome.txt<
Welcome to sunny ftp
eof
#create ftp users without login system pemission
echo "If you don't want to create user ,you can enter number 0 to skip create new ftp user"
read -p "how many ftp users would you want to create(eg:3,means create 3 users total): " usernu
[[ "$usernu" =~ ^[0-9]+$? ]] || { echo your input is no num,user does not create,please check;$usernu=0; }
if [ -z ${usernu:-} ];then
usernu=0
fi
read -p "enter passwd for all new users(default:ftp123): " pass
if [ -z ${pass:-} ];then
pass=ftp123
fi
while [ "$usernu" -gt 0 ];do
read -p "pleas input new username you want to create: " user
if $(id "$user" &>/dev/null);then
echo "$user" is already exist,$user will not be create again
continue
else
useradd -s /sbin/nologin -d /var/ftp/"$user" "$user"
echo "$pass"| passwd --stdin "$user"
echo "$user" >> /etc/vsftpd/chroot_list
let usernu--
fi
done
echo "You will create two dir under /var/ftp for project"
echo "dir share : everyone can do create,delete,upload,download files after one minute once file is upload under share"
echo "dir share_project : it? is similar to dir share,but one can delete file under it"
read -p "If you want to make two share dir,enter yes,it will not create if your enter other: " answer
if [ "$answer" = "yes" ];then
[ -e /var/ftp/share ] || mkdir /var/ftp/share
[ -e /var/ftp/share_project ] || mkdir /var/ftp/share_project
grep "/ftp/share1" /etc/crontab &>/dev/null || echo "* * * * * root chmod -R 777 /var/ftp/share">>/etc/crontab
chattr +a /var/ftp/share_project
else
echo "no share dir was created"
fi
#both cent 6 and 7
chkconfig vsftpd on
service vsftpd restart &>/dev/null
#centOS7 can run below cmd to restart and enble service when start system as well
#systemctl restart vsftpd.service
#systemctl enable vsftpd
setenforce 0? &>/dev/null
#if you want to use selinux,your run cmd below to make ftp work normal
#getsebool -a | grep ftp
#setsebool -P tftp_home_dir=1
#setsebool -P tftp_anon_write=1
#setsebool -P ftpd_anon_write=1
netstat -netlp | grep vsftpd &>/dev/null && echo "Your ftp is working,your can use it now" || echo "Your ftp service does not start,Please check"
unset usernu
unset pass
unset time
4 總結(jié)
本文的一鍵安裝,用戶可以根據(jù)自己的需要決定是否創(chuàng)建新的ftp用戶,以及是否創(chuàng)建共享文件夾