FTP服务器的搭建与安全配置
FTP可以說是Internet上使用非常廣泛的一種通訊協(xié)議了。它工作在OSI模型的第7層,是TCP/IP的一種具體應(yīng)用。FTP采用基于TCP的可靠連接:監(jiān)聽21端口來等待控制連接請(qǐng)求,當(dāng)連接建立后,采用20號(hào)端口來建立數(shù)據(jù)傳輸通道。
FTP中使用的一些典型消息:
125 數(shù)據(jù)連接打開,傳輸開始
200 命令OK
331 用戶名OK,需要輸入密碼
425 不能打開數(shù)據(jù)連接
452 錯(cuò)誤寫文件
500 命令無法識(shí)別
?
Vsftp是Linux系統(tǒng)下的一套開源FTP服務(wù)器軟件,具有結(jié)構(gòu)簡單、性能優(yōu)良的特點(diǎn),是一款輕量型穩(wěn)定安全的FTP。
1.安裝Vsftp。(測(cè)試環(huán)境:ContOS 6.5)
查看本機(jī)有沒有vsftp安裝源:
~# rpm -qa | grep svftpd? 如果有,則會(huì)顯示vsftp的版本,這樣就可以直接安裝:
~# yum install vsftpd但我的上面貌似沒有,只能下載源碼編譯了。Vsftp官網(wǎng)主頁:http://vsftpd.beasts.org ,貌似進(jìn)不去,網(wǎng)上找了下,可以在這里下載:站長之家 。版本是Vsftp v2.3.2 For Linux。解壓編譯:
~# tar xvf vsftpd-2.3.2.tar.gz ~# cd ./vsftpd-2.3.2在此說明一下vsftpd-2.3.2目錄下的builddefs.h文件,該文件主要用來設(shè)定FTP服務(wù)器的一些安全配置:CTP_Wrappers、PAM和SSL??梢园葱枰x擇/取消?!?/span>
vim ./builddefs.h #ifndef VSF_BUILDDEFS_H #define VSF_BUILDDEFS_H#undef VSF_BUILD_TCPWRAPPERS //對(duì)TCP包進(jìn)行解析的工具,用于限制某種服務(wù)的訪問權(quán)限進(jìn)而達(dá)到保護(hù)系統(tǒng)的目的。 #define VSF_BUILD_PAM //一種高效便利的用戶級(jí)別的認(rèn)證方式,用來加強(qiáng)服務(wù)器的安全性能。 #undef VSF_BUILD_SSL //利用加密技術(shù),保障在傳輸過程中數(shù)據(jù)不會(huì)被竊聽。#endif /* VSF_BUILDDEFS_H */2.配置vsftpd.conf文件
vsftpd.conf文件是主配置文件,在/etc/vsftpd/目錄下.
# Allow anonymous FTP? (Beware - allowed by default if you comment this out). anonymous_enable=YES //設(shè)置是否允許匿名訪問 # # Uncomment this to allow local users to log in. local_enable=YES //設(shè)置是否允許本地用戶登錄 # # Uncomment this to enable any form of FTP write command. write_enable=YES //設(shè)置是否允許寫操作 # # Default umask for local users is 077. You may wish to change this to 022, # if your users expect that (022 is used by most other ftpd's) local_umask=022 //本地用戶操作權(quán)限 # # Uncomment this to allow the anonymous FTP user to upload files. This only # has an effect if the above global write enable is activated. Also, you will # obviously need to create a directory writable by the FTP user. #anon_upload_enable=YES //設(shè)置是否允許匿名上傳文件 # # Uncomment this if you want the anonymous FTP user to be able to create # new directories. #anon_mkdir_write_enable=YES //設(shè)置是否允許匿名建立目錄 # # Activate directory messages - messages given to remote users when they # go into a certain directory. dirmessage_enable=YES //設(shè)置是否在改變目錄后發(fā)送消息 # # The target log file can be vsftpd_log_file or xferlog_file. # This depends on setting xferlog_std_format parameter xferlog_enable=YES //設(shè)置是否激活日志功能 # # Make sure PORT transfer connections originate from port 20 (ftp-data). connect_from_port_20=YES //設(shè)置是否使用20端口傳輸數(shù)據(jù)(PORT模式) # # If you want, you can arrange for uploaded anonymous files to be owned by # a different user. Note! Using "root" for uploaded files is not # recommended! //修改匿名用戶上傳文件的所有者 #chown_uploads=YES #chown_username=whoever # # The name of log file when xferlog_enable=YES and xferlog_std_format=YES # WARNING - changing this filename affects /etc/logrotate.d/vsftpd.log #xferlog_file=/var/log/xferlog //設(shè)置日志文件保存位置 # # Switches between logging into vsftpd_log_file and xferlog_file files. # NO writes to vsftpd_log_file, YES to xferlog_file xferlog_std_format=YES //設(shè)置是否使用標(biāo)準(zhǔn)文件日志 # # You may change the default value for timing out an idle session. #idle_session_timeout=600 //設(shè)置會(huì)話的超時(shí)時(shí)間 # # You may change the default value for timing out a data connection. #data_connection_timeout=120 //設(shè)置數(shù)據(jù)傳輸?shù)某瑫r(shí)時(shí)間 # # It is recommended that you define on your system a unique user which the # ftp server can use as a totally isolated and unprivileged user. #nopriv_user=ftpsecure //設(shè)置非特權(quán)用戶賬戶 # # Enable this and the server will recognise asynchronous ABOR requests. Not # recommended for security (the code is non-trivial). Not enabling it, # however, may confuse older FTP clients. #async_abor_enable=YES //設(shè)置是否允許客戶端使用sync等命令 # # By default the server will pretend to allow ASCII mode but in fact ignore # the request. Turn on the below options to have the server actually do ASCII # mangling on files when in ASCII mode. # Beware that on some FTP servers, ASCII support allows a denial of service # attack (DoS) via the command "SIZE /big/file" in ASCII mode. vsftpd # predicted this attack and has always been safe, reporting the size of the # raw file. //設(shè)置是否以ASCII形式傳輸文件 # ASCII mangling is a horrible feature of the protocol. #ascii_upload_enable=YES #ascii_download_enable=YES # # You may fully customise the login banner string: #ftpd_banner=Welcome to blah FTP service. //設(shè)置登錄后的歡迎信息 # # You may specify a file of disallowed anonymous e-mail addresses. Apparently # useful for combatting certain DoS attacks. #deny_email_enable=YES //設(shè)置是否啟用禁止指定匿名用戶登錄 # (default follows) #banned_email_file=/etc/vsftpd/banned_emails //加入用戶列表(前提是上面已經(jīng)設(shè)置為YES) # # You may specify an explicit list of local users to chroot() to their home # directory. If chroot_local_user is YES, then this list becomes a list of # users to NOT chroot(). #chroot_local_user=YES //與下面配置有關(guān) #chroot_list_enable=YES //設(shè)置是否允許本地用戶離開其主目錄 # (default follows) # //如果使用該項(xiàng),則上面的chroot_local_user=YES需設(shè)置為NO。指定不能離開主目錄的用戶,將用戶名一個(gè)一行寫入指定文件。 #chroot_list_file=/etc/vsftpd/chroot_list # # You may activate the "-R" option to the builtin ls. This is disabled by # default to avoid remote users being able to cause excessive I/O on large # sites. However, some broken FTP clients such as "ncftp" and "mirror" assume # the presence of the "-R" option, so there is a strong case for enabling it. #ls_recurse_enable=YES // # # When "listen" directive is enabled, vsftpd runs in standalone mode and # listens on IPv4 sockets. This directive cannot be used in conjunction # with the listen_ipv6 directive. listen=YES //設(shè)置是否開啟IPV4監(jiān)聽 # # This directive enables listening on IPv6 sockets. To listen on IPv4 and IPv6 # sockets, you must run two copies of vsftpd with two configuration files. # Make sure, that one of the listen options is commented !! #listen_ipv6=YES //設(shè)置是否開啟IPV6監(jiān)聽pam_service_name=vsftpd //設(shè)置訪問所使用的PAM模塊 userlist_enable=YES //如果激活該選項(xiàng),將禁止所指定文件中的用戶登錄 tcp_wrappers=YES //設(shè)置是否使用TCP_Wrappers作為主機(jī)訪問控制方式? 3.配置ftpusers文件
?? ftpusers文件用于限定系統(tǒng)中那些用戶能/不能使用FTP服務(wù)??梢愿鶕?jù)實(shí)際情況添加/刪除。
#User that are not allowed to login via ftp root ... ... ...?4.配置user_list文件
user_list文件指定的用戶能否訪問FTP服務(wù)器取決于userlist_deny選項(xiàng)的設(shè)置。默認(rèn)為YES,即禁止user_list文件的用戶訪問FTP服務(wù)器,這與ftpusers文件相似。但是如果設(shè)置為NO,則完全相反,即只允許該文件列表里的用戶訪問該FTP服務(wù)器。
# vsftpd userlist # If userlist_deny=NO,only allow users in this file # If userlist_deny=YES(default),never allow users in this file,and do not even prompt for a password. # ... # ... root ...如果要限制指定的本地用戶(即user_list文件的用戶)不能訪問FTP服務(wù)器,可以相應(yīng)地修改vsftpd.conf文件:
userlist_enable=YES userlist_deny=YES userlist_file=/etc/vsftpd/user_list?同樣,如果要限制指定的本地用戶(即user_list文件的用戶)能夠訪問FTP服務(wù)器,而其他的本地用戶不能訪問,可以相應(yīng)地修改vsftpd.conf文件:
userlist_enable=YES userlist_deny=NO userlist_file=/etc/vsftpd/user_list5.配置允許匿名用戶使用FTP服務(wù)器
創(chuàng)建用戶ftp-anon和目錄/var/ftp-pub:
mkdir /var/ftp-pub useradd -d /var/ftp-pub ftp-anon //-d 指定用戶的主文件目錄,vsftpd默認(rèn)登錄成功后的目錄是該用戶的home目錄。作為匿名訪問,/var/ftp-pub不應(yīng)該屬于用戶ftp-anon,也不應(yīng)該有寫權(quán)限,所以可以用以下方法修改其權(quán)限:
~# chown root.root /var/ftp-pub ~# og-w -d /var/ftp-pub?修改vsftpd.conf文件:
anonymous_enable=YES //設(shè)置允許匿名訪問(默認(rèn)開啟) //設(shè)置允許匿名上傳和創(chuàng)建目錄(非必須,慎用!) anon_upload_enable=YES anon_mkdir_write_enable=YES?6.虛擬用戶使用Vsftp服務(wù)器
上面的(包括一般的)ftp訪問都是通過建立系統(tǒng)帳號(hào)來訪問服務(wù)器的,這樣很不安全,如果權(quán)限配置錯(cuò)誤將會(huì)使服務(wù)器受到威脅。但是,通過建立虛擬FTP賬戶(與系統(tǒng)帳號(hào)分離),就可以大大增強(qiáng)系統(tǒng)的安全性。虛擬FTP賬戶只能用于文件傳輸,也叫g(shù)uest用戶。它是把用戶名/密碼保存起來,再驗(yàn)證的。所以Vsftp需要一個(gè)系統(tǒng)用戶的身份來讀取數(shù)據(jù)(用戶名/密碼)文件,即guest用戶,用以映射虛擬用戶。
具體配置如下:
(1)生成虛擬用戶口令庫文件。以下為例:
~# vim login.txt zhangsan //username1 hehe //passwd1 lisi //username2 mimanicai //passwd2 ......(2)配置生成Vsftp的認(rèn)證文件
保存并退出,使用db_load命令生成口令庫文件:
~# db_load -T -t hash -f login.txt /etc/vsftpd/vsftpd_login.db修改口令庫文件的權(quán)限:
chmod 600 /etc/vsftpd/vsftpd_login.db?編輯虛擬用戶所需的PAM配置文件
~# vim /etc/pam.d/vsftpd //加入以下兩行: auth required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login.db account required /lib/security/pam_userdb.so db=/etc/vsftpd/vsftpd_login.db?(3)建立虛擬用戶訪問的目錄,并設(shè)置相應(yīng)的訪問權(quán)限
~# useradd -d /home/ftp virtual ~# chmod 700 /home/ftp?(4)建立配置文件
~# cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.bak ~# vim /etc/vsftpd/vsftpd.conf //配置如下: anonymous_enable=YES local_enable=YES write_enable=YES anon_upload_enable=NO anon_mkdir_write_enable=NO listen=YES guest_enable=YES guest_username=virtual(5)重啟Vsftp服務(wù)器
~# service vsftpd restart?至此,虛擬用戶使用Vsftp服務(wù)器配置將完成了??梢允褂胠ogin.txt里的用戶帳號(hào)登錄FTP服務(wù)器了。
7.改變Vsftp的端口號(hào)
在vsftpd.conf文件里加入如下,并重啟vsftp。
~# listen_port=2121?8.配置Vstfp服務(wù)器的chroot
chroot就是為登錄用戶指定一個(gè)固定的目錄,這個(gè)目錄一般是用戶的主目錄,用戶被限制在該目錄下,類似與WEB服務(wù)器的虛擬目錄,從而保護(hù)了系統(tǒng)安全。
設(shè)置指定的用戶執(zhí)行chroot:
chroot_local_user=NO chroot_list_enable=YES chroot_list_file=/etc/vsftpd/chroot_list相關(guān)參考文章: http://wiki.ubuntu.org.cn/Vsftpd
轉(zhuǎn)載于:https://www.cnblogs.com/lingerhk/p/4008023.html
總結(jié)
以上是生活随笔為你收集整理的FTP服务器的搭建与安全配置的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 检讨程序猿
- 下一篇: 使用无锁的方式和有锁的方式的程序性能对比