驱魔姐妹快出新卡 發表於 2019-5-11 15:03:00

linux环境:FTP服务器搭建

<p><span style="font-family: &quot;Microsoft YaHei&quot;">转载及参考至:https://www.linuxprobe.com/chapter-11.html</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">       https://www.cnblogs.com/lxwphp/p/8916664.html</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">感谢原作者的分享。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<h5><span id="111" style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(51, 204, 204, 1)"><strong>1 文件传输协议</strong></span></h5>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">一般来讲,人们将计算机联网的首要目的就是获取资料,而文件传输是一种非常重要的获取资料的方式。今天的互联网是由几千万台个人计算机、工作站、服务器、小型机、大型机、巨型机等具有不同型号、不同架构的物理设备共同组成的,而且即便是个人计算机,也可能会装有Windows、Linux、UNIX、Mac等不同的操作系统。为了能够在如此复杂多样的设备之间解决问题解决文件传输问题,文件传输协议(FTP)应运而生。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">FTP是一种在互联网中进行文件传输的协议,基于客户端/服务器模式,默认使用20、21号端口,其中端口20(数据端口)用于进行数据传输,端口21(命令端口)用于接受客户端发出的相关FTP命令与参数。FTP服务器普遍部署于内网中,具有容易搭建、方便管理的特点。而且有些FTP客户端工具还可以支持文件的多点下载以及断点续传技术,因此FTP服务得到了广大用户的青睐。FTP协议的传输拓扑如图11-1所示。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><img src="https://img2018.cnblogs.com/blog/1682975/201905/1682975-20190511140335494-2133810542.png"></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">FTP服务器是按照FTP协议在互联网上提供文件存储和访问服务的主机,FTP客户端则是向服务器发送连接请求,以建立数据传输链路的主机。FTP协议有下面两种工作模式。</span></p>
<blockquote>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>主动模式</strong>:FTP服务器主动向客户端发起连接请求。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>被动模式</strong>:FTP服务器等待客户端发起连接请求(FTP的默认工作模式)。</span></p>
</blockquote>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">(防火墙一般是用于过滤从外网进入内网的流量,因此有些时候需要将FTP的工作模式设置为主动模式,才可以传输数据。本文中使用的是被动模式)</span></p>
<h5><span id="112_Vsftpd" style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(51, 204, 204, 1)"><strong>2 Vsftpd服务程序</strong></span></h5>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">vsftpd作为更加安全的文件传输的服务程序,允许用户以三种认证模式登录到FTP服务器上。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>匿名开放模式</strong>:是一种最不安全的认证模式,任何人都可以无需密码验证而直接登录到FTP服务器。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>本地用户模式</strong>:是通过Linux系统本地的账户密码信息进行认证的模式,相较于匿名开放模式更安全,而且配置起来也很简单。但是如果被黑客破解了账户的信息,就可以畅通无阻地登录FTP服务器,从而完全控制整台服务器。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>虚拟用户模式</strong>:是这三种模式中最安全的一种认证模式,它需要为FTP服务单独建立用户数据库文件,虚拟出用来进行口令验证的账户信息,而这些账户信息在服务器系统中实际上是不存在的,仅供FTP服务程序进行认证使用。这样,即使黑客破解了账户信息也无法登录服务器,从而有效降低了破坏范围和影响。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">ftp是Linux系统中以命令行界面的方式来管理FTP传输服务的客户端工具。我们首先手动安装这个ftp客户端工具,以便在后续实验中查看结果。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(51, 204, 204, 1)">3&nbsp;<strong>FTP安装 及服务开启、重启、关闭</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">首先检查一下你的远程服务器是否已经安装了FTP服务。</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">#rpm -qa | grep vsftpd</span></pre>
</div>
<div class="jb51code"><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">如果啥都没显示,恭喜你,没有安装ftp服务,你可以安心安装。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">运行下面的命令就可以完成vsftpd的安装</span></p>
<div class="jb51code">
<div id="highlighter_74351" class="syntaxhighlighterbash">
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">#yum -y install vsftpd</span></pre>
</div>
<p><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;">安装提示:</span></p>
<div class="cnblogs_code"><span style="font-family: &quot;Microsoft YaHei&quot;"><img src="http://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif"><img id="code_img_opened_e6df0a9b-f1e7-4920-9d42-4d1d4c7d8690" class="code_img_opened lazyload" style="display: none" alt="" data-src="http://images.cnblogs.com/OutliningIndicators/ExpandedBlockStart.gif"></span>
<div id="cnblogs_code_open_e6df0a9b-f1e7-4920-9d42-4d1d4c7d8690" class="cnblogs_code_hide">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># yum install vsftpd
Loaded plugins: langpacks, product</span>-id, subscription-<span style="color: rgba(0, 0, 0, 1)">manager
………………省略部分输出信息………………
</span>================================================================================<span style="color: rgba(0, 0, 0, 1)">
Package Arch Version Repository Size
</span>================================================================================<span style="color: rgba(0, 0, 0, 1)">
Installing:
vsftpd x86_64 </span><span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>-<span style="color: rgba(128, 0, 128, 1)">9</span>.el7 rhel <span style="color: rgba(128, 0, 128, 1)">166</span><span style="color: rgba(0, 0, 0, 1)"> k
Transaction Summary
</span>================================================================================<span style="color: rgba(0, 0, 0, 1)">
Install </span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)"> Package
Total download size: </span><span style="color: rgba(128, 0, 128, 1)">166</span><span style="color: rgba(0, 0, 0, 1)"> k
Installed size: </span><span style="color: rgba(128, 0, 128, 1)">343</span><span style="color: rgba(0, 0, 0, 1)"> k
Is </span><span style="color: rgba(0, 0, 255, 1)">this</span> ok : y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : vsftpd</span>-<span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>-<span style="color: rgba(128, 0, 128, 1)">9</span>.el7.x86_64 <span style="color: rgba(128, 0, 128, 1)">1</span>/<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">
Verifying : vsftpd</span>-<span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>-<span style="color: rgba(128, 0, 128, 1)">9</span>.el7.x86_64 <span style="color: rgba(128, 0, 128, 1)">1</span>/<span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)">
Installed:
vsftpd.x86_64 </span><span style="color: rgba(128, 0, 128, 1)">0</span>:<span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span>-<span style="color: rgba(128, 0, 128, 1)">9</span><span style="color: rgba(0, 0, 0, 1)">.el7
Complete</span>!</span></pre>
</div>
<span class="cnblogs_code_collapse" style="font-family: &quot;Microsoft YaHei&quot;">View Code</span></div>
<p><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;">安装完成再次利用上面的rpm命令检查一下是否完成安装,如果完成安装,那就可以继续。</span></p>
<p><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;">vsftpd服务程序的主配置文件(/etc/vsftpd/vsftpd.conf)内容总长度达到123行,但其中大多数参数在开头都添加了井号(#),从而成为注释信息。</span></p>
<p><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;">&nbsp;<strong>vsftpd服务程序常用的参数以及作用如下</strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="color: rgba(0, 0, 0, 1)">参数                              作用
listen</span>=            是否以独立运行的方式监听服务
listen_address</span>=<span style="color: rgba(0, 0, 0, 1)">IP地址      设置要监听的IP地址
listen_port</span>=<span style="color: rgba(128, 0, 128, 1)">21</span><span style="color: rgba(0, 0, 0, 1)">               设置FTP服务的监听端口
download_enable=    是否允许下载文件
userlist_enable</span>=   设置用户列表为“允许”还是“禁止”操作
userlist_deny</span>=      设置用户列表为“允许”还是“禁止”操作
max_clients</span>=<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">                最大客户端连接数,0为不限制
max_per_ip</span>=<span style="color: rgba(128, 0, 128, 1)">0</span><span style="color: rgba(0, 0, 0, 1)">               同一IP地址的最大连接数,0为不限制
anonymous_enable</span>=    是否允许匿名用户访问
anon_upload_enable</span>=是否允许匿名用户上传文件
anon_umask</span>=<span style="color: rgba(128, 0, 128, 1)">022</span><span style="color: rgba(0, 0, 0, 1)">               匿名用户上传文件的umask值
anon_root</span>=/<span style="color: rgba(0, 0, 255, 1)">var</span>/<span style="color: rgba(0, 0, 0, 1)">ftp         匿名用户的FTP根目录
anon_mkdir_write_enable</span>=    是否允许匿名用户创建目录
anon_other_write_enable</span>=    是否开放匿名用户的其他写入权限(包括重命名、删除等操作权限)
anon_max_rate</span>=<span style="color: rgba(128, 0, 128, 1)">0</span>                     匿名用户的最大传输速率(字节/<span style="color: rgba(0, 0, 0, 1)">秒),0为不限制
local_enable</span>=               是否允许本地用户登录FTP
local_umask</span>=<span style="color: rgba(128, 0, 128, 1)">022</span><span style="color: rgba(0, 0, 0, 1)">                     本地用户上传文件的umask值
local_root</span>=/<span style="color: rgba(0, 0, 255, 1)">var</span>/<span style="color: rgba(0, 0, 0, 1)">ftp               本地用户的FTP根目录
chroot_local_user</span>=          是否将用户权限禁锢在FTP目录,以确保安全
local_max_rate</span>=<span style="color: rgba(128, 0, 128, 1)">0</span>                  本地用户最大传输速率(字节/秒),0为不限制</span></pre>
</div>
<p><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;">开启vsftpd服务</span></p>
</div>
</div>
<div class="jb51code">
<div id="highlighter_359688" class="syntaxhighlighterbash">
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">#service vsftpd start</span></pre>
</div>
<p><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;">#在centos 中使用 systemctl start vsftpd(我觉得上面的也可以)</span></p>
</div>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">检查vsftpd服务</span></p>
<div class="jb51code">
<div id="highlighter_220565" class="syntaxhighlighterbash">
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">#service vsftpd status</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong><span style="font-size: 18px; color: rgba(255, 0, 0, 1); background-color: rgba(255, 255, 0, 1)">注意:关于防火墙的问题</span></strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">CentOS7.0版本的linux系统的防火墙是firewalld,其他版本的可能是iptables(没有去确认)。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">我这里装的时候把防火墙给永久关闭了。</span></p>
<p><span style="color: rgba(255, 0, 0, 1); font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(0, 255, 255, 1)">firewalld防火墙关闭代码</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">关闭防火墙</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"> systemctl stop firewalld.service</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">永久禁止防火墙</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">systemctl disable firewalld.service</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">检查防火墙状态</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">systemctl status firewalld.service</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">提示:Active: inactive (dead)就表示关闭了。</span></p>
<p><span style="color: rgba(255, 0, 0, 1); font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(0, 255, 255, 1)">iptables防火墙关闭代码</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">查询防火墙状态 &nbsp; &nbsp;: &nbsp; &nbsp;# service&nbsp;&nbsp; iptables status</span><br><span style="font-family: &quot;Microsoft YaHei&quot;">停止防火墙 &nbsp; : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# service&nbsp;&nbsp; iptables stop</span><br><span style="font-family: &quot;Microsoft YaHei&quot;">启动防火墙 &nbsp; : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# service&nbsp;&nbsp; iptables start</span><br><span style="font-family: &quot;Microsoft YaHei&quot;">重启防火墙 &nbsp; : &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;# service&nbsp;&nbsp; iptables restart</span><br><span style="font-family: &quot;Microsoft YaHei&quot;">永久关闭防火墙 &nbsp; &nbsp;: &nbsp; &nbsp;# chkconfig&nbsp;&nbsp; iptables off</span><br><span style="font-family: &quot;Microsoft YaHei&quot;">永久关闭后启用 &nbsp; &nbsp;: &nbsp; &nbsp;# chkconfig&nbsp;&nbsp; iptables on</span><br><br></p>
<p><span style="color: rgba(255, 255, 0, 1); font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(255, 0, 0, 1)"><strong>特别注意:不要卸载iptables防火墙,否则你会有大麻烦</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">如果因工作需求,无法关闭iptables防火墙,需要在iptables防火墙规则里添加FTP端口号:</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">(iptables防火墙)添加下面的代码到/etc/sysconfig/iptables里去。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">(firewalld)添加下面的代码到/etc/sysconfig/firewalld里去。</span></p>
<div class="jb51code">
<div id="highlighter_858705" class="syntaxhighlighterbash">
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">-A INPUT -p tcp -m state --state NEW -m tcp --dport <span style="color: rgba(128, 0, 128, 1)">21</span> -j ACCEPT</span></pre>
</div>
</div>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">重启防火墙服务</span></p>
<div class="jb51code">
<div id="highlighter_819309" class="syntaxhighlighterbash">
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;">#systemctl restart iptables.service</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong><span style="color: rgba(255, 0, 0, 1); background-color: rgba(0, 255, 255, 1)">3.1&nbsp;匿名访问模式</span></strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">前文提到,在vsftpd服务程序中,匿名开放模式是最不安全的一种认证模式。任何人都可以无需密码验证而直接登录到FTP服务器。这种模式一般用来访问不重要的公开文件(在生产环境中尽量不要存放重要文件)。当然,如果采用第8章中介绍的防火墙管理工具(如Tcp_wrappers服务程序)将vsftpd服务程序允许访问的主机范围设置为企业内网,也可以提供基本的安全性。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">vsftpd服务程序默认开启了匿名开放模式,我们需要做的就是开放匿名用户的上传、下载文件的权限,以及让匿名用户创建、删除、更名文件的权限。需要注意的是,针对匿名用户放开这些权限会带来潜在危险,我们只是为了在Linux系统中练习配置vsftpd服务程序而放开了这些权限,不建议在生产环境中如此行事。表11-2罗列了可以向匿名用户开放的权限参数以及作用。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>可以向匿名用户开放的权限参数以及作用</strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="color: rgba(0, 0, 0, 1)">参数    作用
anonymous_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES    允许匿名访问模式
anon_umask</span>=<span style="color: rgba(128, 0, 128, 1)">022</span><span style="color: rgba(0, 0, 0, 1)">    匿名用户上传文件的umask值
anon_upload_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES    允许匿名用户上传文件
anon_mkdir_write_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES    允许匿名用户创建目录
anon_other_write_enable</span>=YES    允许匿名用户修改目录名称或删除目录</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>快捷方法:直接向vsftpd服务程序的主配置文件(/etc/vsftpd/vsftpd.conf)中添加下面的配置参数,原本的参数都删了。</strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># vim /etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">vsftpd.conf
</span><span style="color: rgba(128, 0, 128, 1)">1</span> anonymous_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">2</span> anon_umask=<span style="color: rgba(128, 0, 128, 1)">022</span>
<span style="color: rgba(128, 0, 128, 1)">3</span> anon_upload_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">4</span> anon_mkdir_write_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">5</span> anon_other_write_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">6</span> local_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">7</span> write_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">8</span> local_umask=<span style="color: rgba(128, 0, 128, 1)">022</span>
<span style="color: rgba(128, 0, 128, 1)">9</span> dirmessage_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">10</span> xferlog_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">11</span> connect_from_port_20=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">12</span> xferlog_std_format=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">13</span> listen=<span style="color: rgba(0, 0, 0, 1)">NO
</span><span style="color: rgba(128, 0, 128, 1)">14</span> listen_ipv6=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">15</span> pam_service_name=<span style="color: rgba(0, 0, 0, 1)">vsftpd
</span><span style="color: rgba(128, 0, 128, 1)">16</span> userlist_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">17</span> tcp_wrappers=YES</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">然后重启FTP服务。</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># systemctl restart vsftpd
# systemctl enable vsftpd
ln </span>-s <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/usr/lib/systemd/system/vsftpd.service</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/etc/systemd/system/multi-user.target.wants/vsftpd.service</span></span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>测试:</strong>现在就可以在客户端执行ftp命令连接到远程的FTP服务器了。在vsftpd服务程序的匿名开放认证模式下,<strong>其账户统一为anonymous,密码为空。而且在连接到FTP服务器后,默认访问的是/var/ftp/pub目录</strong>。我们可以切换到该目录下的pub目录中,然后尝试创建一个新的目录文件,以检验是否拥有写入权限:</span></p>
</div>
</div>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># ftp <span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">
Connected to </span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span> (<span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">).
</span><span style="color: rgba(128, 0, 128, 1)">220</span> (vsFTPd <span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">)
Name (</span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">:root): anonymous
</span><span style="color: rgba(128, 0, 128, 1)">331</span><span style="color: rgba(0, 0, 0, 1)"> Please specify the password.
Password:此处敲击回车即可
</span><span style="color: rgba(128, 0, 128, 1)">230</span><span style="color: rgba(0, 0, 0, 1)"> Login successful.
Remote system type </span><span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> UNIX.
Using binary mode to transfer files.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> cd pub
</span><span style="color: rgba(128, 0, 128, 1)">250</span><span style="color: rgba(0, 0, 0, 1)"> Directory successfully changed.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> mkdir files
</span><span style="color: rgba(128, 0, 128, 1)">550</span> Permission denied.</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">系统显示拒绝创建目录,查看该目录的权限得知,只有root管理员才有写入权限。怪不得系统会拒绝操作呢!<strong>进入到/var/ftp/pub目录下,右键--属性--权限,将其他类的访问权限改为“创建和删除”,即可通过FTP在该目录下创建文件夹、上传、下载,更改文件名等操作。</strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">如果还不行,系统提示“创建目录的操作失败”(Create directory operation failed),那就是SELinux服务在“捣乱”了吧。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">下面使用getsebool命令查看与FTP相关的SELinux域策略都有哪些:</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># getsebool -a |<span style="color: rgba(0, 0, 0, 1)"> grep ftp
ftp_home_dir </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_anon_write </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_connect_all_unreserved </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_connect_db </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_full_access </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_cifs </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_fusefs </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_nfs </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_passive_mode </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
httpd_can_connect_ftp </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
httpd_enable_ftp_server </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_anon_write </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_enable_homedirs </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_full_access </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_write_ssh_home </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
tftp_anon_write </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
tftp_home_dir </span>--&gt; off</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">我们可以根据经验(需要长期培养,别无它法)和策略的名称判断出是ftpd_full_access--&gt; off策略规则导致了操作失败。接下来修改该策略规则,并且在设置时使用-P参数让修改过的策略永久生效,确保在服务器重启后依然能够顺利写入文件。</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># setsebool -P ftpd_full_access=on</span></pre>
</div>
<div><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></div>
<div><span style="font-family: &quot;Microsoft YaHei&quot;">或:关闭SELinux</span></div>
<div><span style="font-family: &quot;Microsoft YaHei&quot;"># vi /etc/selinux/config</span></div>
<div><span style="font-family: &quot;Microsoft YaHei&quot;">将 SELINUX=XXX --&gt;XXX 代表级别</span></div>
<div><span style="font-family: &quot;Microsoft YaHei&quot;">改为</span></div>
<div><span style="font-family: &quot;Microsoft YaHei&quot;">SELINUX=disabled</span></div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">然后重启系统,或者执行命令:setenforce 0。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">现在便可以顺利执行文件创建、修改及删除等操作了。</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># ftp <span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">
Connected to </span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span> (<span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">).
</span><span style="color: rgba(128, 0, 128, 1)">220</span> (vsFTPd <span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">)
Name (</span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">:root): anonymous
</span><span style="color: rgba(128, 0, 128, 1)">331</span><span style="color: rgba(0, 0, 0, 1)"> Please specify the password.
Password:此处敲击回车即可
</span><span style="color: rgba(128, 0, 128, 1)">230</span><span style="color: rgba(0, 0, 0, 1)"> Login successful.
Remote system type </span><span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> UNIX.
Using binary mode to transfer files.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> cd pub
</span><span style="color: rgba(128, 0, 128, 1)">250</span><span style="color: rgba(0, 0, 0, 1)"> Directory successfully changed.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> mkdir files
</span><span style="color: rgba(128, 0, 128, 1)">257</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/pub/files</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> created
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> rename files database
</span><span style="color: rgba(128, 0, 128, 1)">350</span> Ready <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> RNTO.
</span><span style="color: rgba(128, 0, 128, 1)">250</span><span style="color: rgba(0, 0, 0, 1)"> Rename successful.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> rmdir database
</span><span style="color: rgba(128, 0, 128, 1)">250</span><span style="color: rgba(0, 0, 0, 1)"> Remove directory operation successful.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> bye
</span><span style="color: rgba(128, 0, 128, 1)">221</span> Goodbye.</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong><span style="font-size: 18px; color: rgba(255, 0, 0, 1); background-color: rgba(0, 255, 255, 1)">3..2 本地用户模式</span></strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">相较于匿名开放模式,本地用户模式要更安全,而且配置起来也很简单。如果大家之前用的是匿名开放模式,现在就可以将它关了,然后开启本地用户模式。针对本地用户模式的权限参数以及作用如表11-3所示。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">表11-3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 本地用户模式使用的权限参数以及作用</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"><span style="color: rgba(0, 0, 0, 1)">参数                        作用
anonymous_enable</span>=<span style="color: rgba(0, 0, 0, 1)">NO    禁止匿名访问模式
local_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES    允许本地用户模式
write_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES    设置可写权限
local_umask</span>=<span style="color: rgba(128, 0, 128, 1)">022</span><span style="color: rgba(0, 0, 0, 1)">    本地用户模式创建文件的umask值
userlist_deny</span>=<span style="color: rgba(0, 0, 0, 1)">YES    启用“禁止用户名单”,名单文件为ftpusers和user_list
userlist_enable</span>=YES    开启用户作用名单文件功能</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>快捷方法:直接向vsftpd服务程序的主配置文件(/etc/vsftpd/vsftpd.conf)中添加下面的配置参数,原本的参数都删了。</strong></span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># vim /etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">vsftpd.conf
</span><span style="color: rgba(128, 0, 128, 1)">1</span> anonymous_enable=<span style="color: rgba(0, 0, 0, 1)">NO
</span><span style="color: rgba(128, 0, 128, 1)">2</span> local_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">3</span> write_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">4</span> local_umask=<span style="color: rgba(128, 0, 128, 1)">022</span>
<span style="color: rgba(128, 0, 128, 1)">5</span> dirmessage_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">6</span> xferlog_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">7</span> connect_from_port_20=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">8</span> xferlog_std_format=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">9</span> listen=<span style="color: rgba(0, 0, 0, 1)">NO
</span><span style="color: rgba(128, 0, 128, 1)">10</span> listen_ipv6=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">11</span> pam_service_name=<span style="color: rgba(0, 0, 0, 1)">vsftpd
</span><span style="color: rgba(128, 0, 128, 1)">12</span> userlist_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">13</span> tcp_wrappers=YES</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">在vsftpd服务程序的主配置文件中正确填写参数,然后保存并退出。还需要重启vsftpd服务程序,让新的配置参数生效。在执行完上一个实验后还原了虚拟机的读者,还需要将配置好的服务添加到开机启动项中,以便在系统重启自后依然可以正常使用vsftpd服务。</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># systemctl restart vsftpd
# systemctl enable vsftpd
ln </span>-s <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/usr/lib/systemd/system/vsftpd.service</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/etc/systemd/system/multi-user.target.wants/vsftpd.service</span></span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">按理来讲,现在已经完全可以本地用户的身份登录FTP服务器了。但是在使用root管理员登录后,系统提示如下的错误信息:</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># ftp <span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">
Connected to </span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span> (<span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">).
</span><span style="color: rgba(128, 0, 128, 1)">220</span> (vsFTPd <span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">)
Name (</span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">:root): root
</span><span style="color: rgba(128, 0, 128, 1)">530</span><span style="color: rgba(0, 0, 0, 1)"> Permission denied.
Login failed.
ftp</span>&gt;</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">可见,在我们输入root管理员的密码之前,就已经被系统拒绝访问了。这是因为vsftpd服务程序所在的目录中默认存放着两个名为“用户名单”的文件(ftpusers和user_list)。不知道大家是否已看过一部日本电影“死亡笔记”(刘遄老师在上学期间的最爱),里面就提到有一个黑色封皮的小本子,只要将别人的名字写进去,这人就会挂掉。vsftpd服务程序目录中的这两个文件也有类似的功能—只要里面写有某位用户的名字,就不再允许这位用户登录到FTP服务器上。</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># cat /etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">user_list
</span><span style="color: rgba(128, 0, 128, 1)">1</span><span style="color: rgba(0, 0, 0, 1)"> # vsftpd userlist
</span><span style="color: rgba(128, 0, 128, 1)">2</span> # If userlist_deny=NO, only allow users <span style="color: rgba(0, 0, 255, 1)">in</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)"> file
</span><span style="color: rgba(128, 0, 128, 1)">3</span> # If userlist_deny=YES (<span style="color: rgba(0, 0, 255, 1)">default</span>), never allow users <span style="color: rgba(0, 0, 255, 1)">in</span> <span style="color: rgba(0, 0, 255, 1)">this</span><span style="color: rgba(0, 0, 0, 1)"> file, and
</span><span style="color: rgba(128, 0, 128, 1)">4</span> # <span style="color: rgba(0, 0, 255, 1)">do</span> not even prompt <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> a password.
</span><span style="color: rgba(128, 0, 128, 1)">5</span> # Note that the <span style="color: rgba(0, 0, 255, 1)">default</span> vsftpd pam config also checks /etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">ftpusers
</span><span style="color: rgba(128, 0, 128, 1)">6</span> # <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> users that are denied.
</span><span style="color: rgba(128, 0, 128, 1)">7</span><span style="text-decoration: line-through"><span style="color: rgba(0, 0, 0, 1); text-decoration: line-through"> root
</span></span><span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)"> bin
</span><span style="color: rgba(128, 0, 128, 1)">9</span><span style="color: rgba(0, 0, 0, 1)"> daemon
</span><span style="color: rgba(128, 0, 128, 1)">10</span><span style="color: rgba(0, 0, 0, 1)"> adm
</span><span style="color: rgba(128, 0, 128, 1)">11</span><span style="color: rgba(0, 0, 0, 1)"> lp
</span><span style="color: rgba(128, 0, 128, 1)">12</span><span style="color: rgba(0, 0, 0, 1)"> sync
</span><span style="color: rgba(128, 0, 128, 1)">13</span><span style="color: rgba(0, 0, 0, 1)"> shutdown
</span><span style="color: rgba(128, 0, 128, 1)">14</span><span style="color: rgba(0, 0, 0, 1)"> halt
</span><span style="color: rgba(128, 0, 128, 1)">15</span><span style="color: rgba(0, 0, 0, 1)"> mail
</span><span style="color: rgba(128, 0, 128, 1)">16</span><span style="color: rgba(0, 0, 0, 1)"> news
</span><span style="color: rgba(128, 0, 128, 1)">17</span><span style="color: rgba(0, 0, 0, 1)"> uucp
</span><span style="color: rgba(128, 0, 128, 1)">18</span> <span style="color: rgba(0, 0, 255, 1)">operator</span>
<span style="color: rgba(128, 0, 128, 1)">19</span><span style="color: rgba(0, 0, 0, 1)"> games
</span><span style="color: rgba(128, 0, 128, 1)">20</span> nobody</span></pre>
</div>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># cat /etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">ftpusers
# Users that are not allowed to login via ftp
</span><span style="text-decoration: line-through"><span style="color: rgba(128, 0, 128, 1); text-decoration: line-through">1</span><span style="color: rgba(0, 0, 0, 1); text-decoration: line-through"> root
</span></span><span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)"> bin
</span><span style="color: rgba(128, 0, 128, 1)">3</span><span style="color: rgba(0, 0, 0, 1)"> daemon
</span><span style="color: rgba(128, 0, 128, 1)">4</span><span style="color: rgba(0, 0, 0, 1)"> adm
</span><span style="color: rgba(128, 0, 128, 1)">5</span><span style="color: rgba(0, 0, 0, 1)"> lp
</span><span style="color: rgba(128, 0, 128, 1)">6</span><span style="color: rgba(0, 0, 0, 1)"> sync
</span><span style="color: rgba(128, 0, 128, 1)">7</span><span style="color: rgba(0, 0, 0, 1)"> shutdown
</span><span style="color: rgba(128, 0, 128, 1)">8</span><span style="color: rgba(0, 0, 0, 1)"> halt
</span><span style="color: rgba(128, 0, 128, 1)">9</span><span style="color: rgba(0, 0, 0, 1)"> mail
</span><span style="color: rgba(128, 0, 128, 1)">10</span><span style="color: rgba(0, 0, 0, 1)"> news
</span><span style="color: rgba(128, 0, 128, 1)">11</span><span style="color: rgba(0, 0, 0, 1)"> uucp
</span><span style="color: rgba(128, 0, 128, 1)">12</span> <span style="color: rgba(0, 0, 255, 1)">operator</span>
<span style="color: rgba(128, 0, 128, 1)">13</span><span style="color: rgba(0, 0, 0, 1)"> games
</span><span style="color: rgba(128, 0, 128, 1)">14</span> nobody</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">果然如此!vsftpd服务程序为了保证服务器的安全性而默认禁止了root管理员和大多数系统用户的登录行为,这样可以有效地避免黑客通过FTP服务对root管理员密码进行暴力破解。如果您确认在生产环境中使用root管理员不会对系统安全产生影响,只需按照上面的提示删除掉root用户名即可。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">我们也可以选择ftpusers和user_list文件中没有的一个普通用户尝试登录FTP服务器:</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># ftp <span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">
Connected to </span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span> (<span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">).
</span><span style="color: rgba(128, 0, 128, 1)">220</span> (vsFTPd <span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">)
Name (</span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">:root): linuxprobe (普通用户)
</span><span style="color: rgba(128, 0, 128, 1)">331</span><span style="color: rgba(0, 0, 0, 1)"> Please specify the password.
Password:此处输入该用户的密码
</span><span style="color: rgba(128, 0, 128, 1)">230</span><span style="color: rgba(0, 0, 0, 1)"> Login successful.
Remote system type </span><span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> UNIX.
Using binary mode to transfer files.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> mkdir files
</span><span style="color: rgba(128, 0, 128, 1)">550</span> Create directory operation failed.</span></pre>
</div>
<ul>
<li><span style="font-family: &quot;Microsoft YaHei&quot;"><strong>在采用本地用户模式登录FTP服务器后,默认访问的是该用户的家目录,也就是说,访问的是/home/linuxprobe目录(系统创建的用户,在home文件夹下都有对应用户名的文件夹)</strong>。而且该目录的默认所有者、所属组都是该用户自己,因此不存在写入权限不足的情况。<strong>如果读写权限没给,手动设定,给读写权限。</strong>但是当前的操作仍然被拒绝,是因为我们刚才将虚拟机系统还原到最初的状态了。为此,需要再次开启SELinux域中对FTP服务的允许策略:</span></li>
<li><span style="background-color: rgba(153, 152, 122, 1)"><strong><span style="font-family: &quot;Microsoft YaHei&quot;">采用非本地用户模式登录,需创建FTP组“FTP”用户,与采用本地用户模式的区别是:采用非本地用户模式可以指定FTP文件夹。</span></strong></span></li>
</ul>
<p><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(153, 152, 122, 1)">  新增ftp用户&nbsp;vftpuser</span></p>
<div class="jb51code">
<div id="highlighter_712471" class="syntaxhighlighterbash">
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(153, 152, 122, 1)">#/usr/sbin/adduser -d /mnt/ftp -g ftp -s /sbin/nologin vftpuser</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(153, 152, 122, 1)"><strong><span style="line-height: 1.5">  新增用户vftpuser,用户的主目录是</span><span style="line-height: 1.5">/mnt/ftp&nbsp;</span></strong><span style="line-height: 1.5"><strong>目录,用户组是ftp,不能登录的账户</strong>。</span></span></p>
</div>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; line-height: 1.5; background-color: rgba(153, 152, 122, 1)">  设置vftpuser密码</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(153, 152, 122, 1)">#passwd vftpuser</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(153, 152, 122, 1)">  回车后输入密码</span></p>
<div class="jb51code">
<div id="highlighter_612034" class="syntaxhighlighterbash"><span style="background-color: rgba(153, 152, 122, 1)"><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;"><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;">  <span style="font-family: &quot;Microsoft YaHei&quot;">修改/<span style="font-size: 12px; line-height: 1.5">mnt/ftp/</span></span></span></span><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;">的目录权限</span></span></div>
</div>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(153, 152, 122, 1)">#chmod <span style="color: rgba(128, 0, 128, 1)">755</span> /mnt/ftp/</span></pre>
</div>
<p><span style="line-height: 1.5; font-family: &quot;Microsoft YaHei&quot;; background-color: rgba(153, 152, 122, 1)">  重启vsftp服务</span></p>
<div class="cnblogs_code">
<pre><span style="background-color: rgba(153, 152, 122, 1)">#/sbin/service vsftpd restart</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;; line-height: 1.5">查看SElinux策略内ftp相关权限,并修改。</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># getsebool -a |<span style="color: rgba(0, 0, 0, 1)"> grep ftp
ftp_home_dir </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_anon_write </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_connect_all_unreserved </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_connect_db </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_full_access </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_cifs </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_fusefs </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_nfs </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_passive_mode </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
httpd_can_connect_ftp </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
httpd_enable_ftp_server </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_anon_write </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_enable_homedirs </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_full_access </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_write_ssh_home </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
tftp_anon_write </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off
tftp_home_dir </span>--&gt;<span style="color: rgba(0, 0, 0, 1)"> off<br>
# setsebool -P ftpd_full_access=on</span></pre>
</div>
<p style="margin-left: 30px"><span style="font-family: &quot;Microsoft YaHei&quot;">或:关闭SELinux</span></p>
<div style="margin-left: 30px"><span style="font-family: &quot;Microsoft YaHei&quot;"># vi /etc/selinux/config</span></div>
<p style="margin-left: 30px">&nbsp;</p>
<div style="margin-left: 30px"><span style="font-family: &quot;Microsoft YaHei&quot;">将 SELINUX=XXX --&gt;XXX 代表级别</span><span style="font-family: &quot;Microsoft YaHei&quot;; line-height: 1.5">改为</span></div>
<p style="margin-left: 30px">&nbsp;</p>
<div style="margin-left: 30px"><span style="font-family: &quot;Microsoft YaHei&quot;">SELINUX=disabled</span></div>
<p style="margin-left: 30px">&nbsp;</p>
<p style="margin-left: 30px"><span style="font-family: &quot;Microsoft YaHei&quot;">然后重启系统,或者执行命令:setenforce 0。</span></p>
<p>&nbsp;</p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">在实验课程和生产环境中设置SELinux域策略时,一定记得添加-P参数,否则服务器在重启后就会按照原有的策略进行控制,从而导致配置过的服务无法使用。</span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">在配置妥当后再使用本地用户尝试登录下FTP服务器,分别执行文件的创建、重命名及删除等命令。操作均成功!</span></p>
<div class="cnblogs_code">
<pre><span style="font-family: &quot;Microsoft YaHei&quot;"># ftp <span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">
Connected to </span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span> (<span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">).
</span><span style="color: rgba(128, 0, 128, 1)">220</span> (vsFTPd <span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">)
Name (</span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">:root): linuxprobe
</span><span style="color: rgba(128, 0, 128, 1)">331</span><span style="color: rgba(0, 0, 0, 1)"> Please specify the password.
Password:此处输入该用户的密码
</span><span style="color: rgba(128, 0, 128, 1)">230</span><span style="color: rgba(0, 0, 0, 1)"> Login successful.
Remote system type </span><span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> UNIX.
Using binary mode to transfer files.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> mkdir files
</span><span style="color: rgba(128, 0, 128, 1)">257</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/home/linuxprobe/files</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> created
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> rename files database
</span><span style="color: rgba(128, 0, 128, 1)">350</span> Ready <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> RNTO.
</span><span style="color: rgba(128, 0, 128, 1)">250</span><span style="color: rgba(0, 0, 0, 1)"> Rename successful.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> rmdir database
</span><span style="color: rgba(128, 0, 128, 1)">250</span><span style="color: rgba(0, 0, 0, 1)"> Remove directory operation successful.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> bye
</span><span style="color: rgba(128, 0, 128, 1)">221</span> Goodbye.</span></pre>
</div>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p><span style="font-size: 18px; font-family: &quot;Microsoft YaHei&quot;"><strong><span style="color: rgba(255, 0, 0, 1); background-color: rgba(0, 255, 255, 1)">3.3&nbsp;虚拟用户模式</span></strong></span></p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<p>我们最后讲解的虚拟用户模式是这三种模式中最安全的一种认证模式,当然,因为安全性较之于前面两种模式有了提升,所以配置流程也会稍微复杂一些。</p>
<p>&nbsp;</p>
<p><strong>第1步</strong>:创建用于进行FTP认证的用户数据库文件,其中奇数行为账户名,偶数行为密码。例如,我们分别创建出zhangsan和lisi两个用户,密码均为redhat:</p>
<p>&nbsp;</p>
<p><span style="font-family: &quot;Microsoft YaHei&quot;">&nbsp;</span></p>
<div class="cnblogs_code">
<pre># cd /etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">
# vim vuser.list
zhangsan
redhat
lisi
redhat</span></pre>
</div>
<p>&nbsp;</p>
<p>但是,明文信息既不安全,也不符合让vsftpd服务程序直接加载的格式,因此需要使用db_load命令用哈希(hash)算法将原始的明文信息文件转换成数据库文件,并且降低数据库文件的权限(避免其他人看到数据库文件的内容),然后再把原始的明文信息文件删除。</p>
<div class="cnblogs_code">
<pre># db_load -T -t hash -<span style="color: rgba(0, 0, 0, 1)">f vuser.list vuser.db
# file vuser.db
vuser.db: Berkeley DB (Hash, version </span><span style="color: rgba(128, 0, 128, 1)">9</span>, native <span style="color: rgba(0, 0, 255, 1)">byte</span>-<span style="color: rgba(0, 0, 0, 1)">order)
# chmod </span><span style="color: rgba(128, 0, 128, 1)">600</span><span style="color: rgba(0, 0, 0, 1)"> vuser.db
# rm </span>-f vuser.list</pre>
</div>
<p><strong>第2步</strong>:<strong>创建vsftpd服务程序用于存储文件的根目录(/var/ftproot)</strong>以及虚拟用户映射的系<strong>统本地用户(virtual)</strong>。FTP服务用于存储文件的根目录指的是,当虚拟用户登录后所访问的默认位置。</p>
<p>由于Linux系统中的每一个文件都有所有者、所属组属性,例如使用虚拟账户“张三”新建了一个文件,但是系统中找不到账户“张三”,就会导致这个文件的权限出现错误。为此,需要再创建一个可以映射到虚拟用户的系统本地用户。简单来说,就是让虚拟用户默认登录到与之有映射关系的这个系统本地用户的家目录中,虚拟用户创建的文件的属性也都归属于这个系统本地用户,从而避免Linux系统无法处理虚拟用户所创建文件的属性权限。</p>
<p>为了方便管理FTP服务器上的数据,可以把这个系统本地用户的家目录设置为/var目录(该目录用来存放经常发生改变的数据)。并且为了安全起见,我们将这个系统本地用户设置为不允许登录FTP服务器,这不会影响虚拟用户登录,而且还可以避免黑客通过这个系统本地用户进行登录。</p>
<div class="cnblogs_code">
<pre># useradd -d /<span style="color: rgba(0, 0, 255, 1)">var</span>/ftproot -s /sbin/nologin <span style="color: rgba(0, 0, 255, 1)">virtual</span><span style="color: rgba(0, 0, 0, 1)">
# ls -ld /<span style="color: rgba(0, 0, 255, 1)">var</span>/ftproot/<span style="color: rgba(0, 0, 0, 1)">
drwx</span>------. <span style="color: rgba(128, 0, 128, 1)">3</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(0, 0, 255, 1)">virtual</span> <span style="color: rgba(128, 0, 128, 1)">74</span> Jul <span style="color: rgba(128, 0, 128, 1)">14</span> <span style="color: rgba(128, 0, 128, 1)">17</span>:<span style="color: rgba(128, 0, 128, 1)">50</span> /<span style="color: rgba(0, 0, 255, 1)">var</span>/ftproot/<span style="color: rgba(0, 0, 0, 1)">
# chmod -Rf <span style="color: rgba(128, 0, 128, 1)">755</span> /<span style="color: rgba(0, 0, 255, 1)">var</span>/ftproot/</pre>
</div>
<p><strong>第3步</strong>:建立用于支持虚拟用户的PAM文件。</p>
<p>PAM(可插拔认证模块)是一种认证机制,通过一些动态链接库和统一的API把系统提供的服务与认证方式分开,使得系统管理员可以根据需求灵活调整服务程序的不同认证方式。要想把PAM功能和作用完全讲透,至少要一个章节的篇幅才可以(对该主题感兴趣的读者敬请关注本书的进阶篇,里面会详细讲解PAM)。</p>
<p>通俗来讲,PAM是一组安全机制的模块,系统管理员可以用来轻易地调整服务程序的认证方式,而不必对应用程序进行任何修改。PAM采取了分层设计(应用程序层、应用接口层、鉴别模块层)的思想,其结构如图11-2所示。</p>
<p><img alt="" data-src="https://img2018.cnblogs.com/blog/1682975/201905/1682975-20190511162633896-1039498008.png"></p>
<p>新建一个用于虚拟用户认证的PAM文件vsftpd.vu,其中PAM文件内的“db=”参数为使用db_load命令生成的账户密码数据库文件的路径,但不用写数据库文件的后缀:</p>
<div class="cnblogs_code">
<pre># vim /etc/pam.d/<span style="color: rgba(0, 0, 0, 1)">vsftpd.vu
auth       required   pam_userdb.so db</span>=/etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">vuser
account    required   pam_userdb.so db</span>=/etc/vsftpd/vuser</pre>
</div>
<p><strong>第4步</strong>:在vsftpd服务程序的主配置文件中通过pam_service_name参数将PAM认证文件的名称修改为vsftpd.vu,PAM作为应用程序层与鉴别模块层的连接纽带,可以让应用程序根据需求灵活地在自身插入所需的鉴别功能模块。当应用程序需要PAM认证时,则需要在应用程序中定义负责认证的PAM配置文件,实现所需的认证功能。</p>
<p>例如,在vsftpd服务程序的主配置文件中默认就带有参数pam_service_name=vsftpd,表示登录FTP服务器时是根据/etc/pam.d/vsftpd文件进行安全认证的。现在我们要做的就是把vsftpd主配置文件中原有的PAM认证文件vsftpd修改为新建的vsftpd.vu文件即可。该操作中用到的参数以及作用如表11-4所示。</p>
<p>表11-4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; 利用PAM文件进行认证时使用的参数以及作用</p>
<div class="cnblogs_code">
<pre><span style="color: rgba(0, 0, 0, 1)">参数    作用
anonymous_enable</span>=<span style="color: rgba(0, 0, 0, 1)">NO    禁止匿名开放模式
local_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES    允许本地用户模式
guest_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES    开启虚拟用户模式
guest_username</span>=<span style="color: rgba(0, 0, 255, 1)">virtual</span><span style="color: rgba(0, 0, 0, 1)">    指定虚拟用户账户
pam_service_name</span>=<span style="color: rgba(0, 0, 0, 1)">vsftpd.vu    指定PAM文件
allow_writeable_chroot</span>=YES    允许对禁锢的FTP根目录执行写入操作,而且不拒绝用户的登录请求</pre>
</div>
<div class="cnblogs_code">
<pre># vim /etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">vsftpd.conf
</span><span style="color: rgba(128, 0, 128, 1)">1</span> anonymous_enable=<span style="color: rgba(0, 0, 0, 1)">NO
</span><span style="color: rgba(128, 0, 128, 1)">2</span> local_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">3</span> guest_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">4</span> guest_username=<span style="color: rgba(0, 0, 255, 1)">virtual</span>
<span style="color: rgba(128, 0, 128, 1)">5</span> allow_writeable_chroot=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">6</span> write_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">7</span> local_umask=<span style="color: rgba(128, 0, 128, 1)">022</span>
<span style="color: rgba(128, 0, 128, 1)">8</span> dirmessage_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">9</span> xferlog_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">10</span> connect_from_port_20=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">11</span> xferlog_std_format=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">12</span> listen=<span style="color: rgba(0, 0, 0, 1)">NO
</span><span style="color: rgba(128, 0, 128, 1)">13</span> listen_ipv6=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">14</span> pam_service_name=<span style="color: rgba(0, 0, 0, 1)">vsftpd.vu
</span><span style="color: rgba(128, 0, 128, 1)">15</span> userlist_enable=<span style="color: rgba(0, 0, 0, 1)">YES
</span><span style="color: rgba(128, 0, 128, 1)">16</span> tcp_wrappers=YES</pre>
</div>
<p><strong>第5步</strong>:为虚拟用户设置不同的权限。虽然账户zhangsan和lisi都是用于vsftpd服务程序认证的虚拟账户,但是我们依然想对这两人进行区别对待。比如,允许张三上传、创建、修改、查看、删除文件,只允许李四查看文件。这可以通过vsftpd服务程序来实现。只需新建一个目录,在里面分别创建两个以zhangsan和lisi命名的文件,其中在名为zhangsan的文件中写入允许的相关权限(使用匿名用户的参数):</p>
<div class="cnblogs_code">
<pre># mkdir /etc/vsftpd/vusers_dir/<span style="color: rgba(0, 0, 0, 1)">
# cd /etc/vsftpd/vusers_dir/<span style="color: rgba(0, 0, 0, 1)">
# touch lisi
# vim zhangsan
anon_upload_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES
anon_mkdir_write_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES
anon_other_write_enable</span>=YES</pre>
</div>
<p>然后再次修改vsftpd主配置文件,通过添加user_config_dir参数来定义这两个虚拟用户不同权限的配置文件所存放的路径。为了让修改后的参数立即生效,需要重启vsftpd服务程序并将该服务添加到开机启动项中:</p>
<div class="cnblogs_code">
<pre># vim /etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">vsftpd.conf
anonymous_enable</span>=<span style="color: rgba(0, 0, 0, 1)">NO
local_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES
guest_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES
guest_username</span>=<span style="color: rgba(0, 0, 255, 1)">virtual</span><span style="color: rgba(0, 0, 0, 1)">
allow_writeable_chroot</span>=<span style="color: rgba(0, 0, 0, 1)">YES
write_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES
local_umask</span>=<span style="color: rgba(128, 0, 128, 1)">022</span><span style="color: rgba(0, 0, 0, 1)">
dirmessage_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES
xferlog_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES
connect_from_port_20</span>=<span style="color: rgba(0, 0, 0, 1)">YES
xferlog_std_format</span>=<span style="color: rgba(0, 0, 0, 1)">YES
listen</span>=<span style="color: rgba(0, 0, 0, 1)">NO
listen_ipv6</span>=<span style="color: rgba(0, 0, 0, 1)">YES
pam_service_name</span>=<span style="color: rgba(0, 0, 0, 1)">vsftpd.vu
userlist_enable</span>=<span style="color: rgba(0, 0, 0, 1)">YES
tcp_wrappers</span>=<span style="color: rgba(0, 0, 0, 1)">YES
user_config_dir</span>=/etc/vsftpd/<span style="color: rgba(0, 0, 0, 1)">vusers_dir
# systemctl restart vsftpd
# systemctl enable vsftpd
ln </span>-s <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/usr/lib/systemd/system/vsftpd.service</span><span style="color: rgba(128, 0, 0, 1)">'</span> <span style="color: rgba(128, 0, 0, 1)">'</span><span style="color: rgba(128, 0, 0, 1)">/etc/systemd/system/multi-user.target.wants/vsftpd.service</span></pre>
</div>
<p><strong>第6步</strong>:设置SELinux域允许策略,然后使用虚拟用户模式登录FTP服务器。相信大家可以猜到,SELinux会继续来捣乱。所以,先按照前面实验中的步骤开启SELinux域的允许策略,以免再次出现操作失败的情况:</p>
<div class="cnblogs_code">
<pre># getsebool -a |<span style="color: rgba(0, 0, 0, 1)"> grep ftp
ftp_home_dir –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_anon_write –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_connect_all_unreserved –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_connect_db –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_full_access –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_cifs –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_fusefs –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_nfs –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
ftpd_use_passive_mode –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
httpd_can_connect_ftp –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
httpd_enable_ftp_server –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_anon_write –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_enable_homedirs –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_full_access –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
sftpd_write_ssh_home –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
tftp_anon_write –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
tftp_home_dir –</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> off
# setsebool -P ftpd_full_access=on</pre>
</div>
<p>此时,不但可以使用虚拟用户模式成功登录到FTP服务器,还可以分别使用账户zhangsan和lisi来检验他们的权限。</p>
<div class="cnblogs_code">
<pre># ftp <span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">
Connected to </span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span> (<span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">).
</span><span style="color: rgba(128, 0, 128, 1)">220</span> (vsFTPd <span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">)
Name (</span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">:root): lisi
</span><span style="color: rgba(128, 0, 128, 1)">331</span><span style="color: rgba(0, 0, 0, 1)"> Please specify the password.
Password:此处输入虚拟用户的密码
</span><span style="color: rgba(128, 0, 128, 1)">230</span><span style="color: rgba(0, 0, 0, 1)"> Login successful.
Remote system type </span><span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> UNIX.
Using binary mode to transfer files.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> mkdir files
</span><span style="color: rgba(128, 0, 128, 1)">550</span><span style="color: rgba(0, 0, 0, 1)"> Permission denied.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> exit
</span><span style="color: rgba(128, 0, 128, 1)">221</span><span style="color: rgba(0, 0, 0, 1)"> Goodbye.
# ftp <span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">
Connected to </span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span> (<span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">).
</span><span style="color: rgba(128, 0, 128, 1)">220</span> (vsFTPd <span style="color: rgba(128, 0, 128, 1)">3.0</span>.<span style="color: rgba(128, 0, 128, 1)">2</span><span style="color: rgba(0, 0, 0, 1)">)
Name (</span><span style="color: rgba(128, 0, 128, 1)">192.168</span>.<span style="color: rgba(128, 0, 128, 1)">10.10</span><span style="color: rgba(0, 0, 0, 1)">:root): zhangsan
</span><span style="color: rgba(128, 0, 128, 1)">331</span><span style="color: rgba(0, 0, 0, 1)"> Please specify the password.
Password:此处输入虚拟用户的密码
</span><span style="color: rgba(128, 0, 128, 1)">230</span><span style="color: rgba(0, 0, 0, 1)"> Login successful.
Remote system type </span><span style="color: rgba(0, 0, 255, 1)">is</span><span style="color: rgba(0, 0, 0, 1)"> UNIX.
Using binary mode to transfer files.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> mkdir files
</span><span style="color: rgba(128, 0, 128, 1)">257</span> <span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(128, 0, 0, 1)">/files</span><span style="color: rgba(128, 0, 0, 1)">"</span><span style="color: rgba(0, 0, 0, 1)"> created
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> rename files database
</span><span style="color: rgba(128, 0, 128, 1)">350</span> Ready <span style="color: rgba(0, 0, 255, 1)">for</span><span style="color: rgba(0, 0, 0, 1)"> RNTO.
</span><span style="color: rgba(128, 0, 128, 1)">250</span><span style="color: rgba(0, 0, 0, 1)"> Rename successful.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> rmdir database
</span><span style="color: rgba(128, 0, 128, 1)">250</span><span style="color: rgba(0, 0, 0, 1)"> Remove directory operation successful.
ftp</span>&gt;<span style="color: rgba(0, 0, 0, 1)"> bye
</span><span style="color: rgba(128, 0, 128, 1)">221</span> Goodbye.</pre>
</div>
<p>&nbsp;</p>
</div>
</div><br><br>
来源:https://www.cnblogs.com/guohongwei/p/10848698.html
頁: [1]
查看完整版本: linux环境:FTP服务器搭建