vsftpd的安装及配置(非虚拟用户)

2021-11-30   66 次阅读


文章标题图片 :Pexels 上的 Agnese Lunecka 拍摄的图片

1.环境介绍

  • vsftpd: version 3.0.2
  • CentOS Linux release 7.6.1810 (Core)
  • Linux pg1 3.10.0-957.el7.x86_64 #1 SMP Thu Nov 8 23:39:32 UTC 2018 x86_64 x86_64 x86_64 GNU/Linux

2.创建ftp用户

# 这里创建的ftp用户(user1)就是系统用户,家目录是 /ftpdata/user1 ,bash为 nologin不允许这个用户登录操作系统
[root@test ~]#useradd  -d  /ftpdata/user1 -s /sbin/nologin  user1
# 给user1用户设置密码
[root@test ~]#passwd user1
# user1的家目录权限改为 700
[root@test ~]#chmod 700  /ftpdata/user1

3.安装vsftpd服务

# 安装vsftpd服务
[root@test ~]#yum install vsftpd

4.修改vsftpd的主配置文件

[root@test ~]#cat /etc/vsftpd/vsftpd.conf

# Example config file /etc/vsftpd/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.
#
# Allow anonymous FTP? (Beware - allowed by default if you comment this out).
# 是否允许匿名访问,为 NO 则不允许
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
# 是否允许本地用户访问,为 YES 则允许,我们用的就是本地系统用户登录
local_enable=YES
#
# Uncomment this to enable any form of FTP write command.
# 是否允许本地用户对FTP服务器文件具有写权限,默认设置为YES允许
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)
# 本地ftp用户上传文件默认的文件权限
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.
# When SELinux is enforcing check for SE bool allow_ftpd_anon_write, allow_ftpd_full_access
#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
#
# Activate logging of uploads/downloads.
# YES 为开启上传和下载的日志文件
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
# 数据传输端口默认为 20
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/xferlog
#
# 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的格式保存日志,默认路径在 /var/log/xferlog
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. The vsftpd.conf(5) man page explains
# the behaviour when these options are disabled.
# 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 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)
#################################################################################
# 设置为YES时,将 "所有用户" 限制在自己的家目录,不允许切换到其它任何目录
chroot_local_user=YES
# 这是个例外设置参数:
# 当设置为 YES 时,所有用户都被限制切换到其它目录,但是名单中的用户是 "例外" 不被限制
# 当设置为 NO 时, 所有用户都被限制在家目录,不允许切换到其它任何目录,不使用 名单,没有任何“例外“用户
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
#
# When "listen" directive is enabled, vsftpd runs in standalone mode and
# listens on IPv4 sockets. This directive cannot be used in conjunction
# with the listen_ipv6 directive.
# 如果设置为YES,则vsftpd将以独立模式运行,由vsftpd自己监听和处理IPv4端口的连接请求
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.
# Make sure, that one of the listen options is commented !!
listen_ipv6=YES

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES
allow_writeable_chroot=YES

5.遇到一个坑

# 描述:检查所有配置都没问题,用户名密码都没错,就是提示 拒绝访问
# 思路:最开始创建的ftp用户 user1 ,bash设置为 nologin ,然后 vsftpd 的 pam认证文件有一条参数如下:
# auth       required	pam_shells.so
# 上面这条参数意思是:仅允许用户的shell为/etc/shells文件内指定的shell才允许登录ftp
# 查了一下 /etc/shells 文件内的shell 有这4种:/bin/sh;/bin/bash;/usr/bin/sh;/usr/bin/bash
# 解决方法就简单了,要么 注释 pam_shells.so ,要么把 pam_shells.so 改为 pam_nologin.so 也是可以的
# 要么把ftp用户的shell改为上面那4种,不过不建议,还是nologin安全一些

[root@test ~]#vim /etc/pam.d/vsftpd
#%PAM-1.0
session    optional     pam_keyinit.so    force revoke
# 下面这行的意思是 /etc/vsftpd/ftpusers 这个文件内的用户禁止登录ftp
auth       required	pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
# 懒人方法,注释掉下面这行
#auth       required	pam_shells.so
auth       include	password-auth
account    include	password-auth
session    required     pam_loginuid.so
session    include	password-auth

6.启动或者重启vsftpd服务

  • 启动:systemctl start vsftpd
  • 重启:systemctl restart vsftpd
  • 停止:systemctl stop vsftpd
  • 自启:systemctl enable vsftpd
  • 记得调整防火墙配置

脑无理想 枉活一世