[vsftp服务]——ftp虚拟用户、权限设置等的实验

搭建ftp服务器,满足以下要求:

      1、允许匿名用户登录服务器并下载文件,下载速度设置为最高2MB/s

      2、不允许本地用户登录ftp服务器

      3、在服务器添加虚拟用户vuser01、vuser02、vuser03,密码自己设置。其中:

           (1)vuser01用户的下载速度最高为3MB/s,vuser02为4MB/s,vuser03为5MB/s;

           (2)vuser01可以进行文件上传,但不能进行其它操作;

           (3)vuser02可以上传和创建目录;

           (4)vuser03可以进行上传、创建文件和删除文件;

           (5)所有虚拟用户只能在/myserver/ftproot目录下活动

      4、设置服务器的最大并发客户数为10,密码输入次数最大为3,每个ip地址最多只能建立5个连接

      5、设置防火墙只允许进行ftp访问和ping测试,不能访问其它任何服务

 

实验环境

    服务器A:10.0.10.158

    服务器B:10.0.100.191

    客户端C:10.0.100.198

 


服务器:安装vsftpd和db_load加密工具

# yum -y install vsftp*
# yum -y install db4-utils

 


要求1:允许匿名用户登录服务器并下载文件,下载速度设置为最高2MB/s

# vim /etc/vsftpd/vsftp.conf
   anonymous_enable=YES         #允许匿名用户登录
   anon_max_rate=2000000        #设置最大下载速度为2MB/s

结果验证:

# ftp 10.0.10.158
Connected to 10.0.10.158 (10.0.10.158).
220 (vsFTPd 2.2.2)
Name (10.0.10.158:root): ftp    ------>#用系统默认的匿名用户ftp登录
331 Please specify the password.
Password:
230 Login successful.
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> ls                                    -------->#匿名用户ftp默认的家目录是/var/ftp
227 Entering Passive Mode (10,0,10,158,118,127).
150 Here comes the directory listing.
-rw-r--r--    1 0        0        104857600 Nov 30 06:42 100m_file    ------>#事先在服务器上传好一个100M大小的文件
drwxr-xr-x    2 0        0            4096 Mar 02  2012 pub
226 Directory send OK.
ftp> get 100m_file
local: 100m_file remote: 100m_file
227 Entering Passive Mode (10,0,10,158,189,24).
150 Opening BINARY mode data connection for 100m_file (104857600 bytes).
226 Transfer complete.
104857600 bytes received in 61.3 secs (1711.39 Kbytes/sec)    ------>#看耗时和下载速度
 
ftp> put /etc/passwd       ------>#试图上传一个文件,deny
local: /etc/passwd remote: /etc/passwd
227 Entering Passive Mode (10,0,10,158,126,52).
550 Permission denied.

ftp> mkdir aa           ------>#试图建立目录,deny
550 Permission denied.

 


要求2:在服务器添加虚拟用户vuser01、vuser02、vuser03,限制在/myserver/ftproot目录下活动。且每个虚拟用户有不同的权限要求。

1.在配置文件中添加对虚拟用户的支持

# vim /etc/vsftpd/vsftp.conf
   guest_enable=YES                      #实体用户均被假设成‘guest’登录
   guest_username=virtftp                #这个‘guest’被映射为本地的‘virtftp’用户
   pam_service_name=vsftpd               #设置在PAM所使用的名称,默认值为vsftpd
   user_config_dir=/etc/vsftpd/virt_dir  #虚拟用户的单独配置信息设置放在/etc/vsftpd/virt_dir下

2.编辑虚拟用户名和密码的文本文件(奇数行是用户名,偶数行是密码)

# vim /etc/vsftpd/virt_user.txt
   vuser01     \\用户名
   123123       \\密码
   vuser02
   123123
   vuser03
   123123

3.将文本文件生成数据库文件

# db_load -T -t  hash  -f  /etc/vsftpd/virt_user.tct /etc/vsftpd/virt_user.db

4.创建PAM认证文件

# vim  /etc/pam.d/vsftpd 
#%PAM-1.0
auth     sufficient     /lib/security/pam_userdb.so db=/etc/vsftpd/virt_user  --->#我的机子是32位的所以是/lib(64位的机子要写成/lib64)
account  sufficient    /lib/security/pam_userdb.so db=/etc/vsftpd/virt_user      

