FTP服務器快速搭建

FTP服務器快速搭建

1. 安裝vsftpd服務

sudo apt-get install vsftpd

2. 配置ftp

2.1 編寫配置文件

配置文件位置/etc/vsftpd.conf

#這些設置系統默認是開啓的,可以不管
listen=NO
listen_ipv6=YES
dirmessage_enable=YES
use_localtime=YES
xferlog_enable=YES
connect_from_port_20=YES
#下面的就要自定義設置了,建議系統默認的不管,然後複製下面的

#是否允許匿名訪問,NO爲不允許
anonymous_enable=NO
#是否允許本地用戶訪問,就是linux本機中存在的用戶,YES允許
local_enable=YES
#是否開啓寫模式,YES爲開啓
write_enable=YES
#新建文件權限,一般設置爲022,那麼新建後的文件的權限就是777-022=755
local_umask=022

#是否啓動userlist爲通過模式,YES的話只有存在於userlist文件中的用戶才能登錄ftp(可以理解爲userlist是一個白名單),NO的話,白名單失效,和下面一個參數配合使用
userlist_enable=YES
#是否啓動userlist爲禁止模式,YES表示在userlist中的用戶禁止登錄ftp(黑名單),NO表示黑名單失效,我們已經讓userlist作爲一個白名單,所以無需使用黑名單功能
userlist_deny=NO
#指定哪個文件作爲userlist文件,我們稍後編輯這個文件
userlist_file=/etc/vsftpd.user_list

#是否限制本地所有用戶切換根目錄的權限,YES爲開啓限制,即登錄後的用戶不能訪問ftp根目錄以外的目錄,當然要限制啦
chroot_local_user=YES
#是否啓動限制用戶的名單list爲允許模式,上面的YES限制了所有用戶,可以用這個名單作爲白名單,作爲例外允許訪問ftp根目錄以外
chroot_list_enable=YES
#設置哪個文件是list文件,裏面的用戶將不受限制的去訪問ftp根目錄以外的目錄
chroot_list_file=/etc/vsftpd.chroot_list
#是否開啓寫模式,開啓後可以進行創建文件夾等寫入操作
allow_writeable_chroot=YES
#設置ftp根目錄的位置,這個文件我們稍後自己創建
local_root=/opt/ftp_root
# set port
listen_port=8088

#開啓被動模式
pasv_enable=YES
#FTP服務器公網IP
pasv_address=49.65.2.38
#設置被動模式下,建立數據傳輸可使用port範圍的最小值
pasv_min_port=8000
#設置被動模式下,建立數據傳輸可使用port範圍的最大值
pasv_max_port=8003

2.2 創建ftp專屬用戶和文件夾

mkdir /opt/ftp_root
sudo useradd -d /opt/ftp_root ftpuser
sudo passwd ftpuser
sudo chown ftpuser /opt/ftp_root/

2.3 創建白名單文件

創建我們在配置裏寫好的/etc/vsftpd.user_list文件和/etc/vsftpd.chroot_list文件,並且添加可以用於登陸的用戶名

vim /etc/vsftpd.user_list
vim /etc/vsftpd.chroot_list

3. 啓動、調試

3.1 啓動ftp服務器

sudo /etc/init.d/vsftpd start

3.2 客戶端登陸ftp

ftp 49.65.2.38 8088

然後輸入配置好的用戶名和密碼,即可登陸成功

3.3 ftp的url

ftp的url組裝格式如下

ftp://userName:password@url/path/fileName

按本次舉例

ftp://ftpuser:[email protected]/

3.3 問題的解決方式

3.3.1 530 Login incorrect

如果遇到登陸後出現

[hyperchain@puer-prod ~]$ ftp 49.65.2.38 8088
Connected to 49.65.2.38 (49.65.2.38).
220 (vsFTPd 3.0.3)
Name (49.65.2.38:hyperchain): ftpuser
331 Please specify the password.
Password:
530 Login incorrect.
Login failed.

進入到以下文件

vim /etc/pam.d/vsftpd

註釋掉下面這一行

auth required pam_shells.so

然後重啓ftp服務

3.3.2 500 Illegal PORT command.

如果登陸成功後,使用主動模式後出現

ftp> ls
500 Illegal PORT command.
ftp: bind: Address already in use

這是由於主動模式需要服務器連接客戶端的端口。如果服務器和客戶端不在同一局域網下(比如服務器在公網客戶端在局域網)就會出現這個問題,服務端無法連接客戶端的端口,只能切換到被動模式。在ftp命令行使用如下命令可以進行主動被動模式切換

ftp> passive mode
3.3.3 ftp: connect: Connection refused

如果在被動模式下出現以下問題

ftp> ls
227 Entering Passive Mode (0,0,0,0,31,67).
ftp: connect: Connection refused

可能是配置文件中pasv_address字段的問題,可以註釋掉這個字段,再重啓ftp服務器

附錄

1. 原配置文件備份

# Example config file /etc/vsftpd.conf
#
# The default compiled in settings are fairly paranoid. This sample file
# loosens things up a bit, to make the ftp daemon more usable.
# Please see vsftpd.conf.5 for all compiled in defaults.
#
# READ THIS: This example file is NOT an exhaustive list of vsftpd options.
# Please read the vsftpd.conf.5 manual page to get a full idea of vsftpd's
# capabilities.
#
#
# Run standalone?  vsftpd can run either from an inetd or as a standalone
# daemon started from an initscript.
listen=NO
#
# This directive enables listening on IPv6 sockets. By default, listening
# on the IPv6 "any" address (::) will accept connections from both IPv6
# and IPv4 clients. It is not necessary to listen on *both* IPv4 and IPv6
# sockets. If you want that (perhaps because you want to listen on specific
# addresses) then you must run two copies of vsftpd with two configuration
# files.
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
#
# 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
#
# Uncomment this if you want the anonymous FTP user to be able to create
# new directories.
#anon_mkdir_write_enable=YES
#
# 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
#
# 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
#
# 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 change the default value for timing out an idle session.
#idle_session_timeout=600
#
# You may change the default value for timing out a data connection.
#data_connection_timeout=120
#
# 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
#
# 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
#
# 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.
# 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.
#
# You may specify a file of disallowed anonymous e-mail addresses. Apparently
# useful for combatting certain DoS attacks.
#deny_email_enable=YES
# (default follows)
#banned_email_file=/etc/vsftpd.banned_emails
#
# You may restrict local users to their home directories.  See the FAQ for
# the possible risks in this before using chroot_local_user or
# chroot_list_enable below.
#chroot_local_user=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().
# (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
#
# 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
#
# Customization
#
# Some of vsftpd's settings don't fit the filesystem layout by
# default.
#
# 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
#
# 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
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章