ubuntu16.04搭建ftp服務器

  哎呀呀呀暈暈暈暈暈暈!先來嘆氣一波,花費了一天的時間css

  環境:虛擬機linux

  搭建緣由:虛擬機上掛載U盤失敗後就想到了使用ftp文件上傳下載,因而行動shell

  搭建過程:ubuntu安裝ftp很簡單:ubuntu

      

sudo apt-get update 

sudo apt-get install vsftpd

[vsftpd --version 檢測是否安裝]

 

     ftp配置是頗有學問的,必需要強調的是若是你的ftp服務沒法啓動,十有八九就是配置文件的配置錯誤windows

     配置參考以下:
瀏覽器

  

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
上面的配置文件中,我不是很熟悉,我挑一些我稍微知道的講。

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中進行。

測試 服務器

  使用filezilla等工具ide

  

如今在windows下打開cmd窗口,執行命令:工具

ftp [你的linux機器的ip地址]
  • 1

  linux下查看ip地址的命令爲:測試

ifconfig
  • 1

  執行完ftp命令後,若是鏈接成功,會讓你輸入用戶名和密碼,輸入咱們剛剛添加的用戶uftp的用戶名和密碼便可: 
  image_1b6e2jfpi1okq1obiiajsdg3ebm.png-18.4kB

  命令pwd和lcd分別能夠查看遠程linux下和本地windows下的當前目錄: 
   
  image_1b6e2lanng2bk8611i3ns21emb13.png-9.2kB 
   
  如今在linux下的目錄/home/uftp下新建文件test_ftp_download.txt,在windows下的家目錄(個人爲C:\Users\40696)下新建文件test_ftp_upload.txt,分別用來測試下載和上傳功能: 
  

下載文件使用命令:get [文件名] 上傳文件使用命令:put [文件名]
  • 1
  • 2

   
  image_1b6e2td69155h1cr71bsod5b14u91g.png-26.8kB

相關文章
相關標籤/搜索