ubuntu搭建ftp和vsftpd.conf详解

ubuntu搭建ftp

1、简介

FTP(File Transfer Protocol)文件传输协议

​ 基于客户端/服务器模式,默认使用20、21端口号,其中端口20(数据端口)用于 进行数据传输,端口21(命令端口)用于接受客户端发出的相关FTP命令与参数。FTP服 务器普遍部署于局域网中,具有容易搭建、方便管理的特点。而且有些FTP客户端工具 还可以支持文件的多点下载以及断点续传技术,因此FTP服务得到了广大用户的青睐。

FTP协议有以下两种工作模式:

主动模式(PORT):FTP服务器主动向客户端发起连接请求。

被动模式(PASV):FTP服务器等待客户端发起连接请求(FTP的默认工作模式)。 vsftpd是一款运行在Linux操作系统上的FTP服务程序,具有很高的安全性和传输速度。

2、安装vsftpd

在使用FTP前必须先安装FTP,ubuntu下安装:apt-get install vsftpd 

3、配置vsftpd.conf文件

# Example config file /etc/vsftpd.conf # 客户端连接服务器后显示的欢迎信息 #ftpd_banner=Welcome to ftp service #远程客户端响应端口数据连接超时时间, 单位为秒, 默认 60 #connect_timeout= #空闲的数据连接超时时间, 单位为秒, 默认 120 #accept_connection_timeout= #空闲的用户会话超时时间, 单位为秒, 默认 300 #data_connection_timeout= #在独立模式运行时, 最大连接数, 0 表示无限制 
#max_clients= #独立模式运行时, 每 IP 的最大连接数, 0表示无限制 
#max_per_ip= #是否允许虚拟用户登录 
#guest_enable=YES #限制远程的客户机连接后,所建立的控制连接,在多长时间没有做任何的操作就会中断(秒) #idle_session_timeout=60

#设置ftp服务来监听的地址,客户端可以用哪个地址来连接 
#listen_address=IP地址 #设置FTP工作的端口号,默认的为21 
#listen_port=端口号

#设置切换到目录时显示.message隐含文件的内容 
dirmessage_enable=YES 
use_localtime=YES #激活上传和下载日志 xferlog_enable=YES
#启用FTP数据端口连接 connect_from_port_20=YES
secure_chroot_dir=/var/run/vsftpd/empty #设置PAM认证服务的配置文件名称, 该文件存放在/etc/pam.d目录下 pam_service_name=vsftpd
 
 
rsa_cert_file=/etc/ssl/certs/ssl-cert-snakeoil.pem rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key ssl_enable=NO
 
 
#YES: 服务以独立运行方式运行; NO: 运行在 xinetd 内。 默认为 YES listen=YES 
#是否允许本地用户登录 
local_enable=YES 
#是否允许本地用户上传 
write_enable=YES 
#本地用户上传的文件的生成掩码, 默认为077 
#local_umask=077
#本地用户最大的传输速率, 单位为 Byte/s,值为0表示不限制 
#local_max_rate=0
 
 
#是否允许匿名ftp,如否则选择NO 
anonymous_enable=YES 
#no_anon_password=YES 
#匿名用户是否能上传 
#anon_upload_enable=YES 
#匿名用户是否能创建目录 
#anon_mkdir_write_enable=YES 
#修改文件名和删除文件
#anon_other_write_enable=YES 
#匿名用户上传的文件的生成掩码, 默认为077 
#anon_umask=022 
#匿名用户的最大传输速率, 单位为 Byte/s, 值为 0 表示不限制 
#anon_max_rate=0 
#是否允许匿名用户只读浏览 
anon_world_readable_only=YES
 
 
#userlist_enable=YES 
#userlist_deny=NO 
#userlist_file=/etc/vsftpd.user_list
#userlist_enable和userlist_deny两个选项联合起来针对的是:本地全体用户(除去ftpusers中的用户)和出现在user_list 文件中的用户以及不在在user_list #文件中的用户这三类用户集合进行的设置。
 
 
#当且仅当userlist_enable=YES时:userlist_deny项的配置才有效,user_list文件才会被使用;当其为NO时,无论 userlist_deny项为何值都是无效的,本地全 #体用户(除去ftpusers中的用户)都可以登入FTP

#当userlist_enable=YES时,userlist_deny=YES时:user_list是一个黑名单,即:所有出现在名单中的用户都会被拒绝登 入;

