CentOS 7.x 使用vsftpd来搭建ftp

一、环境说明

[root@localhost ~]# cat /etc/redhat-release 
CentOS Linux release 7.3.1611 (Core) 
[root@localhost ~]# uname -r
3.10.0-514.el7.x86_64
[root@localhost ~]# uname -i
x86_64
[root@localhost ~]# uname -p
x86_64
[root@localhost ~]# ip addr show|sed -rn '/inet[[:space:]]+127/! s/^[[:space:]]+inet[[:space:]]+(.*)\/[[:digit:]]+[[:space:]]+.*$/\1/p'
192.168.56.5

二、vsftpd软件安装和说明

2.1、ftp协议和实现的应用程序软件vsftpd简单概要

vsftpd是一款实现应用层ftp协议的应用程序,在CentOS默认的base仓库有提供。我们简单来看看软件的描述信息:

[root@localhost ~]# yum info vsftpd
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.cn99.com
 * extras: mirrors.cn99.com
 * updates: mirrors.shu.edu.cn
Available Packages
Name        : vsftpd
Arch        : x86_64
Version     : 3.0.2
Release     : 25.el7
Size        : 171 k
Repo        : base/7/x86_64
Summary     : Very Secure Ftp Daemon
URL         : https://security.appspot.com/vsftpd.html
License     : GPLv2 with exceptions
Description : vsftpd is a Very Secure FTP daemon. It was written completely from
            : scratch.

由于ftp是一种明文的协议,它是基于C/S架构的,其私密信息传输和认证信息都可以很轻易的被抓取,所以ftp使用的场景要考虑安全设定。除此之外,ftp早期设计的时候只是为了解决某种特定场景的问题,并没有过多的考虑安全问题, 后来的很多服务端的应用程序实现也没有过多的考虑安全设定,比如实现ftp协议的服务端的应用程序有wuftpd, proftpd, pureftpd, vsftpd,在其中,vsftpd相对其同类产品,安全性相对良好,也是现在红帽发行版系统的默认yum仓库里的软件。

2.2、安装vsftpd以及安装文件说明

安装vsftpd,直接使用yum安装就行。

[root@localhost ~]# yum -y install vsftpd
...... #安装过程省略。

安装完后的文件列表:

[root@localhost ~]# rpm -ql vsftpd
/etc/logrotate.d/vsftpd  #通过rsyslog管理的日志轮询脚本
/etc/pam.d/vsftpd	#基于pam认证框架的配置文件
/etc/vsftpd	#主配置文件根目录
/etc/vsftpd/ftpusers	#基于黑白名单控制的默认配置文件之一
/etc/vsftpd/user_list	#基于黑白名单控制的默认配置文件之一
/etc/vsftpd/vsftpd.conf  #主配置文件
/etc/vsftpd/vsftpd_conf_migrate.sh #自带的一个工具脚本,有时候可以用来移除旧版的配置文件和符号链接
/usr/lib/systemd/system-generators/vsftpd-generator#与unit文件相关的一个bash脚本
/usr/lib/systemd/system/vsftpd.service #vsftpd程序的unit文件
/usr/lib/systemd/system/vsftpd.target
/usr/lib/systemd/system/[email protected]
/usr/sbin/vsftpd  #主程序文件
/usr/share/doc/vsftpd-3.0.2  #下面的目录都是一些文档之类的
/usr/share/doc/vsftpd-3.0.2/AUDIT
/usr/share/doc/vsftpd-3.0.2/BENCHMARKS
/usr/share/doc/vsftpd-3.0.2/BUGS
/usr/share/doc/vsftpd-3.0.2/COPYING
/usr/share/doc/vsftpd-3.0.2/Changelog
/usr/share/doc/vsftpd-3.0.2/EXAMPLE
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/README.configuration
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.conf
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE/vsftpd.xinetd
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/README.configuration
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/INTERNET_SITE_NOINETD/vsftpd.conf
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/README.configuration
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/PER_IP_CONFIG/hosts.allow
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_HOSTS
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_HOSTS/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/README
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/README.configuration
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/logins.txt
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.conf
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS/vsftpd.pam
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS_2
/usr/share/doc/vsftpd-3.0.2/EXAMPLE/VIRTUAL_USERS_2/README
/usr/share/doc/vsftpd-3.0.2/FAQ
/usr/share/doc/vsftpd-3.0.2/INSTALL
/usr/share/doc/vsftpd-3.0.2/LICENSE
/usr/share/doc/vsftpd-3.0.2/README
/usr/share/doc/vsftpd-3.0.2/README.security
/usr/share/doc/vsftpd-3.0.2/REWARD
/usr/share/doc/vsftpd-3.0.2/SECURITY
/usr/share/doc/vsftpd-3.0.2/SECURITY/DESIGN
/usr/share/doc/vsftpd-3.0.2/SECURITY/IMPLEMENTATION
/usr/share/doc/vsftpd-3.0.2/SECURITY/OVERVIEW
/usr/share/doc/vsftpd-3.0.2/SECURITY/TRUST
/usr/share/doc/vsftpd-3.0.2/SIZE
/usr/share/doc/vsftpd-3.0.2/SPEED
/usr/share/doc/vsftpd-3.0.2/TODO
/usr/share/doc/vsftpd-3.0.2/TUNING
/usr/share/doc/vsftpd-3.0.2/vsftpd.xinetd
/usr/share/man/man5/vsftpd.conf.5.gz
/usr/share/man/man8/vsftpd.8.gz
/var/ftp   #默认的匿名用户登录路径,也是系统用户ftp的家目录
/var/ftp/pub 

2.3、vsftpd的配置指令分类和使用简单说明

[root@localhost vsftpd]# man vsftpd.conf | grep '^[^[:space:]]\+'
VSFTPD.CONF(5)                                                        File Formats Manual                                                        VSFTPD.CONF(5)
NAME
DESCRIPTION
FORMAT
BOOLEAN OPTIONS
NUMERIC OPTIONS
STRING OPTIONS
AUTHOR

vsftpd的配置文件主要分为三大选项组成,分别为布尔值类型选项、数值类型选项以及字符串值类型选项。
其中布尔值类型选项的值可以使用YES或NO以及1或0,TRUE或FALSE,手册中建议使用YES或者NO,所以
我们最好统一一种风格,比较就使用推荐的YES或者NO来表示启用或者不启用某种选项。
数值类型的选项的值进可以使用数值或者八进制的数值,八进制以0开头。字符串类型的选项的值可以使用字符串
或数值字符串。(以实际使用测试为准)
vsftpd的选项设置格式为:option(directive)=value,其中的指令部分前面不能有多余的其他符号,特别是容易忽
视的空白字符。man手册中说明来指令和等号之间以及值和等号之间也不能有任意的空白,不过实际测试效果有
些场景的选项可以有些不能,所以建议还是按照规范来定义。就按照shell的变量赋值风格来,不要按照python的
变量赋值风格来。注释部分以符号#开头,表示此行都是注释。符合shell脚本风格。
原文如下:
Below is a list of boolean options. The value for a boolean option may be set to YES or NO.
Below  is  a  list of numeric options. A numeric option must be set to a non negative integer. Octal numbers are
 supported, for convenience of the umask options. To specify an octal number, use 0 as the first digit of the
  number.
Below is a list of string options.

The format of vsftpd.conf is very simple. Each line is either a comment or a directive. Comment lines start with a 
# and are ignored.A  directive  line has the format:
option=value
It is important to note that it is an error to put any space between the option, = and value.
Each setting has a compiled in default which may be modified in the configuration file.

三、ftp的主动模式和被动模式

ftp是C/S架构的,有服务端和客户端的概念。ftp设计的连接认证以及数据传输比较特殊。它把指令传输和数据传输分为两个不同的连接通道。于是就有两所谓的两个连接方式,分别为:
命令连接:传输命令
数据连接:传输数据
而其中的数据连接由根据命令连接后,数据连接建立的方式不同分为主动连接和被动连接,也叫主动模式和被动模式。主动模式也叫PORT模式,被动模式可以叫PASV模式。数据连接分类:
主动模式(PORT模式):ftp服务端主动使用默认的20号端口(在vsftpd软件中可以自定义)去连接客户端当时发起命令连接的随机端口向后的第一个可用端口(每次加1,不可用再往后寻找,直到找到可用的为止)
被动模式(PASV模式):打开一个随机端口,等待客户端去连接。这个随机端口的范围在vsftpd实现中可以自定义。
请看下面的图解:
在这里插入图片描述

主动模式和被动模式一直都是ftp使用的一个难点之一,不是因为连接本身不好理解,而是实际生产使用配合上防火墙,会出现各种各样的问题。我们来考虑这样一种场景。假设我们的ftp服务端的监听套接字的端口为21(就是默认的端口),客户端使用随机端口11502向服务端发起连接请求,经过tcp的三次握手之后建立可靠的连接虚拟通道。如果此时发起一个命令叫做get FILENAME(下载一个文件),get是命令,会通过命令连接通道传输这个指令数据。而文件的内容通过磁盘获得数据块后要在内核中构建响应报文后,数据的传输要额外建立通道传输。假设使用主动模式来传输数据,此时ftp服务端会使用默认的数据连接端口20(只有ftp使用主动模式传输数据的时候,20端口的套接字才有信息连接,一旦数据传输完成,20端口并不会像默认的21号端口那样长期监听处于监听状态)去向之前ftp发起命令通道的端口11502向后加1,通过探测发现是否可用,如果不可用再往后加1,直到找到可用的(假设超超时时间允许),假设刚好11503端口可用,ftp服务端会以20向ftp客户端主动的11503端口主动发起连接请求,我们都直到,一般客户端去连接服务端,服务端充当的角色是提供服务的,所以会开放连接套接字给指定的主机或者所有主机。客户端本身只是会去使用固定随机端口去连接服务端,客户端本身的角色是享用外界提供的服务,它本身可能不提供服务,这个时候如果你ftp服务端去主动连接客户端,一般情况下,客户端那边的防火墙是关闭的,不允许外界去连接它,只允许它去连接别人,通俗来讲就是,客户端本身出去的报文受限制的概率较低,而进入客户端的报文受限制的概念很高,这也是很多应用场景ftp的主动模式不可用的主要原因之一。不过这些都不成问题,防火墙,比如linux的iptables实现,它本身只支持连接状态追踪功能的,所以这种场景刚好适用,前提是要会配置。说完ftp的主动模式,我们来说说的被动模式,被动模式就是在服务端收到ftp的指令的时候,并且确认来了双方传输数据使用被动模式,ftp 服务端会随机打开一个端口(当然,由于安全原因这种随机的可能性,我们会实现配置它固定到一个端口访问,服务端防火墙允许这个端口范围的报文即可),ftp服务端会以一种特殊的方式走ftp的命令连接通道告诉ftp客户端这个连接端口,ftp客户端受到信息后计算出ftp服务端的这个随机端口,然后ftp客户端也会使用一个随机端口去连接服务端的这个随机端口,此次连接通过后,就可以开始传输数据了。

四、实现基本的用户访问设置

ftp的认证用户可以分为:匿名用户(无密码),系统用户以及虚拟用户。所以连接访问方式也分为这三种。基本的访问用户设置就是匿名用户和系统用户。一般生产环境为了使用比较特殊的场景,会使用虚拟用户(相对安全一点)。

4.1、匿名用户配置和连接访问测试

匿名用户可以使用 anonymous 或 ftp,前者好理解,单词本身就是匿名的之意。后者也可以是因为默认的vsftpd安装完成,会创建一个叫做ftp的系统用户。匿名用户访问的初始根目录就是ftp系统用户的家目录,默认是/var/ftp。来看看与匿名用户设置相关的几个常用选项:

[root@localhost vsftpd]# man vsftpd.conf | grep -E 'anonymous_|anon_'
       allow_anon_ssl
       anon_mkdir_write_enable
       anon_other_write_enable
       anon_upload_enable
       anon_world_readable_only
       anonymous_enable
       force_anon_data_ssl
       force_anon_logins_ssl
       no_anon_password
       anon_max_rate
       anon_umask
       anon_root
常用的有:
anonymous_enable=YES|NO  #是否启用匿名用户登录,默认值为YES
anon_upload_enable=YES|NO #是否允许匿名用户可以上传文件(这只是控制ftp实现层面,具体是否可写,还要取决于文件系统的权限),默认值为NO
anon_world_readable_only=YES|NO #如果启用,匿名用户将只能下载它可见的文件,默认值为YES
anon_mkdir_write_enable=YES|NO #是否允许匿名创建目录(这只是控制ftp实现层面,具体是否可以创建目录,取决于匿名用户ftp是否对父目录具有写权限),默认值NO
anon_other_write_enable=YES|NO #是否允许匿名用户执行写操作,包括删除和重命名文件。默认值为NO
anon_umask=077 #设置匿名用户创建文件时候的umask值,默认值为077

修改前备份一下:
[root@localhost ~]# cp /etc/vsftpd/vsftpd.conf{,.bak}

ftp协议加入防火墙允许(如果测试没有开启,就忽略下面这部分内容)
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 
	
[root@localhost ~]# firewall-cmd --add-service=ftp --permanent
success
[root@localhost ~]# firewall-cmd --reload
success
[root@localhost ~]# firewall-cmd --list-all
public (active)
  target: default
  icmp-block-inversion: no
  interfaces: ens33
  sources: 
  services: dhcpv6-client ftp ssh
  ports: 
  protocols: 
  masquerade: no
  forward-ports: 
  sourceports: 
  icmp-blocks: 
  rich rules: 

使用案例一:按照默认值,上面的参数常用匿名参数列表的值为

anonymous_enable=YES
anon_upload_enable=NO
anon_world_readable_only=YES
anon_mkdir_write_enable=NO
anon_other_write_enable=NO
anon_umask=077

启动服务:
systemctl start vsftpd.service
查看端口以及进程:
ss -tnl
ps aux|grep vsftpd

测试:
linux浏览器或者windows命令行可以测试。或者其他linux主机使用lftp或ftp连接测试。
lftp或ftp可能要使用yum来安装。

[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): ftp
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,56,5,198,216).
150 Here comes the directory listing.
drwxr-xr-x    2 0        0               6 Oct 30 19:45 pub
226 Directory send OK.
查看帮助:
ftp> help
Commands may be abbreviated.  Commands are:

!		debug		mdir		sendport	site
$		dir		mget		put		size
account		disconnect	mkdir		pwd		status
append		exit		mls		quit		struct
ascii		form		mode		quote		system
bell		get		modtime		recv		sunique
binary		glob		mput		reget		tenex
bye		hash		newer		rstatus		tick
case		help		nmap		rhelp		trace
cd		idle		nlist		rename		type
cdup		image		ntrans		reset		user
chmod		lcd		open		restart		umask
close		ls		prompt		rmdir		verbose
cr		macdef		passive		runique		?
delete		mdelete		proxy		send


是否可创建目录?
ftp> mkdir test1
550 Permission denied.

是否可删除文件或目录?
ftp> rmdir pub
550 Permission denied.

是否可上传文件或下载文件?
ftp> lcd /etc/
Local directory now /etc
ftp> put fstab
local: fstab remote: fstab
227 Entering Passive Mode (192,168,56,5,131,10).
550 Permission denied.
ftp> get pub
local: pub remote: pub
227 Entering Passive Mode (192,168,56,5,183,213).
550 Failed to open file.

使用案例2:给匿名用户完全控制的权限,配置选项(正常生产环境要禁用匿名用户登录)
anonymous_enable=YES
anon_upload_enable=YES
anon_world_readable_only=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
anon_umask=077
测试略,要随时注意文件系统目录权限。因为匿名用户不常用,所以就不举例。

4.2、系统用户配置和连接访问测试

用户通过vsftpd服务访问到的默认路径,是用户自己的家目录;默认可以自己有权限访问的所有路径间切换;如果要禁锢用户于其家目录中,就不能让系统用户对其家目录拥有写的权限(也就是系统用户设置为ftp用户登录后,就不能让它再登录系统,否则就不安全了)。

几个常用选项和配置说明:

local_enable=YES|NO #是否允许系统用户可以登录(/etc/passwd中出现的用户),默认值为NO。如果要让匿名用户
可以登录以及虚拟用户可以登录,都需要启用这个选项,因为匿名用户要使用系统用户ftp,匿名用户也要使用自
己定义的一个系统用户来管理所有匿名用户。

write_enable=YES|NO #是否允许FTP改变文件系统内容的相关命令。默认值为NO。包含系统用户是否可写之意
local_umask=022 #指定系统用户创建文件时候的umask值
辅助配置文件/etc/vsftpd/ftpusers,列在此文件中的用户 均禁止使用ftp服务;
chroot_local_user=YES #禁锢所有本地用户 于其家目录;需要事先去除用户对家目录的写权限;默认值为NO
chroot_list_enable=YES 
chroot_list_file=/etc/vsftpd/chroot_list #禁锢列表中文件存在的用户于其家目录中;需要事先去除用户对家目录的写权限;
控制可登录vsftpd服务的用户列表:
userlist_enable=YES #启用/etc/vsftpd/user_list文件来控制可登录用户;
userlist_deny=YES|NO #YES:意味着此为黑名单;NO:白名单;
userlist_file=/etc/vsftpd/user_list #黑白名单的用户列表文件,每行一个用户名。要在userlist_enable启用的时候有效,而且至于是黑白名单是由userlist_deny选项来控制。

示例:

(1) 创建一个系统用户,来实验禁锢于在家目录和没有禁锢在家目录的比较
[root@localhost ~]# useradd yanhui
[root@localhost ~]# echo 'yanhui.com' | passwd yanhui --stdin
Changing password for user yanhui.
passwd: all authentication tokens updated successfully.
[root@localhost ~]# finger yanhui
Login: yanhui         			Name: 
Directory: /home/yanhui             	Shell: /bin/bash
Never logged in.
No mail.
No Plan.
默认没有禁锢于家目录:
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,56,5,159,114).
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/home/yanhui"
ftp> cd ../
250 Directory successfully changed.
ftp> pwd
257 "/home"
ftp> cd /var/tmp/
250 Directory successfully changed.
ftp> lcd /etc
Local directory now /etc
ftp> put fstab .
local: fstab remote: .
227 Entering Passive Mode (192,168,56,5,237,217).
553 Could not create file.
ftp> ls 
227 Entering Passive Mode (192,168,56,5,247,185).
150 Here comes the directory listing.
drwx------    4 0        0              59 Dec 02 21:27 dracut.PFkSf1
drwx------    3 0        0              17 Dec 16 09:00 systemd-private-60cceef7dd504b6da580f743fda92924-vmtoolsd.service-dEUclD
drwx------    3 0        0              17 Dec 02 21:27 systemd-private-7e00d9f20e814ed0b1fb4c07e8329e57-systemd-hostnamed.service-rkPmzP
drwx------    3 0        0              17 Dec 02 21:26 systemd-private-7e00d9f20e814ed0b1fb4c07e8329e57-vmtoolsd.service-shJVQq
226 Directory send OK.

可以看到,上面可以直接切换系统目录,可以想象,这种做法危害有多大。

设置选项后chroot_local_user=YES,默认我们先看看系统用户是否还能切换,没有去掉用户对家目录的写权限
登录会有以下的提示:
[root@localhost ~]# ftp 192.168.56.5
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.
421 Service not available, remote server has closed connection
ftp> ls
Not connected.
原因是:从2.3.5之后,vsftpd增强了安全检查,如果用户被限定在了其主目录下,则该用户的主目录不能再具有
写权限了!如果检查发现还有写权限,就会报该错误。所以,如果我们想让系统用户锁定在其主目录下, 要去掉
其对目录的写权限。
[root@localhost ~]# ls -l /home/
total 0
drwx------ 2 yanhui yanhui 62 Dec 16 20:08 yanhui
[root@localhost ~]# chmod u-w /home/yanhui/
[root@localhost ~]# ls -l /home/
total 0
dr-x------ 2 yanhui yanhui 62 Dec 16 20:08 yanhui
重新测试登录:
[root@localhost ~]# ftp 192.168.56.5
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> pwd
257 "/"
ftp> ls
227 Entering Passive Mode (192,168,56,5,58,219).
150 Here comes the directory listing.
226 Directory send OK.
ftp> cd /etc/
550 Failed to change directory.
ftp> pwd
257 "/"
这次切换目录就报错了。


(2) 创建系统用户,来实验黑白名单
创建2个系统用户,分别为user1和user2来模拟黑白名单。
[root@localhost ~]# for i in {1..2};do
> useradd user${i}
> done
[root@localhost ~]# id user1
uid=1001(user1) gid=1001(user1) groups=1001(user1)
[root@localhost ~]# id user2
uid=1002(user2) gid=1002(user2) groups=1002(user2)
[root@localhost ~]# for i in {1..2};do
> echo "user${i}"|passwd user${i} --stdin
> done
Changing password for user user1.
passwd: all authentication tokens updated successfully.
Changing password for user user2.
passwd: all authentication tokens updated successfully.
实现黑名单,之前有创建一个系统用户yanhui,这里有创建两个用户,分别为user1和user2.只有yanhui可以登录,user1和user2都不能登录。配置:
userlist_enable=YES
userlist_deny=NO
userlist_file=/etc/vsftpd/user_list

[root@localhost ~]# vim /etc/vsftpd/user_list 
[root@localhost ~]# cp /etc/vsftpd/user_list{,.bak}
[root@localhost ~]# >/etc/vsftpd/user_list
[root@localhost ~]# echo 'yanhui' >/etc/vsftpd/user_list
[root@localhost ~]# cat /etc/vsftpd/user_list
yanhui
测试登录yanhui用户:
[root@localhost ~]# ftp 
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls
227 Entering Passive Mode (192,168,56,5,47,98).
150 Here comes the directory listing.
226 Directory send OK.
ftp> pwd
257 "/"
测试登录user1和user2用户:
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user1
530 Permission denied.
Login failed.
ftp> open 192.168.56.5 21
Already connected to 192.168.56.5, use close first.
ftp> close
221 Goodbye.
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user2
530 Permission denied.
Login failed.
直接在输入用户后就拒绝登录。 


实现白名单,出了yanhui用户之外,其他用户都能登录。
userlist_enable=YES
userlist_deny=YES
userlist_file=/etc/vsftpd/user_list
改了一下userlist_deny为YES,其他配置都不用动。重启vsftpd.service,然后验证。
验证yanhui用户登录:
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): yanhui
530 Permission denied.
Login failed.
ftp> close
221 Goodbye.

验证user1和user2用户登录:
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user1
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.
421 Service not available, remote server has closed connection
ftp> close
Not connected.
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user2
331 Please specify the password.
Password:
500 OOPS: vsftpd: refusing to run with writable root inside chroot()
Login failed.
421 Service not available, remote server has closed connection
上述失败是由于之前遇到的安全为题引起,可以把系统用户家目录的写权限去掉或者
[root@localhost ~]# chmod u-w /home/user{1,2}
[root@localhost ~]# ls -ld /home/user{1,2}
dr-x------ 2 user1 user1 62 Dec 16 20:46 /home/user1
dr-x------ 2 user2 user2 62 Dec 16 20:46 /home/user2
再次测试:
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user1
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> close
221 Goodbye.
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): user2
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> close
221 Goodbye.

4.3、一些与连接数、传输日志、守护进程类型等相关的说明

(1) 与传输日志相关的选项
vsftpd_log_file=/var/log/vsftpd.log
dual_log_enable=NO
syslog_enable=NO
xferlog_enable=YES
xferlog_file=/var/log/xferlog
xferlog_std_format=YES

(2) 与长传下载速率以及并发连接数相关的选项
anon_max_rate=0
local_max_rate=0
max_clients=2000
max_per_ip=50

上述选项的相关主机可以参考:man 5 vsftpd.conf ,如果没有,请安装man-pages软件包,我们这里实验不会用到,我会在其他翻译博文中给出所有选项的含义的注解。


(3) 守护进程类型说明
standalone:独立守护进程;由服务进程自行监听套按字,并接收用户访问请求; 
transient:瞬时守护进程;由受托管方代为监听套按字,服务进程没有访问请求时不启动;当托管方收到访问请求时,才启动服务进程;
其中瞬时守护进程(非独立守护进程)在CentOS6和CentOS7托管的对象不同,分别如下:
CentOS 6:xinetd独立守护进程, /etc/xinetd.d/,
CentOS 7:由systemd代为监听;

五、基于pam_mysql来实现虚拟用户控制

5.1、几个相关的选项说明

pam_service_name=ftp
#设置vsfptd的pam的配置文件,这个配置文件配置了具体授权和认证相关。默认值为ftp,这个配置文件默认要放#在/etc/pam.d/目录下,而且命名尽量和应用程序vsftpd相关。

guest_enable=YES|NO
#是否启用虚拟用户登录。默认值为NO,表示不启用,如果设置为启用后,所有的虚拟用户映射
#guest_username选项对应的系统用户。即所有虚拟用户都通过选项guest_username指定的系统用户来管理。

guest_username=ftp
#设置用于被虚拟用户映射的系统用户,默认值为ftp。

user_config_dir=
默认值为none。定义用于管理不同的虚拟用户的目录,这个目录可以单独为每个用户设置不同的子配置文件,
里面可以直接存放于虚拟用户命名一样的配置文件,然后单独为每个虚拟用户自定义不同的权限。

virtual_use_local_privs=YES|NO
默认为NO。如果设置为YES,虚拟用户权限检查和本地系统用户权限检查一样。默认为NO,表示虚拟用户权限
检查和匿名用户权限检查一样,这种权限检查趋势更加的严谨,特别是在写权限控制方面。

local_root=
默认值为none。这个选项用于控制,vsftpd的用户在文件系统上登录后切换到的路径。在虚拟用户时,可以通过
此选项来配置不同虚拟用户切换的路径不一样。

说明:虚拟用户的写权限,通过匿名一样的指令进行定义;

5.2、yum安装mariadb以及mariadb的几个选项设置

(1) 安装mariadb-server

[root@localhost ~]# yum install mariadb-server
...... #安装过程略

(2) 设置几个参数

[root@localhost ~]# cat /etc/my.cnf.d/server.cnf
在上面配置文件的[mysqld]分组后面写入一下配置:
skip-name-resolve=ON
lower_case_table_names=1
innodb_file_per_table=ON
含义分别为:
跳过IP地址反向解析;
忽略字符大小敏感;
每个表一个数据文件;

启动mysql:
[root@localhost ~]# systemctl start mariadb.service
[root@localhost ~]# ps aux|grep mysql
mysql     25378  0.0  0.3 113252  1580 ?        Ss   21:13   0:00 /bin/sh /usr/bin/mysqld_safe --basedir=/usr
mysql     25576  2.2 16.4 969280 79480 ?        Sl   21:13   0:00 /usr/libexec/mysqld --basedir=/usr --datadir=/var/lib/mysql --plugin-dir=/usr/lib64/mysql/plugin --log-error=/var/log/mariadb/mariadb.log --pid-file=/var/run/mariadb/mariadb.pid --socket=/var/lib/mysql/mysql.sock
root      25612  0.0  0.1 112648   960 pts/0    R+   21:14   0:00 grep --color=auto mysql
[root@localhost ~]# ss -tnl|grep 3306
LISTEN     0      50           *:3306                     *:*                  
[root@localhost ~]# ss -tnlp|grep 3306
LISTEN     0      50           *:3306                     *:*                   users:(("mysqld",pid=25576,fd=13))

(3) 安全设置

#要在mysql启动的情况下
[root@localhost ~]# mysql_secure_installation 

NOTE: RUNNING ALL PARTS OF THIS SCRIPT IS RECOMMENDED FOR ALL MariaDB
      SERVERS IN PRODUCTION USE!  PLEASE READ EACH STEP CAREFULLY!

In order to log into MariaDB to secure it, we'll need the current
password for the root user.  If you've just installed MariaDB, and
you haven't set the root password yet, the password will be blank,
so you should just press enter here.

Enter current password for root (enter for none):   #第一次默认root密码为空,回车即可
OK, successfully used password, moving on...

Setting the root password ensures that nobody can log into the MariaDB
root user without the proper authorisation.

Set root password? [Y/n] Y  #是否设置root登录密码,肯定要设置,默认root是mysql的管理员账号
New password: 
Re-enter new password: 
Password updated successfully!
Reloading privilege tables..
 ... Success!


By default, a MariaDB installation has an anonymous user, allowing anyone
to log into MariaDB without having to have a user account created for
them.  This is intended only for testing, and to make the installation
go a bit smoother.  You should remove them before moving into a
production environment.

Remove anonymous users? [Y/n] y  #是否移除匿名用户,这里就是用户名为空的账户,一定要删除。
 ... Success!

Normally, root should only be allowed to connect from 'localhost'.  This
ensures that someone cannot guess at the root password from the network.

Disallow root login remotely? [Y/n] n  #是否禁止root远程登录,禁止后只能通过本地回环或localhost登录,根据实际需要设置,我这里就跳过没有禁止。
 ... skipping.

By default, MariaDB comes with a database named 'test' that anyone can
access.  This is also intended only for testing, and should be removed
before moving into a production environment.

Remove test database and access to it? [Y/n] n #是否删除默认的test数据库,可以删也可以不删,没有多大意义。
 ... skipping.

Reloading the privilege tables will ensure that all changes made so far
will take effect immediately.

Reload privilege tables now? [Y/n] y  #是否重新加载权限配置内容到内存中,要加载一起,理解生效权限表的配置
 ... Success!

Cleaning up...

All done!  If you've completed all of the above steps, your MariaDB
installation should now be secure.

Thanks for using MariaDB!

测试root登录:
[root@localhost ~]# mysql -h127.0.0.1 -uroot -P3306 -pyanhui.com --default-character-set=utf8
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 8
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 
测试OK。

5.3、编译安装pam_mysql

在CentOS6,这个软件可以通过配置epel源后安装。CentOS7,默认没有提供,可以去网站http://sf.net/projects/pam-mysql/下载这个源码包编译安装。有兴趣或者有能力的话,可以去epel的源码包目录下载对应的SRPM包,参考里面的SPEC文件手动制作用于CentOS 7上的rpm安装包。CentOS6就不演示了,可以列出
CentOS 6的rpm包的安装目录作为参考:

[root@VM_0_4_centos packages]# rpm -qpl pam_mysql-0.7-0.12.rc1.el6.x86_64.rpm
/lib64/security/pam_mysql.so
/usr/share/doc/pam_mysql-0.7
/usr/share/doc/pam_mysql-0.7/COPYING
/usr/share/doc/pam_mysql-0.7/CREDITS
/usr/share/doc/pam_mysql-0.7/ChangeLog
/usr/share/doc/pam_mysql-0.7/NEWS
/usr/share/doc/pam_mysql-0.7/README

CentOS 6.x上的pam_mysql的SRPM包下载链接为:
https://mirrors.tuna.tsinghua.edu.cn/epel/6/SRPMS/Packages/p/pam_mysql-0.7-0.12.rc1.el6.src.rpm
CentOS 7.x上的pam_mysql的源码包下载链接为:
 http://sf.net/projects/pam-mysql/

CentOS 7上编译安装:

安装两个依赖:
yum install mariadb-devel pam-devel gcc gcc-c++ make openssl-devel

或者直接安装开发包组以及指定包组可能没安装的软件包:
yum groupinstall "Development Tools" "Server Platform Development"
yum install mariadb-devel pam-devel
过程忽略。

[root@localhost ~]# ls -l pam_mysql-0.7RC1.tar.gz 
-rw-r--r-- 1 root root 335240 Dec 16 21:22 pam_mysql-0.7RC1.tar.gz
[root@localhost ~]# tar -xf pam_mysql-0.7RC1.tar.gz 
[root@localhost ~]# cd pam_mysql-0.7RC1
[root@localhost pam_mysql-0.7RC1]# ./configure --help  #可以查看帮助信息

最终编译和安装:
./configure --with-pam=/usr --with-mysql=/usr --with-pam-mods-dir=/usr/lib64/security
make
make install 

查看文件:
[root@localhost pam_mysql-0.7RC1]# ls -l /usr/lib64/security/pam_mysql.*
-rwxr-xr-x 1 root root    886 Dec 16 21:39 /usr/lib64/security/pam_mysql.la
-rwxr-xr-x 1 root root 141680 Dec 16 21:39 /usr/lib64/security/pam_mysql.so
上面的pam_mysql.so这个模块文件很关键,如果没有就不行。默认的pam软件包的模块安装路径为/usr/lib64/security,所以我们把第三方的模块也安装到这个默认的路径下。

5.4、授权数据库用户,创建数据库和表

(1) 管理员账号来创建账号并授权:
    GRANT ALL ON vsftpd.* TO vsftpd@'127.0.0.1' IDENTIFIED BY 'vsftpd';
    FLUSH PRIVILEGES; #不刷也可以,mysql5.1的时候要刷,这里的默认的mariadb对于GRANT语句已经不需要刷新权限表了。
  测试新创建用户vsftpd的登录:
   [root@localhost ~]# mysql -h127.0.0.1 -uvsftpd -P3306 -pvsftpd
Welcome to the MariaDB monitor.  Commands end with ; or \g.
Your MariaDB connection id is 10
Server version: 5.5.60-MariaDB MariaDB Server

Copyright (c) 2000, 2018, Oracle, MariaDB Corporation Ab and others.

Type 'help;' or '\h' for help. Type '\c' to clear the current input statement.

MariaDB [(none)]> 

(2) 创建用于管理虚拟用户的库表
CREATE DATABASE vsftpd DEFAULT CHARACTER SET utf8;
USE vsftpd;
CREATE TABLE `users` (
  `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
  `name` varchar(100) NOT NULL,
  `password` char(48) NOT NULL,
  PRIMARY KEY (`id`),
  UNIQUE KEY `name` (`name`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;   

MariaDB [(none)]> CREATE DATABASE vsftpd DEFAULT CHARACTER SET utf8;
Query OK, 1 row affected (0.00 sec)

MariaDB [(none)]> USE vsftpd;
Database changed
MariaDB [vsftpd]> CREATE TABLE `users` (
    ->   `id` int(10) unsigned NOT NULL AUTO_INCREMENT,
    ->   `name` varchar(100) NOT NULL,
    ->   `password` char(48) NOT NULL,
    ->   PRIMARY KEY (`id`),
    ->   UNIQUE KEY `name` (`name`)
    -> ) ENGINE=InnoDB DEFAULT CHARSET=utf8; 
Query OK, 0 rows affected (0.10 sec)

MariaDB [vsftpd]> SHOW TABLES;
+------------------+
| Tables_in_vsftpd |
+------------------+
| users            |
+------------------+
1 row in set (0.01 sec)
 

5.5、向虚拟用户表中插入多个普通用户

插入虚拟用户tom和jerry的信息
INSERT INTO users(name,password) VALUES('tom',PASSWORD('tom')),('jerry',PASSWORD('jerry'));
MariaDB [vsftpd]> INSERT INTO users(name,password) VALUES('tom',PASSWORD('tom')),('jerry',PASSWORD('jerry'));
Query OK, 2 rows affected (0.02 sec)
Records: 2  Duplicates: 0  Warnings: 0

MariaDB [vsftpd]> SELECT * FROM users;
+----+-------+-------------------------------------------+
| id | name  | password                                  |
+----+-------+-------------------------------------------+
|  1 | tom   | *71FF744436C7EA1B954F6276121DB5D2BF68FC07 |
|  2 | jerry | *09FB9E6E2AA0750E9D8A8D22B6AA8D86C85BF3D0 |
+----+-------+-------------------------------------------+
2 rows in set (0.00 sec)

5.6、配置vsftpd的配置以及pam的配置

(1) 创建用于管理的虚拟用户vftpadmin:
useradd  -s /sbin/nologin vftpadmin
[root@localhost ~]# useradd  -s /sbin/nologin vftpadmin
[root@localhost ~]# finger vftpadmin
Login: vftpadmin      			Name: 
Directory: /home/vftpadmin          	Shell: /sbin/nologin
Never logged in.
No mail.
No Plan.

(2) 设定虚拟用户的子配置文件目录为:/etc/vsftpd/vsftpd_user_config
[root@localhost ~]# mkdir -pv /etc/vsftpd/vsftpd_user_config
mkdir: created directory ‘/etc/vsftpd/vsftpd_user_config’

(3) 创建用于vsftpd的pam的配置文件:
[root@localhost ~]# cat /etc/pam.d/vsftpd.vftpadmin
auth required /usr/lib64/security/pam_mysql.so user=vsftpd passwd=vsftpd host=127.0.0.1 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2
account required /usr/lib64/security/pam_mysql.so user=vsftpd passwd=vsftpd host=127.0.0.1 db=vsftpd table=users usercolumn=name passwdcolumn=password crypt=2

配置字段说明(如果要弄懂原理,要看得懂pam的配置才行)
auth 表示认证 
account 验证账号密码正常使用 
required 表示认证要通过 
pam_mysql.so模块是默认的相对路径,是相对/lib64/security/路 径而言,也可以写绝对路径;后面为给此模块传递的参数 
user=vsftpd   登录mysql的用户 
passwd=vsftpd 登录mysql的的密码,我们的vsftpd用户的密码vsftpd,之前有授权过
host=127.0.0.1   mysql服务器的主机名或ip地址,我们之前授权vsftpd用户通过127.0.0.1登录才有权限。
db=vsftpd  指定连接msyql的数据库名称 
table=users 指定连接数据库中的表名 
usercolumn=name 当做用户名的字段 
passwdcolumn=password 当做用户名字段的密码 
crypt=2 密码的加密方式为mysql password()函数加密,crypt是加密方式,0表示不加密,1表示crypt(3)加密,2表示使用mysql password()函数加密,3表示md5加密,4表示sha1 加密 

(4) 设定工作目录
[root@localhost ~]# mkdir -pv /data/vftpadmin/upload
mkdir: created directory ‘/data/vftpadmin’
mkdir: created directory ‘/data/vftpadmin/upload’
[root@localhost ~]# chmod a-w /data/vftpadmin  #为了应对高版本的vsftpd的安全问题,只能单独设置一个切换的目录,然后上传文件控制到子目录中。
[root@localhost ~]# ls -l /data/vftpadmin/upload/
total 0
[root@localhost ~]# ls -ld /data/vftpadmin/upload/
drwxr-xr-x 2 root root 6 Dec 16 22:18 /data/vftpadmin/upload/
[root@localhost ~]# setfacl -m u:vftpadmin:rwx /data/vftpadmin/upload
[root@localhost ~]# getfacl /data/vftpadmin/upload
getfacl: Removing leading '/' from absolute path names
# file: data/vftpadmin/upload
# owner: root
# group: root
user::rwx
user:vftpadmin:rwx
group::r-x
mask::rwx
other::r-x

5.7、模拟测试不同虚拟用户的不同权限

全局配置文件加入或者启用:
pam_service_name=vsftpd.vftpadmin
user_config_dir=/etc/vsftpd/vsftpd_user_config

两个虚拟用户的子配置文件:
[root@localhost upload]# cat /etc/vsftpd/vsftpd_user_config/tom 
local_root=/data/vftpadmin
guest_enable=YES  #启用虚拟用户
write_enable=YES  #系统用户可写(vftpadmin)
download_enable=YES #全局允许所有下载请求
guest_username=vftpadmin #映射虚拟用户的系统用户为vftpadmin
virtual_use_local_privs=NO #前面有介绍,建议使用默认值NO, 其他权限控制和匿名用户权限一样。如果设置
#为YES,其他权限控制和系统用户一样的。
local_umask=007  #虚拟用户创建文件的umask值
anonymous_enable=NO #匿名用户不可登录
anon_upload_enable=YES #匿名是否支持上传功能,YES表示支持。由于我们的virtual_user_local_privs权限为
#NO,所以这里也是控制虚拟用户是否可以上传。
anon_world_readable_only=NO #要想虚拟用户能下载,即使download_enable为YES,这个也不能设置为
#YES,否则就不可以下载了。因为是虚拟用户映射vftpadmin用户,其本身不属于虚拟用户的文件,所以不能做
#这样的控制。
anon_mkdir_write_enable=YES #匿名用户是否支持创建文件功能,YES表示支持,和上面同理,虚拟用户可以
#创建文件
anon_other_write_enable=YES #匿名用户是否支持删除文件功能,YES表示支持,和上面同理,虚拟用户可以
#删除文件

[root@localhost upload]# cat /etc/vsftpd/vsftpd_user_config/jerry 
local_root=/data/vftpadmin
guest_enable=YES
write_enable=NO
download_enable=NO
guest_username=vftpadmin
virtual_use_local_privs=NO
local_umask=007
anonymous_enable=NO
anon_upload_enable=NO
anon_world_readable_only=NO 
anon_mkdir_write_enable=NO

重启服务:
[root@localhost vsftpd_user_config]# systemctl restart vsftpd.service

验证测试:tom用户
[root@localhost ~]# ftp
ftp> open 192.168.56.5 21
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): tom
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd /etc    #切换目录失败,符合预期值。前面有讲如何限定用户不能切换目录
550 Failed to change directory.
ftp> pwd
257 "/"
ftp> ls
227 Entering Passive Mode (192,168,56,5,19,204).
150 Here comes the directory listing.
drwxrwxr-x    3 0        0              31 Dec 16 14:32 upload
226 Directory send OK.
ftp> cd upload
250 Directory successfully changed.
ftp> lcd /etc
Local directory now /etc
ftp> ls
227 Entering Passive Mode (192,168,56,5,90,61).
150 Here comes the directory listing.
226 Directory send OK.
ftp> put fstab   #上传文件OK
local: fstab remote: fstab
227 Entering Passive Mode (192,168,56,5,169,151).
150 Ok to send data.
226 Transfer complete.
541 bytes sent in 6.3e-05 secs (8587.30 Kbytes/sec)
ftp> mkdir test1    #创建目录OK
257 "/upload/test1" created
ftp> mkdir test2
257 "/upload/test2" created
ftp> rmdir test2    #删除目录OK
250 Remove directory operation successful.
ftp> put passwd
local: passwd remote: passwd
227 Entering Passive Mode (192,168,56,5,219,91).
150 Ok to send data.
226 Transfer complete.
1008 bytes sent in 3.5e-05 secs (28800.00 Kbytes/sec)
ftp> delete passwd   #删除文件OK
250 Delete operation successful.
ftp> ls
227 Entering Passive Mode (192,168,56,5,63,84).
150 Here comes the directory listing.
-rw-------    1 1003     1003          541 Dec 16 14:43 fstab
drwx------    2 1003     1003            6 Dec 16 14:43 test1
226 Directory send OK.
ftp> lcd /etc/
Local directory now /etc
ftp> mput issue passwd
mput issue? y
227 Entering Passive Mode (192,168,56,5,194,181).
150 Ok to send data.
226 Transfer complete.
23 bytes sent in 3.2e-05 secs (718.75 Kbytes/sec)
mput passwd? y
227 Entering Passive Mode (192,168,56,5,239,122).
150 Ok to send data.
226 Transfer complete.
1008 bytes sent in 3.4e-05 secs (29647.06 Kbytes/sec)
ftp> lcd /var/tmp/
Local directory now /var/tmp
ftp> ls
227 Entering Passive Mode (192,168,56,5,182,33).
150 Here comes the directory listing.
-rwx------    1 1003     1003          541 Dec 16 15:19 fstab
-rw-------    1 1003     1003           23 Dec 16 15:20 issue
-rw-------    1 1003     1003         1008 Dec 16 15:20 passwd
drwx------    2 1003     1003            6 Dec 16 15:12 test1
226 Directory send OK.
ftp> mget issue fstab   #下载测试OK
mget issue? y
227 Entering Passive Mode (192,168,56,5,166,86).
150 Opening BINARY mode data connection for issue (23 bytes).
226 Transfer complete.
23 bytes received in 2e-05 secs (1150.00 Kbytes/sec)
mget fstab? y
227 Entering Passive Mode (192,168,56,5,154,7).
150 Opening BINARY mode data connection for fstab (541 bytes).
226 Transfer complete.
541 bytes received in 4.5e-05 secs (12022.22 Kbytes/sec)


验证测试:jerry用户
ftp> open 192.168.56.5 
Connected to 192.168.56.5 (192.168.56.5).
220 (vsFTPd 3.0.2)
Name (192.168.56.5:root): jerry
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> cd upload
250 Directory successfully changed.
ftp> ls
227 Entering Passive Mode (192,168,56,5,47,167).
150 Here comes the directory listing.
-rwx------    1 1003     1003          541 Dec 16 15:19 fstab
-rw-------    1 1003     1003           23 Dec 16 15:20 issue
-rw-------    1 1003     1003         1008 Dec 16 15:20 passwd
drwx------    2 1003     1003            6 Dec 16 15:12 test1
226 Directory send OK.
ftp> mkdir test2    #创建目录失败
550 Permission denied.
ftp> rmdir test1    #删除目录失败
550 Permission denied.
ftp> lcd /etc      
Local directory now /etc
ftp> put passwd   #上传文件失败
local: passwd remote: passwd
227 Entering Passive Mode (192,168,56,5,206,125).
550 Permission denied.
ftp> delete fstab    #删除文件失败
550 Permission denied.
ftp> lcd /tmp
Local directory now /tmp
ftp> get issue    #下载文件失败
local: issue remote: issue
227 Entering Passive Mode (192,168,56,5,178,217).
550 Permission denied.
验证来,tom用户拥有几乎所有权限,jerry用户只拥有只读的权限。

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章