5.创建本地用户virtftp(我们在配置文件中已经写了,虚拟用户都映射为本地用户virtftp,且要求虚拟用户的家目录为/myserver/ftproot,所以virtftp的家目录也应该是这个)

# useradd -d /myserver/ftproot/ -s /sbin/nologin virtftp  #设置家目录为/myserver/ftproot,shell为不可登录
# ll -d /myserver/ftproot/
  drwx------. 3 virtftp virtftp 4096 11月 29 20:17 /myserver/ftproot/
# chmod 755 /myserver/ftproot/    #将家目录的权限改成755
# ll -d /myserver/ftproot/
  drwxr-xr-x. 3 virtftp virtftp 4096 11月 29 20:17 /myserver/ftproot/   #家目录的权限和属主属组一定要正确

 6.创建/etc/vsftpd/virt_dir目录,在其中写每个用户的不同权限配置要求(为什么是/etc/vsftpd/virt_dir这个目录,也是因为我们在配置文件中设置好的)

# mkdir /etc/vsftpd/virt_dir
# cd /etc/vsftpd/virt_dir
# vim vuser01                    ------>#在里面写虚拟用户vuser01的相关配置
  local_root=/myserver/ftproot   ------>#用户家目录
  anon_upload_enable=YES         ------>#允许上传
  anon_max_rate=3000000          ------>#设置最大不超过3MB/s
# vim vuser02
  local_root=/myserver/ftproot
  anon_upload_enable=YES          ------>#允许上传
  anon_mkdir_write_enable=YES     ------>#允许创建目录
  anon_max_rate=4000000
# vim  vuser03
  local_root=/myserver/ftproot
  anon_upload_enable=YES       
  anon_mkdir_write_enable=YES
  anon_other_write_ebale=YES    ------>#允许有‘写’以外的权限
  anon_max_rate=5000000

7.注意服务器上防火墙和SELinux的设置(无论在服务器还是客户端,若出现配置无问题但总是不成功就要考虑到这两个的设置)

# iptables  -F
# setenforce  0
# getsebool -a | grep ftp
  allow_ftpd_anon_write --> off
  allow_ftpd_full_access --> off
  allow_ftpd_use_cifs --> off
  allow_ftpd_use_nfs --> off
  ftp_home_dir --> off
  ftpd_connect_db --> off
  ftpd_use_passive_mode --> off
  httpd_enable_ftp_server --> off
  tftp_anon_write --> off
# setsebool ftp_home_dir 1
# setsebool tftp_anon_write 1
# setsebool allow_ftpd_anon_write 1
# setsebool allow_ftpd_full_access 1
# getsebool -a | grep ftp
  allow_ftpd_anon_write --> on
  allow_ftpd_full_access --> on
  allow_ftpd_use_cifs --> off
  allow_ftpd_use_nfs --> off
  ftp_home_dir --> on
  ftpd_connect_db --> off
  ftpd_use_passive_mode --> off
  httpd_enable_ftp_server --> off
  tftp_anon_write --> on

8.还有一个要求是“不允许本地用户登录”,但是配置文件中不能直接就写成"local_enable=NO",因为虚拟用户还要登录(即映射在本地的virtftp要能登录)。所以利用user_list来实现这一要求

# vim /etc/vsftpd/vsftp.conf
  local_enable=YES    
  userlist_enable=YES  ------>#启用user_list文件
  userlist_deny=NO     ------>#userlist文件变成白名单!表示只允许userlist列表中的用户登录


# vim /etc/vsftp/userlist  ------>#在userlist中写入允许登录的用户(即虚拟用户) (注意并不是写virtftp)
  vuser01
  vuser02
  vuser03

 


要求3:服务器的最大并发客户数为10,密码输入次数最大为3,每个ip地址最多只能建立5个连接

# vim /etc/vsftpd/vsftp.conf
  max_clients=10  ------>#最大并发客户连接数
  max_per_ip=5    ------>#每个IP最大连接数

 


验证

(1) vuser02用户验证

