CentOS 7安装及配置vsftpd

绪论

vsftp被公认目前最好的ftp之一,所以,搭建它还是很有意义的,有了它,我们可以让虚拟机与主机更加方便的通信。

FTP 用户认证

FTP 的用户一共有三种类型

  • 匿名用户:ftp,anonymous,对应Linux用户ftp
  • 系统用户:Linux用户,用户/etc/passwd,密码/etc/shadow
  • 虚拟用户:特定服务的专用用户,独立的用户名/密码文件

FTP传输模式

文件传输协议(File Transfer Protocol,FTP),基于该协议FTP客户端与服务端可以实现共享文件、上传文件、下载文件。 
FTP基于C/S模式,FTP客户端与服务器端有两种传输模式,分别是FTP主动模式FTP被动模式。主被动模式均是以FTP服务器端为参照。

  • FTP主动模式:客户端从一个任意的端口N(N>1024)连接到FTP服务器的port 21命令端口,客户端开始监听端口N+1,并发送FTP命令“port N+1”到FTP服务器,FTP服务器以数据端口(20)连接到客户端指定的数据端口(N+1)。 
    这里写图片描述

  • FTP被动模式:客户端从一个任意的端口N(N>1024)连接到FTP服务器的port 21命令端口,客户端开始监听端口N+1,客户端提交 PASV命令,服务器会开启一个任意的端口(P >1024),并发送PORT P命令给客户端。客户端发起从本地端口N+1到服务器的端口P的连接用来传送数据。 

在企业实际环境中,如果FTP客户端与FTP服务端均开放防火墙,FTP需以主动模式工作,这样只需要在FTP服务器端防火墙规则中,开放20、21端口即可。

以上内容来自:http://www.linuxidc.com/Linux/2017-06/144886.htm

1.安装vsftp

安装前可以用rpm -qa|grep vsftpd 来查看是否安装

用yum命令查找到了我们想要的vsftpd,开始安装。

yum search vsftpd
[root@localhost bob]# yum search vsftpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
================================= N/S matched: vsftpd =================================
vsftpd-sysvinit.x86_64 : SysV initscript for vsftpd daemon
vsftpd.x86_64 : Very Secure Ftp Daemon

安装:yum install -y vsftpd 

yum install -y vsftpd 

查看安装位置

[root@localhost bob]# whereis vsftpd
vsftpd: /usr/sbin/vsftpd /etc/vsftpd /usr/share/man/man8/vsftpd.8.gz

 2.创建系统用户

sudo useradd ftpuser -d /home/ftpuser -s /sbin/nologin

设置ftpuser密码

sudo passwd ftpuser

3.修改配置文件

查看帮助文档

可以使用man命令查看配置文件的帮助文档,查看有哪些选项是我们需要的,以及各种值分别代表什么含义。

man 5 vsftpd.conf

备份原始配置文件

cp /etc/vsftpd/vsftpd.conf /etc/vsftpd/vsftpd.conf.backup

编辑配置文件

vim /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).
anonymous_enable=YES
#
# 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=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=NO
#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=YES
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=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
#是否使用user_list文件
userlist_enable=YES
#user_list中的用户是否允许访问server
userlist_deny=NO
userlist_file=/etc/vsftpd/user_list
#仅当userlist_enable=YES时,userlist_deny设置才有效
#userlist_enable=YES && userlist_deny=YES:user_list中用户无法登录
#userlist_enable=YES && userlist_deny=NO:只有user_list中用户可以登录
#userlist_enable=YES && userlist_deny=NO:要想匿名登录,必须通过anonymous这一空用户名实现
tcp_wrappers=YES

#配置文件中找到“connect_from_port_20=YES”并将它修改为“connect_from_port_20=NO”,关闭掉vsftpd的主动模式。
#然后在配置文件的末尾追加:
#使vsftpd运行在被动模式
pasv_enable=YES
#被动模式最小端口号20000
pasv_min_port=20000
#被动模式最大端口号21000
pasv_max_port=21000

 不知道为什么,下面的配置文件让vsftpd无法运行

