原址:How to Install and Configure FTP Server in Ubuntu
FTP(File Transfer Protocol 文件傳輸協(xié)議 )是通過網(wǎng)絡在兩臺電腦之間傳輸文件的協(xié)議,這是一種很古老但是很常用的協(xié)議。它認證所用的用戶名跟密碼是通過明文在網(wǎng)絡上傳輸?shù)?,因而先天就是不安全的?/b>
警告:如果你計劃使用FTP,請考慮使用SSL/TLS配置FTP連接,否則,做好使用安全的FTP,如SFTP。
在這篇文章中,我們將介紹如何在Ubuntu中去安裝 配置并加固FTP服務器(在文章中我們使用 VsFTP,一個號稱非常安全的FTP服務器軟件),以去應對FTP漏洞。
Step 1.安裝VsFTP
1.首先,升級Ubuntu系統(tǒng)并安裝VsFTP
sudo apt-get update
sudo apt-get install -y vsftpd
2.安裝完成后,啟動VsFTP,并設置開機自啟動。
------------- On SystemD -------------
systemctl start vsftpd
systemctl enable vsftpd
------------- On SysVInit -------------
service vsftpd start
chkconfig --level 35 vsftpd on
3.如果,你在服務器上開啟了 UFW firewall 的話,還要開放 21 和 20 端口,來允許遠程計算機的TCP連接,然后添加新的防火墻規(guī)則,
sudo ufw allow 20/tcp
sudo ufw allow 21/tcp
sudo ufw status
Step 2.配置Vsftp
4.首先,創(chuàng)建原始配置文件/etc/vsftpd.conf 的備份
sudo cp /etc/vsftpd.conf /etc/vsftpd.conf.orig
然后,打開配置文件
sudo vi /etc/vsftpd.conf
OR
sudo nano /etc/vsftpd.conf
添加/修改以下選項
anonymous_enable=NO? ? ? ? ? ? # disable? anonymous login
local_enable=YES ? ? ? ? ? ? ? ? ? ? ?# permit local logins
write_enable=YES ? ? ? ? ? ? ? ? ? ? # enable FTP commands which change the filesystem
local_umask=022 ? ? ? ? ? ? ? ? ? ? ? # value of umask for file creation for local users
dirmessage_enable=YES ? ? ? ? ?# enable showing of messages when users first enter a new directory
xferlog_enable=YES ? ? ? ? ? ? ? ? ?# a log file will be maintained detailing uploads and downloads
connect_from_port_20=YES? ? ? ? # use port 20 (ftp-data) on the server machine for PORT style connections
xferlog_std_format=YES ? ? ? ? ? ? # keep standard log file format
listen=NO ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?# prevent vsftpd from running in standalone mode
listen_ipv6=YES ? ? ? ? ? ? ? ? ? ? ? ? ? # vsftpd will listen on an IPv6 socket instead of an IPv4 one
pam_service_name=vsftpd? ? ? ? # name of the PAM service vsftpd will use
userlist_enable=YES ? ? ? ? ? ? ? ? # enable vsftpd to load a list of usernames
tcp_wrappers=YES ? ? ? ? ? ? ? ? ? # turn on tcp wrappers
5.配置?/etc/vsftpd.userlist(注:在我的系統(tǒng)中默認是 /etc/vsftpd.user_list 文件且不存在,需要自己創(chuàng)建)?去允許/阻止FTP用戶登陸
請注意,默認情況下,如果 userlist_enable = YES,userlist_deny = YES,則在 userlist_file = /etc/vsftpd.userlist 中列出的用戶將被拒絕登陸。
但是,userlist_deny = NO 時 userlist_file = /etc/vsftpd.userlist 中的用戶會允許登錄FTP服務器。
userlist_enable=YES? ? ? ? ? ? ? ? ? # vsftpd將從userlist_file給出的文件名中加載用戶名列表
userlist_file=/etc/vsftpd.userlist? ? # 給出文件名列表的存儲文件(注:若不配置,則默認為 /etc/vsftpp.user_list)
userlist_deny=NO
注意:當用戶登錄到FTP服務器時,由于開啟了根目錄限制,用戶只能瀏覽他們的本地根目錄,而不能跳轉到其他目錄。
接下來,我們將修改根目錄限制。
6.我們添加/修改/取消注釋以下兩個選項,將FTP用戶限制到其主目錄。
chroot_local_user=YES
allow_writeable_chroot=YES
chroot_local_user = YES 意味著本地用戶將被限制在根目錄下,默認情況下登錄在它們各自的主目錄下。
默認情況下出于安全的考慮,VsFTP是不允許根目錄可寫的,但是我們可以通過 allow_writeable_chroot=YES 來使其可寫。
保存并關閉配置文件,重啟VsFTP服務器使上述改變生效
------------- On SystemD -------------
# systemctl restart vsftpd
------------- On SysVInit -------------
# service vsftpd restart
Step 3:測試VsFTP服務器
7.現(xiàn)在我們將通過使用 useradd 命令創(chuàng)建FTP用戶來測試FTP服務器,如下所示:
sudo useradd -m -c "Aaron Kili, Contributor" -s /bin/bash aaronkilik
sudo passwd aaronkilik
然后,我們必須使用echo命令和tee命令在/etc/vsftpd.userlist文件中顯式地列出用戶aaronkilik,如下所示:
echo "aaronkilik" | sudo tee -a /etc/vsftpd.userlist
cat /etc/vsftpd.userlist
8.現(xiàn)在測試我們的服務器是否按上述要求配置工作。我們先測試匿名登錄;我們可以從下面的輸出中清楚地看到,不允許在FTP服務器上進行匿名登錄:
# ftp 192.168.56.102 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Connected to 192.168.56.102? (192.168.56.102). ?
220 Welcome to TecMint.com FTP service.
Name (192.168.56.102:aaronkilik) : anonymous
530 Permission denied.
Login failed.
ftp> bye
221 Goodbye.
9.接下來,讓我們測試未在文件/etc/vsftpd.userlist中列出的用戶是否可以登錄到服務器。從以下輸出中可以看出這是不允許的:
# ftp 192.168.56.102 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Connected to 192.168.56.102? (192.168.56.102).
220 Welcome to TecMint.com FTP service.
Name (192.168.56.10:root) : user
1530 Permission denied.
Login failed.
ftp> bye
221 Goodbye
10.現(xiàn)在,我們將進行最后的測試,以確定在/etc/vsftpd.userlist文件中列出的用戶是否可以成功登錄并登錄到主目錄下。
# ftp 192.168.56.102 ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Connected to 192.168.56.102? (192.168.56.102). ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
220 Welcome to TecMint.com FTP service. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Name (192.168.56.102:aaronkilik) : aaronkilik ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
331 Please specify the password. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Password:
230 Login successful. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Remote system type is UNIX. ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
Using binary mode to transfer files.
ftp>ls