# ftp 10.0.10.158
Connected to 10.0.10.158 (10.0.10.158).
220 (vsFTPd 2.2.2)
Name (10.0.10.158:root): vuser02  ------>#以vuser02用户登录
331 Please specify the password.
Password:
230 Login successful.      ------>#可登录
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> mkdir aa     ------>#可创建目录
257 "/aa" created
ftp> ls
227 Entering Passive Mode (10,0,10,158,131,117).
150 Here comes the directory listing.
-rw-r--r--    1 0        0        104857600 Nov 30 06:48 100m_file
drwx------    2 503      503          4096 Nov 30 08:19 aa
drwxr-xr-x    2 0        0            4096 Nov 30 04:56 test
226 Directory send OK.
ftp> put /test.txt  ------>#可上传文件
local: /test.txt remote: /test.txt
227 Entering Passive Mode (10,0,10,158,106,249).
150 Ok to send data.
226 Transfer complete.
ftp> ls
227 Entering Passive Mode (10,0,10,158,188,10).
150 Here comes the directory listing.
-rw-r--r--    1 0        0        104857600 Nov 30 06:48 100m_file
drwx------    2 503      503          4096 Nov 30 08:19 aa
drwxr-xr-x    2 503      503          4096 Nov 30 09:52 test
-rw-------    1 503      503             0 Nov 30 09:55 test.txt
226 Directory send OK.

(2) vuser01用户验证 

# ftp 10.0.10.158
Connected to 10.0.10.158 (10.0.10.158).
220 (vsFTPd 2.2.2)
Name (10.0.10.158:root): vuser01    ------>#用vuser01登录
331 Please specify the password.
Password:
230 Login successful.     ------>#可登录
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> put /test2.txt       ------>#可上传
local: /test2.txt remote: /test2.txt
227 Entering Passive Mode (10,0,10,158,94,158).
150 Ok to send data.
226 Transfer complete.
ftp> ls
227 Entering Passive Mode (10,0,10,158,208,4).
150 Here comes the directory listing.
-rw-r--r--    1 0        0        104857600 Nov 30 06:48 100m_file
drwx------    2 503      503          4096 Nov 30 08:19 aa
drwxr-xr-x    2 503      503          4096 Nov 30 09:52 test
-rw-------    1 503      503             0 Nov 30 09:55 test.txt
-rw-------    1 503      503             0 Nov 30 09:57 test2.txt
226 Directory send OK.
ftp> mkdir aa          ------>#不可新建目录
550 Permission denied.

(3) vuser03用户验证

# ftp 10.0.10.158
Connected to 10.0.10.158 (10.0.10.158).
220 (vsFTPd 2.2.2)
Name (10.0.10.158:root): vuser03    ------>#用vuser03登录
331 Please specify the password.
Password:
230 Login successful.    ------>#可登录
Remote system type is UNIX.
Using binary mode to transfer files.
ftp> put test3.txt      ------>#可上传
local: test3.txt remote: test3.txt
227 Entering Passive Mode (10,0,10,158,89,248).
150 Ok to send data.
226 Transfer complete.
ftp> mkdir bb       ------>#可新建目录
257 "/bb" created
ftp> rm bb          ------>#可删除目录(但貌似只能删除自己创建的目录??)
250 Remove directory operation successful.
ftp> delete test2.txt   ------>#可删除文件
250 Delete operation successful.
ftp> delete test.txt
250 Delete operation successful.
ftp> get 100m_file 
local: 100m_file remote: 100m_file
227 Entering Passive Mode (10,0,10,158,19,173).
150 Opening BINARY mode data connection for 100m_file (104857600 bytes).
226 Transfer complete.
104857600 bytes received in 21.6 secs (4856.31 Kbytes/sec)    ------>#下载的速度也符合设置

(4)本地普通用户验证

# ftp 10.0.10.158
Connected to 10.0.10.158 (10.0.10.158).
220 (vsFTPd 2.2.2)
Name (10.0.10.158:root): user_00    ------>#拒绝了本地用户user_00的登录
530 Permission denied.
Login failed.

 


 

补充

1.贴出该实验中的配置文件全部的有效选项

# cat vsftpd.conf | grep -v "^#" | grep -v "^$"
  anonymous_enable=YES
  local_enable=YES
  write_enable=YES
  local_umask=022
  anon_upload_enable=YES
  anon_max_rate=2000000
  dirmessage_enable=YES
  xferlog_enable=YES
  connect_from_port_20=YES
  xferlog_std_format=YES
  listen=YES
  guest_enable=YES
  guest_username=virtftp
  pam_service_name=vsftpd
  user_config_dir=/etc/vsftpd/virt_dir
  userlist_enable=YES
  userlist_deny=NO
  tcp_wrappers=YES
  max_clients=10
  max_per_ip=5
  max_login_fails=3

2.实验中出现的错误记录,请参考:

posted @ 2016-06-28 20:35  Jelly_lyj  阅读(3508)  评论(0编辑  收藏  举报