Linux:FTP服务器 vsftpd 的搭建和配置

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/xietansheng/article/details/84145618

本文链接: https://blog.csdn.net/xietansheng/article/details/84145618

vsftpd(Very Secure FTP Daemon)是一个在 Linux/Unix 系统上运行的一款开源免费的 FTP 服务器软件。vsftpd 支持支持 匿名用户、本地用户、虚拟用户 3种登录方式。vsftpd 高速安全,支持带宽限制,支持IPv6,可分配虚拟IP,支持创建虚拟用户。

相关网站:

1. FTP 服务器搭建

1.1 安装 vsftpd(Ubuntu)

sudo apt-get install vsftpd

查看 vsftpd 是否正在运行并监听 21 端口

netstat -npl | grep :21

其他 vsftpd 服务管理命令

sudo service vsftpd start             # 启动 vsftpd 服务
sudo service vsftpd stop              # 关闭 vsftpd 服务
sudo service vsftpd restart           # 重新启动 vsftpd 服务
sudo service vsftpd status            # 查看 vsftpd 服务状态

vsftpd 服务安装完成并启动后,即可直接使用当前 Liunx 系统用户名和密码进行访问:

ftp localhost       # 回车后输入 Linux系统 用户名 和 密码,登录
ls                  # 登录后默认显示的是用户主目录,输入 help 回车查看所有操作命令

1.2 vsftpd 配置

vsftpd 配置文件路径: /etc/vsftpd.conf

# 先备份原配置文件
sudo cp /etc/vsftpd.conf /etc/vsftpd.conf_backup

# 打开配置文件
sudo vim /etc/vsftpd.conf

# 打开配置文件后, 配置下面的字段, 如果字段被注释, 则打开注释
listen=YES
local_enable=YES
write_enable=YES

配置完成后,sudo service vsftpd restart重启服务,用本地用户(Linux系统标准用户)的 用户名 和 密码 登录FTP。

1.3 限制用户登录 /etc/ftpusers

/etc/ftpusers文件内的用户名禁止登录 FTP,文件内已有的默认禁止的用户名有 root、daemon、bin、sys 等。如果需要禁止某个用户登录,可将用户名添加到该文件(每行一个用户名)。该文件由 PAM 模块的/etc/pam.d/vsftpd配置文件指定:

# Standard behaviour for ftpd(8).
auth	required	pam_listfile.so item=user sense=deny file=/etc/ftpusers onerr=succeed

# Note: vsftpd handles anonymous logins on its own. Do not enable pam_ftp.so.

# Standard pam includes
@include common-account
@include common-session
@include common-auth
auth	required	pam_shells.so

vsftpd 也有自己的限制用户登录的管理机制,详见/etc/vsftpd.conf文件中userlist_file字段介绍。

PS: 如果有本地用户没有在 /etc/ftpusers 文件内,却依然无法登录,打开sudo vim /etc/pam.d/vsftpd,找到下面这句话,注释掉,重启服务,再尝试登录:

#auth	required	pam_shells.so

1.4 其他 vsftpd 配置字段说明(vsftpd.conf)

#
# 1. 监听相关
#
listen=<YES/NO>                     # YES: 服务以独立运行方式运行; NO: 运行在 xinetd 内。 默认为 YES
listen_address=<ip address>         # 服务监听地址, 如果有多个网卡, 需要将服务绑定到指定 IP 地址
listen_port=<port>                  # 服务监听端口, 默认为 21

#
# 2. 匿名用户相关
#
anonymous_enable=<YES/NO>           # 是否允许匿名用户访问, 默认 NO
anon_mkdir_write_enable=<YES/NO>    # 是否允许匿名用户创建文件夹, 默认 NO
anon_other_write_enable=<YES/NO>    # 是否允许匿名用户其他的写权限, 创建文件、重命名、删除文件等权限(默认为 NO, 基于安全性考虑这个权限一般不打开)
anon_upload_enable=<YES/NO>         # 是否允许匿名用户上传, 默认 NO
anon_umask=<nnn>                    # 匿名用户上传的文件的生成掩码, 默认为077
anon_max_rate=<n>                   # 匿名用户的最大传输速率, 单位为 Byte/s, 值为 0 表示不限制
anon_world_readable_only=<YES/NO>   # 是否允许匿名用户只读浏览

#
# 3. 本地用户(Linux标准系统用户)相关
#
local_enable=<YES/NO>               # 是否支持本地用户帐号访问
write_enable=<YES/NO>               # 是否开放本地用户的写权限
local_umask=<nnn>                   # 本地用户上传的文件的生成掩码, 默认为077
local_max_rate=<n>                  # 本地用户最大的传输速率, 单位为 Byte/s,值为 0 表示不限制
local_root=<file>                   # 本地用户登陆后的目录,默认为 本地用户 的 主目录

chroot_local_user=<YES/NO>          # 本地用户是否可以执行 chroot, 默认为 NO
chroot_list_enable=<YES/NO>         # 是否只有指定的用户才能执行 chroot, 默认为 NO
chroot_list_file=<filename>         # 当 chroot_local_user=NO 且 chroot_list_enable=YES 时,
                                    # 只有 filename 文件内指定的用户(每行一个用户名)可以执行 chroot,
                                    # 默认值为 /etc/vsftpd.chroot_list