警告:設置選項 allow_writeable_chroot = YES 是比較危險,它可能有安全隱患,特別是如果用戶有上傳權限,shell訪問權限等。只有你完完全全的知道你在做什么時,才能授予此權限。
這些安全隱患不僅僅只存在于 VSFTPD 進程中,它們也可以影響其他所有的,將用戶限制于根目錄中的FTP守護進程。
因為這個原因,在下面的部分,我們將介紹一個更安全的方法,為用戶設置不同的不可寫的本地根目錄。
Step 4:配置 FTP 用戶家目錄
11.現(xiàn)在再次打開 VsFTP 配置文件
sudo vi /etc/vsftpd.conf
OR
sudo nano /etc/vsftpd.conf
使用#字符注釋掉不安全的選項,如下所示:
#allow_writeable_chroot=YES
接下來,為用戶創(chuàng)建備用本地根目錄(aaronkilik,你的目錄可能不相同),并通過禁用其他用戶對此目錄的寫入權限來設置我們所需的權限:
sudo mkdir /home/aaronkilik/ftp
sudo chown nobody:nogroup /home/aaronkilik/ftp
sudo chmod a-w /home/aaronkilik/ftp
12.然后,在本地目錄下創(chuàng)建一個新目錄,并賦予用戶適當權限:
sudo mkdir /home/aaronkilik/ftp/files
sudo chown -R aaronkilk:aaronkilik /home/aaronkilik/ftp/files
sudo chmod -R 0770 /home/aaronkilik/ftp/files/
然后,在 VsFTPD 配置文件中添加/修改以下選項:
user_sub_token=$USER? ? ? ? ? # 將用戶名插入本地根目錄
local_root=/home/$USER/ftp? ? # 定義任何用戶的本地根目錄
保存并關閉配置文件,重啟 VsFTP 使上述配置生效
------------- On SystemD ------------- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
# systemctl restart vsftpd ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ?
------------- On SysVInit ------------- ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ??
# service vsftpd restart
13.現(xiàn)在,讓我們進行最終的檢查,確保用戶登錄后的根目錄是我們在其主目錄中創(chuàng)建的FTP文件夾
# ftp 192.168.56.102
Connected to 192.168.56.102? (192.168.56.102).
220 Welcome to TecMint.com FTP service.
Name (192.168.56.10:aaronkilik) : aaronkilik
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp>ls

PS:翻譯的不好,英語四級剛過,見諒見諒!各位就湊合看吧,應該不影響操作吧!