天天看點

Ubuntu Server 16.04.1 LTS 64位使用vsftpd搭建ftp伺服器

最近給自己的伺服器用vsftpd搭了個ftp伺服器。

最好先

sudo su

獲得root權限。

sudo apt-get install vsftpd

安裝vsftpd

修改/etc/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=

# 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
           

上面的配置檔案中,我不是很熟悉,我挑一些我稍微知道的講。

  • listen=NO

    不了解,我檢視了很多部落格,這個設定的是YES,但是我設定成YES,登陸直接被拒絕。
  • anonymous_enable=NO

    拒絕匿名登陸
  • write_enable=YES

    設定可以上傳檔案,這個設定看需要個人需要
  • xferlog_enable=YES

    開啟日志記錄
  • xferlog_file=/var/log/vsftpd.log

    設定日志檔案路徑
  • xferlog_std_format=YES

    設定日志格式為标準輸出
  • connect_from_port_20=YES

    綁定20端口
  • ftpd_banner=Welcome to FTP service.

    歡迎語句,在使用shell時可以看到

下面這幾個的設定比較重要:

  • chroot_local_user=YES

  • chroot_list_enable=YES

  • chroot_list_file=/etc/vsftpd.chroot_list

    上面的這幾個配置實作的功能是:使用者被限制在自己的主目錄下。使用者名單來源于

    /etc/vsftpd.chroot_list

    具體可以參考:http://blog.csdn.net/bluishglc/article/details/42398811

一個重要的配置

  • pam_service_name=ftp

    原配置中為vsftpd,ubuntu使用者需要更改成ftp

關于編碼:

  • utf8_filesystem=YES

    不知道這項有沒有起作用,上傳的檔案不亂碼,用浏覽器打開是亂碼(浏覽器編碼問題?),使用windows自帶的檔案資料總管是沒有亂碼的,使用filezila亂碼。

建立使用者:

  • mkdir /home/username

  • sudo useradd username -g ftp -d /home/username -m username

  • sudo passwd username's password

  • mkdir /home/username/pub

  • chmod 777 -R /home/username/pub

    建立一個pub目錄用于存放檔案,并且賦予全部通路權限
  • usermod -s /sbin/nologin username

    限制使用者username隻能通過ftp登陸,而不能直接登陸伺服器

重要的一點:

  • 建立

    /etc/vsftpd.chroot_list

    将username放進去

啟動vsftpd或者重新開機

  • systemctl start vsftpd

    或者

    service vsftpd start

  • systemctl restart vsftpd

    或者

    service vsftpd restart

登陸:

  • 在windows的檔案資料總管或者在浏覽器中打開

    ftp://your_server_ip

    輸入賬号密碼,即可用登陸。浏覽器中隻能檢視,檔案操作如建立等需要在window的檔案資料總管中或者filezila中進行。

水準有限,有問題歡迎留言