VSFTPD FTP服务器搭建手册

环境

阿里云Centos ECS 服务器。

安装

执行以下命令安装vsftpd

yum install -y vsftpd

检测是否安装:

rpm -qa |grep vsftpd

卸载:

rpm -aq vsftpd
rpm -e vsftpd-3.0.2-22.el7.x86_64

控制命令

设置自启动

systemctl enable vsftpd.service

启动

systemctl start vsftpd.service

重启

systemctl restart vsftpd.service

关闭

service vsftpd stop
#或
systemctl stop vsftpd.service

查看是否启动

netstat -antup | grep ftp

配置

配置登录用户相关的操作

增加ftp登录用户:

# 增加用户test,并指定test用户的主目录为/home/test,如果目录已经存在,会有提示,但是不影响用户的权限登陆
useradd -d /home/test test
# 增加ftp-test3 用户
useradd -d /mnt/pub/ftp-test3 ftp-test3

修改登录用户密码:

passwd test
passwd ftp-test3

限制用户只能ftp登录:


#限定用户test不能telnet,只能ftp
usermod -s /sbin/nologin test 
#用户test恢复可以登录
usermod -s /bin/bash test 

usermod -s /sbin/nologin ftp-test3
usermod -s /sbin/bash ftp-test3

更改用户主目录

#更改用户test的主目录为/test
usermod -d /test test 

修改用户主目录权限

chmod o+w /mnt/pub/ftpdev
chmod o+w /mnt/pub/ftp-test3

配置vsftpd

关闭ftp匿名登录

#修改vsftp配置文件
vim /etc/vsftpd/vsftpd.conf  

修改:
anonymous_enable=NO
重启vsftp生效

限制登录用户不能跳出根目录

限制登录用户不能跳出根目录,即用户登录后只能访问用户主目录下的所有目录和文件,不能访问和主目录同级和上级的目录和文件。例如ftptest的用户主目录是/mnt/pub/ftptest,那么用户登录后就只能有/mnt/pub/ftptest 这个目录的权限,用户看不到也修改不了同级的或上级的目录和文件。

vim /etc/vsftpd/vsftpd.conf //修改vsftp配置文件
local_enable=YES //允许本地用户登录
write_enable=YES // 可以上传(全局控制)
local_umask=022 // 本地用户上传文件的umask
chroot_local_user=NO // 用户不能修改自己根目录
chroot_list_enable=YES //开启chroot_list 配置,该配置文件下的用户只能访问自己的目录,不在该列表的用户,可以访问自己目录以为的目录
chroot_list_file=/etc/vsftpd/chroot_list // chroot_list 配置文件存放路径, 如果没有该配置,可以手动添加
userlist_enable=YES
userlist_deny=NO //开启ftp用户登录白名单,只允许/etc/vsftpd/user_list的用户访问ftp

重启vsftp 生效

常见问题

当我们限定了用户不能跳出其主目录之后,使用该用户登录FTP时往往会遇到这个错误:

500 OOPS: vsftpd: refusing to run with writable root inside chroot ()

这个问题发生在最新的这是由于下面的更新造成的:
- Add stronger checks for the configuration error of running with a writeable root directory inside a chroot(). This may bite people who carelessly turned on chr

从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有写权限了!如果检查发现还有写权限,就会报该错误。
要修复这个错误,可以用命令chmod a-w /home/user去除用户主目录的写权限,注意把目录替换成你自己的。或者你可以在vsftpd的配置文件中增加下列两项中的一项
allow_writeable_chroot=YES

FTP主动模式和被动模式

主动和被动的主语是FTP服务
主动模式(PORT): 是服务器主动去连接客户端的随机高位端口,这个场景需要客户端防火墙额外开启1024以后的端口给FTP服务端连接。
被动模式(PASV): 是服务器被动等待客户端去连接随意的高位端口,这个场景需要FTP服务端额外防火墙开启1024以后的端口所有来源的连接。
VSFTP 默认是开启主动模式,不支持被动模式。

VSFTP 主动模式配置:

connect_from_port_20=YES

VSFTP 被动模式配置:

需要开启被动模式,并设置被动模式地址,以及开启监听这个地址的请求,最后还需要在FTP防火墙上开启允许任何地址连接被动模式的端口范围的端口。

#开启 IPV4的地址监听
listen=YES
#设置IPV4的地址
pasv_address=
#开启被动模式
pasv_enable=yes
#被动模式允许连接的端口最大值(这个值同时需要在FTP防火墙上设置)
pasv_max_port=20480  
#被动模式允许连接的端口最小值(这个值同时需要在FTP防火墙上设置)    
pasv_min_port=10240
#开启 IPV6的地址监听(如果不需要或没有可以关闭,IPV6需要设置对应的IPV6地址)
listen_ipv6=YES 


阿里云安全组策略的配置
在这里插入图片描述
在这里插入图片描述

被动模式同时需要客户端支持,现在主流客户端都支持该模式,部分客户端可以通过执行pass on指令切换被动模式命令,或者在连接FTP时指定连接类型是被动模式。

在Java的Apache FTP Client可以通过执行以下方法来切换到被动模式。

ftpClient.enterLocalPassiveMode();

530 Login incorrect. 的处理

1.密码错误。

2.检查/etc/vsftpd/vsftpd.conf配置
看下面配置

local_enable=YES  
# 这里重要,有人说ubuntu是pam_service_name=ftp,可以试试
pam_service_name=vsftpd    
userlist_enable=YES

3.检查/etc/pam.d/vsftpd

注释掉

#auth required pam_shells.so

4.修改/etc/pam.d/vsftpd

auth required pam_shells.so修改为->auth required pam_nologin.so 即可

参考:ubuntu vsftpd 530 Login incorrect 根本原因和解决方案

附录

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).
anonymous_enable=NO
#
# Uncomment this to allow local users to log in.
# When SELinux is enforcing check for SE bool ftp_home_dir
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.
# 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.
xferlog_enable=YES
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES
#port_enable=NO
#
# 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_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 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=NO
chroot_list_enable=YES
# (default follows)
chroot_list_file=/etc/vsftpd/chroot_list
allow_writeable_chroot=YES
#
# 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.
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.
# Make sure, that one of the listen options is commented !!
listen_ipv6=NO

pam_service_name=vsftpd
userlist_enable=YES
userlist_deny=NO
tcp_wrappers=YES
pasv_enable=yes
pasv_max_port=20480	
pasv_min_port=10240
# 配置你的服务器外网IP
pasv_address= 
#pasv_addr_resolve=yes

user_list 配置文件

# vsftpd userlist
# If userlist_deny=NO, only allow users in this file
# If userlist_deny=YES (default), never allow users in this file, and
# do not even prompt for a password.
# Note that the default vsftpd pam config also checks /etc/vsftpd/ftpusers
# for users that are denied.
ftpupload
ftp-test
ftp-test2
ftp-test3

chroot_list 配置文件

ftpupload
ftp-test
ftp-test2
ftp-test3

  • 5
    点赞
  • 25
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值