Author:Liedra
https://www.cnblogs.com/LieDra/
一、前言
網絡安全實驗五中需要搭建FTP服務器進行實驗,在踩了一些坑之后,記錄下搭建和配置過程,希望其他人可以少走彎路。
本文中Linux下安裝和配置FTP在VMware中Ubuntu 16下進行,對於CentOS等其他Linux系統不適用,其他系統的安裝需要參照其他文章。
同時注意權限問題,有的命令需要sudo [原命令]才可執行,因此這里基本上都用了sudo。
下面是正文
二、基本安裝
1.安裝ftp服務器
分別執行下列命令
sudo apt-get update
sudo apt-get install vsftpd
#這里遇到了問題1,解決方案在最下面。
之后可以利用命令vsftpd --version 檢測是否安裝已經安裝后的版本。
2.配置ftp服務器
(1)配置文件修改之前先備份,系列命令進行備份。
sudo cp /etc/vsftpd.conf /etc/vsftpd_bk.conf
(2)修改配置文件
可以使用vim,也可以使用gedit進入修改,本人感覺gedit操作相對簡單,因此這里使用的gedit.(下面命令2選1)
使用sudo vim /etc/vsftpd.conf或者 sudo gedit /etc/vsftpd.conf
(3)配置文件內容
我這里的配置文件vsftpd.conf中內容如下(對於更深入的要求,可在之后修改配置文件),此處只配置簡單的情形。
listen=NO
listen_ipv6=YES
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=NO
# Uncomment this to allow local users to log in.
local_enable=YES
# Uncomment this to enable any form of FTP write command.
write_enable=YES
# 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
# Activate directory messages - messages given to remote users when they
# go into a certain directory.
dirmessage_enable=YES
#
# If enabled, vsftpd will display directory listings with the time
# in your local time zone. The default is to display GMT. The
# times returned by the MDTM FTP command are also affected by this
# option.
use_localtime=YES
#
# Activate logging of uploads/downloads.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
# You may override where the log file goes if you like. The default is shown
# below.
xferlog_file=/var/log/vsftpd.log
#
# If you want, you can have your log file in standard ftpd xferlog format.
# Note that the default log file location is /var/log/xferlog in this case.
xferlog_std_format=YES
# You may fully customise the login banner string:
ftpd_banner=Welcome to FTP service.
# 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().
# (Warning! chroot'ing can be very dangerous. If using chroot, make sure that
# the user does not have write access to the top level directory within the
# chroot)
chroot_local_user=YES
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
# This option should be the name of a directory which is empty. Also, the
# directory should not be writable by the ftp user. This directory is used
# as a secure chroot() jail at times vsftpd does not require filesystem
# access.
secure_chroot_dir=/var/run/vsftpd/empty
#
# This string is the name of the PAM service vsftpd will use.
# pam_service_name=vsftpd
pam_service_name=ftp
# This option specifies the location of the RSA certificate to use for SSL
# encrypted connections.
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key
ssl_enable=NO
#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
utf8_filesystem=YES
注意Ctrl+s保存。
對於終端中的警告暫時可以忽視(一般問題不大)。
三、添加用戶
這里以添加用戶t1為例,其他用戶名同理可以添加。
1.先在/home下創建一個用戶名目錄
sudo mkdir /home/t1
2.接着綁定用戶登錄目錄和shell
命令中-d 后面指定用戶登入時的目錄,-s指定用戶登入后使用的shell
sudo useradd -d /home/t1 -s /bin/bash t1
3.添加用戶t1的密碼
sudo passwd t1
之后密碼自己設置
4.設置/home/t1的擁有者為t1
sudo chown t1:t1 /home/t1
5.添加用戶到/etc/vsftpd.user_list中
在下面命令打開的文件中添加一行,內容為t1(即你新建的用戶的用戶名),保存后退出。
sudo gedit /etc/vsftpd.user_list
文件中內容如下圖

6.添加用戶到/etc/vsftpd.chroot_list中,新增行內容也為t1,與上一步操作類似。
sudo gedit /etc/vsftpd.chroot_list
最后嘗試重啟服務
systemctl restart vsftpd
基本配置完成
四、測試
1.Ubuntu本地測試ftp localhost

2.Windows下測試:
(1)cmd中: ftp IP地址

(2)地址欄
也可以在地址欄ftp:IP地址或ftp://IP地址/進行查看。

輸入正確的用戶名和密碼后出現

文件夾為空,因為我們還沒有創建文件。
3.Ubuntu新建文件后測試:
在Ubuntu中新建一個文件
cd /home/t1
sudo gedit test.txt
輸入下列內容后退出

然后在windows下查看

打開txt得到

成功搭建了簡單的FTP服務器
五、遇到的問題
1.sudo apt-get update之后的命令出現下列問題。
E: 無法獲得鎖 /var/lib/dpkg/lock-frontend - open (11: 資源暫時不可用)
E: 無法獲取 dpkg 前端鎖 (/var/lib/dpkg/lock-frontend),是否有其他進程正占用它?
解決:
sudo rm /var/cache/apt/archives/lock
sudo rm /var/lib/dpkg/lock-frontend
之后如果:
E: 無法獲得鎖 /var/lib/dpkg/lock - open (11: 資源暫時不可用)
E: 無法鎖定管理目錄(/var/lib/dpkg/),是否有其他進程正占用它?
則:
sudo rm /var/lib/dpkg/lock
整體過程如下,成功解決:

