Centos 7 搭建FTP详细配置步骤方法

Posted 唐僧还在拜佛求经路。

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Centos 7 搭建FTP详细配置步骤方法相关的知识,希望对你有一定的参考价值。

vsftpd的安裝使用:

ftp概述:FTP(File Transfer protocol,文件传输协议)是经典的C/S架构的应用层协议,需要有服务端软件,客户端软件两个部共同组成实现文件传输功能。

主程序包:/usr/sbin/vsftpd

服务名:vsftpd

用户控制列表文件:

 /etc/vsftpd/ftpusers,/etc/vsftpd/user_list

主配置文件:/etc/vsftpd/vsftpd.conf

启动脚本:/etc/rc.d/init.d/vsftpd

[[email protected] mnt]# yum -y install vsftpd    安装vsftpd服务

[[email protected] mnt]# systemctl start vsftpd.service    启动vsftpd服务

[[email protected] mnt]# netstat -tunlp           查看vsftpd服务是否启动,端口为:21

此时可以访问vsftpd默认提供的站点服务;ftp://192.168.214.140,提供站点的访问资源路径为:[[email protected] pub]# cd /var/ftp/pub 下;

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  //开启匿名用户

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    //启用任何形式的写入权限,(如上传,删除文件等)都需要开启此项
efault 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  //目录消息(在目录下切换时会提醒,在目录新建一个(.message文件)在里面填写提示的内容)
#
# Activate logging of uploads/downloads.
xferlog_enable=YES //激活上载/下载的日志记录。
#
# Make sure PORT transfer connections originate from port 20 (ftp-data).
connect_from_port_20=YES  //确保端口传输连接来自端口20(FTP数据)。
#
# 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  //是否改变上传文件的属主,改变谁呢,统一改变whoever用户,和下面选项关联
chown_username=whoever //改变上传的属主
#
# You may override where the log file goes if you like. The default is shown
# below.
xferlog_file=/var/log/xferlog  //启用xferlog日志,默认记录到:/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 //启用标准的xferlog日志格式,若禁用此项,将使用vsftpd自己的日志格式;
#
# 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  运行 vsftpd 需要的非特权系统用户,缺省是nobody

# 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 //是否将FTP本地用户禁锢在宿主目录中;
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.
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  //vsftpd使用pam完成用户认证
userlist_enable=YES       //是否启用控制用户登陆列表文件;:/etc/vsftpd/user_list;

userlist_deny=YES|NO        //路径为:/etc/vsftpd/user_list;
tcp_wrappers=YES

 

自定义添加:

连接限制:

  max_clients:最大并发连接数;

  max_per_ip:每个IP可同时发起的并发请求数;

传输速率: 

  anon_max_rate:匿名用户的最大传输速率,单位是“字节”

  local_max_rate:本地用户的最大传输速率,单位是“字节”

虚拟用户:

  所以的虚拟用户会被统一映射为一个指定的系统账号,访问的共享位置既为此系统账号的家目录;

  各虚拟用户可以被赋予不同的访问权限;通过匿名用户的权限控制参数进行指定;

虚拟用户账号的存储方式:

    文件:编辑文件指定,奇数行为用户名,偶数为行的密码;  此文件需要被编码为hash格式;

    关系型数据库众的表中:既时查询数据库完成用户认证; mysql库:pam要依赖与pam-mysql, yum -y install pam_mysql

 

  

-------------------------------------------------------------------------------

实验

一::开放匿名用户配置,并启动vsftpd服务;

  anonymous_enable=yes             //允许匿名用户访问

  local_enable=YES                //若不需要启用本地用户,可将此项设为NO;

  write_enable=YES       //开放服务器的写权限

  anon_umask=022                 //设置匿名用户上传数据的权限掩码

  anon_upload_enable=YES    //允许匿名上传文件

  anon_upload_write_enable=YES  //允许匿名用户创建目录

关闭匿名访问:anonymous_enable=NO,此时重启服务在此刷新则需要用户名和密码验证;

2:测试匿名FTP服务器;

[[email protected] pub]# yum -y install ftp

[[email protected] pub]# ftp 192.168.214.140
Connected to 192.168.214.140 (192.168.214.140).
220 (vsFTPd 3.0.2)
Name (192.168.214.140:root): ftp
331 Please specify the password.
Password:      //默认密码为空
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls     //查看ftp服务器众的内容
227 Entering Passive Mode (192,168,214,140,210,168).
150 Here comes the directory listing.
drwxr-xr-x    2 0        0              19 Dec 10 09:42 pub
226 Directory send OK.

可以在/var/ftp/新建一个目录;

给权限:setfacl -m u:root:rwx  upload

在配置文件中修改或添加:anon_ohter_write_enable=YES 才可以进行删除;

在进行测试:
ftp> lcd /opt      //将本地目录切换到/opt

ftp> get vsftpdconf.tar.gz  //将文件下载到本地(/opt/目录)

ftp> cd put     //将ftp目录切换到/pub

ftp> put install.log  //将文件上传到服务器(pub/目录)

ftp> ls        //查看所上传文件的权限

ftp> quit       //断开ftp链接并退出

二:vsftpd的用户验证

1:配置本地用户验证

  vim /etc/vsftpd/vsftpd.conf

  local_enable=YES  

  write_enabl=YES  

  local_umask=022

  chroot_local_user=YES

2:使用user_list用户列表

当vsftpd服务器开放了“local_enable”配置以后,默认情况下所以的系统服务用户都可以登陆到次FTP服务器,若只希望对一小部分用户开放FTP服务,则需要开放用户列表控制的相关配置,其中主要包含userlist_enable,userlist_deny,例如:执行一下操作后vsftpd服务器将只允许laya,vanko,hunter三个用户登陆。

[[email protected] vsftpd]# vim /etc/vsftpd/user_list

laya
vanko
hunter

[[email protected] vsftpd]# vim /etc/vsftpd/vsftpd.conf

userlist_enable=YES

userlist_deny=NO

systemctl restart vsftpd.service

就可以测试验证了:

ftp://192.168.214.140

 

这时会报错:

 

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

 

解决方法:[[email protected] vsftpd]# vim /etc/vsftpd/vsftpd.conf
allow_writeable_chroot=YESsystemctl restart vsftpd.service

 

这时使用账户就可以访问了,使用其他用户则拒绝登陆;

 

其他:

 

前两小结中分别做了学习匿名访问,用户验证的FTP服务器基本配置,除此以为还用一下其他的常用配置,许需要我们有一定的了解

 

1:修改vsftpd服务的监听地址,端口

 

vim /etc/vsftpd/vsftp.conf

 

  listen=YES  //允许独立监听服务  listen_address=192.168.4.11  //只在一个接口监听服务  listen_port=2121        //监听端口为2121[[email protected] vsftpd]# service restart vsftpd[[email protected] vsftpd]#netstat -anpt | grep vsftpd2:允许使用FTP服务器的被动模式vim /etc/vsftpd/vsftp.conf     pasv_enable=YES    //允许被动模式连接

 

  pasv_min_port=24500  //指定被动模式下限端口

 

  pasv_max_port=24600  //指定被动模式的上限端口

 

[[email protected] vsftpd]# service restart vsftpd

 

3:限制FTP连接的并发数,传输速度

 

 vim /etc/vsftpd/vsftp.conf

 

   max_client=20    //限制并发客户端连接最多20个

 

  max_per_ip=2    //限制每个IP地址的连接数最多2个

 

  anan_max_rate=50000  //限制匿名用户传输速度为50kB/s

 

  local_max_rate=200000  //限制本地用户传输速率为200kB/s

 

[[email protected] vsftpd]# service restart vsftpd

 

三:基于虚拟用户的FTP服务

 

1:创建文本格式的用户名称,密码列表

 

  [[email protected] vsftpd]# vim /etc/vsftpd/vusers.list

 

  mike

 

  123

 

  john  

 

  456

 

2:创建Berkeley DB格式的数据库文件

[[email protected] vsftpd]# cd /etc/vsftpd/
[[email protected] vsftpd]# db_load -T -t hash -f vusers.list vusers.db
[[email protected] vsftpd]# file vusers.db
vusers.db: Berkeley DB (Hash, version 9, native byte-order)
[[email protected] vsftpd]# chmod 600 /etc/vsftpd/vusers.*
[[email protected] vsftpd]# ls -lh /etc/vsftpd/vsftpd.*
-rw-------. 1 root root 5.0K 12月 12 21:05 /etc/vsftpd/vsftpd.conf
-rw-------. 1 root root 5.0K 12月 12 20:44 /etc/vsftpd/vsftpd.conf.bak
3:添加虚拟用户的映射账号,创建FTP根目录

[[email protected] vsftpd]# useradd -d /var/ftproot -s /sbin/nologin virtual
[[email protected] vsftpd]# chmod 755 /var/ftproot/
4:为vsftpd服务添加虚拟用户支持

 1:为虚拟用户建立PAM认证文件
      vim /etc/pam.d/vsftpd.vu
      #%PAM-1.0
      auth    required        pam_userdb.so db=/etc/vsftpd/vusers
      account required        pam_userdb.so db=/etc/vsftpd/vusers

 2:修改vsftpd配置,添加虚拟用户支持

 vim /etc/vsftpd/vsftpd.conf
   anonymous_enable=NO
   local_enable=YES
   wirte_enable=YES
   anon_umask=022
   guest_enable=YES
   guest_username=virtual
   pam_service_name=vsftpd.vu

 3:为不同的虚拟用户建立独立的配置文件

  vim /etc/vsftpd/vsftpd.conf

    user_config_dir=/etc/vsftpd/vusers_dir

    [[email protected] vsftpd]# mkdir /etc/vsftpd/vusers_dir
    [[email protected] vsftpd]# cd /etc/vsftpd/vusers_dir/
    [[email protected] vusers_dir]# vim john

      anon_upload_enable=YES

      anon_mkdir_write=YES

  [[email protected] vusers_dir]# touch mike
  [[email protected] vusers_dir]# systemctl restart vsftpd.service

  ?:mike用户可以登陆,并能够正常浏览,下载文件,但不能上传文件。  

      ?:john用户可以登陆,且能够正常流量,下载文件,也可以上传文件。

  ?:Linux主机的系统用户将无法登陆;

 

 

 

  

 

 

 

 

 

 

 

  

 

 

 

 

  

 

userlist_deny=NO

以上是关于Centos 7 搭建FTP详细配置步骤方法的主要内容,如果未能解决你的问题,请参考以下文章

centos 7 linux系统默认ftp安装配置和部署(详细讲解)

CentOS 8 上配置 FTP 服务器详细步骤,附截图

Centos 7 上安装Samba的详细步骤

Hadoop集群搭建(步骤图文超详细版)

CentOS服务器上搭建Gitlab安装步骤中文汉化详细步骤日常管理以及异常故障排查

CentOS服务器上搭建Gitlab安装步骤中文汉化详细步骤日常管理以及异常故障排查