#是否允许匿名登录
anonymous_enable=YES
#是否允许实体用户登录ftp服务器
local_enable=YES
#是否允许实体用户对ftp服务器文件进行写操作
write_enable=YES
#权限掩码
local_umask=022
#是否允许匿名用户上传文件
anon_upload_enable=NO
#是否允许匿名用户创建新文件夹
anon_mkdir_write_enable=NO
#
#guest_enable= #使用允许guest登录
#guest_username= #指定guset的宿主用户
#user_config_dir= #指定guest的配置文件存放路径。
#
#是否显示目录欢迎信息
dirmessage_enable=YES
#
#是否使用20数据连接端口,YES表示启用,选用主动模式;为NO,则通过被动模式连接
connect_from_port_20=NO
#
#chown_uploads=YES #是否允许改变上传文件的属主
#chown_username=whoever #设置上传文件的属主
#
#是否支持ASCII模式上传
ascii_upload_enable=YES
#是否支持ASCII模式下载
ascii_download_enable=YES
#
#是否将所有用户限制在主目录,YES为启用 NO禁用.(该项默认值是NO,即在安装vsftpd后不做配置的,ftp用户是可以向上切换到要目录之外的)
chroot_local_user=YES
#是否启动限制用户例外的名单 YES为启用  NO禁用
chroot_list_enable=YES
#指定限定用户名单
chroot_list_file=/etc/vsftpd/chroot_list
allow_writeable_chroot=YES
#
#chroot_local_user=YES&&chroot_list_enable=YES:默认所有用户限制在主目录下,chroot_list_file不受限制
#chroot_local_user=YES&&chroot_list_enable=NO:所有用户限制在主目录下,不启用chroot_list_file
#chroot_local_user=NO&&chroot_list_enable=YES:默认所有用户不限制在主目录下,chroot_list_file中受限制
#chroot_local_user=YES&&chroot_list_enable=NO:默认所有用户不限制在主目录下,不启用chroot_list_file
#
#指定FTP连接的超时时间
accept_timeout=60
#指定port方式下FTP建立连接的超时时间
connect_timeout=60
#设定会话时限,超时离线
idle_session_timeout=360
#设定传输时限
data_connection_timeout=120
#
xferlog_enable=YES
#指定日志保存路径(事先创建好)
xferlog_file=/var/log/vsftpd.log
#是否使用标准xferlog日志格式
xferlog_std_format=YES
#
#指定ftp端口,默认为21,可修改
#listen_port=21
listen=NO
#listen_ipv6=YES  
#
#用于认证的PAM模块配置文件名
pam_service_name=vsftpd 
#
#是否使用user_list文件 
userlist_enable=YES
#user_list中的用户是否允许访问server
userlist_deny=NO
userlist_file=/etc/vsftpd/user_list
#仅当userlist_enable=YES时,userlist_deny设置才有效
#userlist_enable=YES && userlist_deny=YES:user_list中用户无法登录
#userlist_enable=YES && userlist_deny=NO:只有user_list中用户可以登录
#userlist_enable=YES && userlist_deny=NO:要想匿名登录,必须通过anonymous这一空用户名实现
#
#是否使用tcp_wrappers作为主机访问控制方式
tcp_wrappers=YES
#指定匿名用户最大数据传输速度,默认值为0(不限速)
anon_max_rate=0
#指定实体用户最大数据传输速度,默认值为0(不限速)
local_max_rate=0
#
#配置文件中找到“connect_from_port_20=YES”并将它修改为“connect_from_port_20=NO”,关闭掉vsftpd的主动模式。
#然后在配置文件的末尾追加:
#使vsftpd运行在被动模式
pasv_enable=YES
#被动模式最小端口号20000
pasv_min_port=20000
#被动模式最大端口号21000
pasv_max_port=21000

在user_list中添加ftpuser

vim /etc/vsftpd/user_list

4.防火墙设置

#开放21端口
firewall-cmd --zone=public --add-port=21/tcp --permanent

#开放20端口
firewall-cmd --zone=public --add-port=20/tcp --permanent

#开放ftp服务添加到防火墙外
firewall-cmd --permanent --add-service=ftp

# 添加上面设置的被动模式端口段 20000-21000/tcp
firewall-cmd --permanent --service=ftp --add-port=20000-21000/tcp

#使其生效
firewall-cmd --reload

5.设置Selinux

setsebool -P ftp_home_dir=1 //设置ftp可以使用home目录

setsebool -P allow_ftpd_full_access=1 //设置ftp用户可以有所有权限

参考文章https://blog.csdn.net/programer_bei/article/details/52333586

6.测试登录

使用客户端软件登录

登录成功!

6.遇到的问题

    新手什么都不懂,网上的资料又感觉缺少些什么,所以自己也写了一篇,防止以后采坑。

1.500 OOPS: vsftpd:chroot

在用filezilla登录的时候出现的问题,找了老半天,以为是chroot那些配置的原因,修改/home/ftpuser的写权限,都没用,在chroot_list中加入ftpuser后虽然可以登录了,但是那样的配置限制不了只能在根目录,后来设置了Selinux才解决

2. 550 Create directory operation failed.

前面500能够看到目录了,以为没问题了,在客户端创建目录的时候又报错,再去网上搜索了一下,发现这篇文章,这里面还有其他错误https://blog.csdn.net/programer_bei/article/details/52333586

在文章最下面发现是文件夹权限不够

sudo chmod -R 777 /home/ftpuer/

7.虚拟用户配置(待续)

目前用系统用户登录就够用了,以后需要再补上这部分内容

参考文章 

http://blog.51cto.com/bigtrash/1827429

https://blog.csdn.net/aiynmimi/article/details/77012507

转载于:https://my.oschina.net/u/3939059/blog/1923505

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值