#
# 4. 本地用户 黑/白名单管理
#
userlist_enable=<YES/NO>    # 是否启用 userlist_file 白/黑名单用户列表, 默认为 NO

userlist_deny=<YES/NO>      # 当 userlist_enable=YES(即启用 userlist_file )时, 则该字段才有效。
                            # userlist_deny=YES: userlist_file 为 黑名单, 即在该文件内的用户均不可登录, 其他用户可以登录
                            # userlist_deny=NO:  userlist_file 为 白名单, 即在该文件内的用户才可以登录, 其他用户均不可登录

userlist_file=<filename>    # 黑/白名单用户列表文件(每行一个用户名), 
                            # 是黑名单还是白名单, 根据 userlist_deny 的值决定,
                            # 默认值为 /etc/vsftpd.user_list

#
# 5. 连接相关
#
ftpd_banner=<message>               # 客户端连接服务器后显示的欢迎信息
connect_timeout=<n>                 # 远程客户端响应端口数据连接超时时间, 单位为秒, 默认 60
accept_connection_timeout=<n>       # 空闲的数据连接超时时间, 单位为秒, 默认 120
data_connection_timeout=<n>         # 空闲的用户会话超时时间, 单位为秒, 默认 300
max_clients=<n>                     # 在独立模式运行时, 最大连接数, 0 表示无限制
max_per_ip=<n>                      # 在独立模式运行时, 每 IP 的最大连接数, 0表示无限制

其他详细配置介绍参考:

2. 守护程序 xinetd

上面所说的 vsftpd 服务均是以比较常用的 standalone 模式独立运行,除此之外还可以以 super daemon 的模式由 xinetd 管理运行。

xinetd 是 Linux 守护进程服务程序,将服务集中管理。许多服务进程只有偶尔才需要用到,为了节省资源,可以交由 xinted 代理监听相应的端口,一旦有请求到达这个端口,先到达 xinted,xinted 根据请求的端口,临时启动相应的服务进程处理请求,响应完成后,xinted 再关闭相应进程,继续监听端口。

  • 使用 super daemon 模式,必须修改/etc/vsftpd.conf中的配置
# 必须改为 NO, 否则连接 FTP 时会报错: 500 OOPS: could not bind listening IPv4 socket
listen=NO
  • 安装 xinetd
sudo apt-get install xinetd
  • 配置守护进程
sudo vim /etc/xinetd.conf

把下面配置添加到文件尾部:

service ftp
{
        socket_type             = stream
        wait                    = no
        user                    = root
        server                  = /usr/sbin/vsftpd
        log_on_success          += DURATION USERID
        log_on_failure          += USERID
        nice                    = 10
        disable                 = no
}
  • 停止 vsftpd 服务,启动 xinetd 服务
sudo service vsftpd stop
sudo service xinetd restart
  • 查询端口是否在监听
sudo netstat -npl | grep :21

3. 访问 FTP

3.1 命令行 FTP 客户端

  • 登录 FTP
ftp [host [port]]       # 例如: ftp 192.168.0.100(默认端口号为21可不填写)

# 输入 用户名
# 输入 密码
# 登录成功进入 FTP 管理界面

ls                      # 查看文件夹下的文件
cd dir                  # 进入文件夹

help                    # 查看更多命令, 大部分命令和 Linux 本地文件管理命令相同
  • 上传文件
put LocalFile [RemoteFile]          # 不指定 RemoteFile, 则以原文件名保存到当前FTP目录
mput LocalFile [LocalFile ...]      # 上传多个本地文件, 默认以原文件名保存到当前FTP目录
  • 下载文件
get RemoteFile [LocalFile]          # 不指定 LocalFile, 则以原文件名保存到本地(默认保存到当前工作目录下)
mget RemoteFile [RemoteFile ...]    # 下载多个文件, 默认以原文件名保存到当前本地工作目录
  • 创建目录、删除目录/文件、重命名
mkdir dir_name                      # 创建文件夹
rmdir dir_name                      # 删除文件夹
delete file                         # 删除文件
rename FromFile ToFile              # 重命名

3.2 图形界面 FTP 客户端

FTP图形界面客户端,推荐使用 FileZilla,支持 Windows、Linux、MAC 多个平台,免费开源,界面友好,使用简单。

FileZilla 官网: https://filezilla-project.org

4. vsftpd.conf

最后附上 /etc/vsftpd.conf 配置文件的默认原文, 供配置参考(不同版本,不同系统安装后,默认值和默认状态可能不一样):

# 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
#
# Run standalone with IPv6?
# Like the listen parameter, except vsftpd will listen on an IPv6 socket
# instead of an IPv4 one. This parameter and the listen parameter are mutually
# exclusive.
#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
# This option specifies the location of the RSA key to use for SSL
# encrypted connections.
rsa_private_key_file=/etc/ssl/private/ssl-cert-snakeoil.key

猜你喜欢

转载自blog.csdn.net/xietansheng/article/details/84145618