#当userlist_enable=YES时,userlist_deny=NO时:user_list是一个白名单,即:只有出现在名单中的用户才会被准许登入 (user_list之外的用户都被拒绝登入 #);另外需要特别提醒的是:使用白名单后,匿名用户将无法登入!除非显式在user_list中加入一行:anonymous
 
 
chroot_local_user=YES 
chroot_list_enable=YES 
chroot_list_file=/etc/vsftpd.chroot_list #chroot_local_user=YES 时,chroot_list_enable=YES时, 所有用户都可以ftp连接,限制在主目录下面。 #启用chroot_list_file里面的用户名单,作为例外,不限制访问范围
 
#chroot_local_user=NO 时,chroot_list_enable=YES 时,  所有用户都可以ftp连接,不限制范围。 #启用chroot_list_file里面的用户名单,作为例外,限制访问范围在主目录
 
#chroot_local_user=NO 时,chroot_list_enable=NO 时,   所有用户可以ftp连接,不限制范围。 #不启用 chroot_list_file里面的用户名单,没有例外
 
#chroot_local_user =YES 时,chroot_list_enable=NO 时,   所有用户可以ftp连接,限制在主目录下 #不启用chroot_list_file里面的用户名单,没有例外
 
#本地用户登陆后的目录,默认为本地用户的主目录 
#local_root=/srv/ftp/
allow_writeable_chroot=YES
 
#开启被动模式 pasv_enable=YES 
#PASV模式最小端口 
pasv_min_port=40000 
#PASV模式最大端口 
pasv_max_port=45000
黑名单文件:ftpusers,里面的用户禁止使用ftp服务,其中每个用户占一行,系统为 了防止权限过高的用户登录ftp服务器,随意篡改数据,将很多高权限列入了黑名单, 如root用户列入其中。

4、重启服务

systemctl restart vsftpd

配置文件日志信息可在/var/log/vsftpd.log和syslog下查看,如开启ftp服务出错可根 据日志排查

5、访问

  • 匿名访问
    在上述配置文件中,采用的是匿名访问方式,用户登录可以不需要进行用户名密码验 证,如果在命令行界面操作需要验证,默认用户名为ftp,密码直接回车,在Linux的用 户配置信息/etc/passwd中,可以看到当匿名访问FTP时,访问的FTP目录为/srv/ftp。
    上面图片是是添加的两个普通用户
    匿名登录下只能访问/srv/ftp目录,如果需要更改ftp用户目录,可以强制修 改/etc/passwd中的对应用户,也可以使用usermod命令进行更改,再重启FTP服务。
    测试:
Connected to 59.110.42.24.
220 (vsFTPd 3.0.3)
Name (59.110.42.24:root): yangjc
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> passive
Passive mode on.
ftp> ls
227 Entering Passive Mode (172,22,46,218,166,190).
150 Here comes the directory listing.
-rw-r--r--    1 0        0               0 May 29 20:44 k.c
drwxr-xr-x    2 0        0            4096 May 29 21:24 test
226 Directory send OK.
ftp> 

然后在浏览器输入ftp://ip 就可以索引到ubuntu中文件
ubuntu搭建ftp和vsftpd.conf详解_第1张图片

  • 用户访问
    修改配置文件,开启红色部分。默认vsftpd.user_list中的用户才能登录,由于匿名用 户仍然开启,需要将匿名用户ftp加入到vsftpd.user_list中,才能正常匿名访问。 创建新用户和其目录。
  • 下面是我的配置文件
root@yjc:~# cat /etc/vsftpd.user_list 
yangjinc
root@yjc:~# ftp 59.110.42.24
Connected to 59.110.42.24.
220 (vsFTPd 3.0.3)
Name (59.110.42.24:root): yangjinc
530 Permission denied.
Login failed.
ftp> 

yangjinc用户在userlist中,所以禁止登录。
还有登录目录限制,在上面有提到,可以自行设置。

# 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=YES
#
# 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=NO
#
# Allow anonymous FTP? (Disabled by default).
anonymous_enable=YES
#
# 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
pasv_promiscuous=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=NO
# (default follows)
chroot_list_file=/etc/vsftpd.chroot_list
#
userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd.user_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
#

anon_root=/home/hjy/ftp
local_root=/home/hjy/ftp
allow_writeable_chroot=YES
# 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
allow_writeable_chroot=YES
ssl_enable=NO
pasv_enable=YES
pasv_min_port=40000
pasv_max_port=45000
#
# Uncomment this to indicate that vsftpd use a utf8 filesystem.
utf8_filesystem=YES


你可能感兴趣的:(ubuntu搭建ftp和vsftpd.conf详解)