Linux:ftp文件共享服务实战之虚拟用户设置/用户访问权限设置

作业14:ftp文件共享服务实战

14.0 实验环境

软件IP系统
客户端192.168.6.146Centos 6.9
服务端192.168.6.128Centos 7.0
  1. 服务端

    • 卸载vsftpd

      #通过检索,找不到重启失败的源头,所以我索性将服务删除干净,重新安装服务
      [root@ /etc/vsftpd 05:49:26]#yum remove vsftpd
      [root@ /etc/vsftpd 05:50:10]#find / -name "vsftpd"
      [root@ /etc/vsftpd 05:50:10]#find / -name "vsftpd"
      /var/www/cobbler/ks_mirror/ubuntu1064-x86_64/pool/main/v/vsftpd
      /etc/vsftpd
      [root@ /etc/vsftpd 05:50:41]#rm -rf /etc/vsftpd
      [root@ /etc/vsftpd 05:50:49]#rm -rf /var/www/cobbler/ks_mirror/ubuntu1064-x86_64/pool/main/v/vsftpd
      
      [root@ /etc/vsftpd 05:50:55]#yum -y install vsftpd
      [root@ /etc/vsftpd 05:51:16]#systemctl start vsftpd.service
      
    • 防火墙设置/selinux关闭

      #1.selinux设置
      [root@ /etc/vsftpd 08:24:14]#setenforce 0
      setenforce: SELinux is disabled
      [root@ /etc/vsftpd 05:51:28]#getenforce
      Disabled
      
      #2.防火墙设置
      [root@ ~ 08:28:42]#firewall-cmd --zone=public --add-port=21/tcp --permanent
      success
      [root@ ~ 08:32:12]#firewall-cmd --zone=public --add-port=60000-60500/tcp --permanent
      success
      [root@ ~ 08:32:53]#firewall-cmd --add-service=ftp --permanent
      success
      [root@ ~ 08:33:11]#firewall-cmd --reload
      success
      [root@ ~ 08:33:40]#firewall-cmd --list-services|grep ftp
      ssh dhcpv6-client ftp
      
    • 普通用户创建

      #1.创建用户
      [root@ ~ 08:48:04]#useradd  -d /var/ftproot -s /sbin/nologin vuser
      Creating mailbox file: File exists
      
      #2.调整普通用户vuser进行FTP登陆后在根目录的权限
      [root@ ~ 08:48:07]#chmod +rx /var/ftproot #普通用户可以有在根目录下下载/读取文件的权限
      [root@ ~ 08:48:43]#chmod -w /var/ftproot #普通用户在根目录下不能修改/上传文件
      
      #3.创建额外的目录供普通用户上传下载文件
      [root@ ~ 08:49:44]#mkdir /var/ftproot/upload -p 
      
      [root@ ~ 11:45:19]#chmod o+w /var/ftproot/upload/
      [root@ ~ 08:50:03]#setfacl -m u:vuser:rwx /var/ftproot/upload/
      
  2. 客户端

    #安装ftp客户端服务
    [root@lin ~]# yum -y intstall ftp
    
    #防火墙和selinux设置
    [root@lin ~]# getenforce
    Enforcing
    [root@lin ~]# setenforce 0 
    [root@lin ~]# getenforce
    Permissive
    [root@lin ~]# iptables -F
    
    

14.1 搭建只允许匿名用户访问文件共享服务器

  • 服务器端配置

    [root@ ~ 04:26:20]#vim /etc/vsftpd/vsftpd.conf 
    local_enable=NO            #注意:NO/YES后面绝对不能带有空格,否则vsftpd服务重启报错
    anonymous_enable=YES
    [root@ ~ 08:37:53]#systemctl restart vsftpd
    
  • 客户端测试

    #1.测试匿名登陆
    [root@lin ~]# ftp 192.168.6.128
    Connected to 192.168.6.128 (192.168.6.128).
    220 (vsFTPd 3.0.2)
    Name (192.168.6.128:root): anonymous      #匿名登陆名
    331 Please specify the password.
    Password:								  #不需要密码
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    ftp> 
    
    #2.测试普通用户登陆
    [root@lin ~]# ftp 192.168.6.128
    Connected to 192.168.6.128 (192.168.6.128).
    220 (vsFTPd 3.0.2)
    Name (192.168.6.128:root): vuser
    530 This FTP server is anonymous only.  #被拒绝,提示只能匿名登陆
    Login failed.
    ftp> 
    
    

14.2 让本地用户登陆时访问指定目录,并不能跳出此目录

  • 服务器端配置

    [root@ ~ 08:55:58]#vim /etc/vsftpd/vsftpd.conf 
    anonymous_enable=NO            #禁止匿名登陆
    local_enable=YES			   #允许本地用户登录
    local_root=/var/ftproot        #本地用户登陆目录指定
    chroot_local_user=YES          #如果为NO,客户端就能跳出此目录,且客户端显示当前目录路径为服务器端系统的绝对路径,而不是 ‘/’
    chroot_list_enable=YES
    [root@ ~ 10:25:02]#systemctl restart vsftpd
    
  • 客户端测试

    #测试普通用户vuser,测试成功,无法跳出根目录
    [root@lin ~]# ftp 192.168.6.128
    Connected to 192.168.6.128 (192.168.6.128).
    220 (vsFTPd 3.0.2)
    Name (192.168.6.128:root): vuser  							 #测试用户vuser
    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,6,128,199,165).
    150 Here comes the directory listing.
    drwxrwxr-x    2 0        0            4096 Sep 28 12:50 upload
    226 Directory send OK.
    ftp> cd ..
    250 Directory successfully changed.
    ftp> pwd
    257 "/"            
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,117,122).
    150 Here comes the directory listing.
    drwxrwxr-x    2 0        0            4096 Sep 28 12:50 upload  #无法跳出根目录
    226 Directory send OK.
    ftp> 
    
    #测试xixi用户,测试成功,登陆到指定目录
    [root@lin ~]# ftp 192.168.6.128
    Connected to 192.168.6.128 (192.168.6.128).
    220 (vsFTPd 3.0.2)
    Name (192.168.6.128:root): xixi   							
    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,6,128,37,119).
    150 Here comes the directory listing.
    drwxrwxr-x    2 0        0            4096 Sep 28 12:50 upload 
    				#同一个文件,登录到同一个目录/var/ftproot,而不是xixi用户所在的家目录
    226 Directory send OK.
    ftp> pwd
    

14.3 让匿名对某目录(/var/ftp/upload)有权限上传文件

  • 服务端

    #1.修改权限
    [root@ ~ 11:48:51]#ll /var/ |grep ftp
    drwxr-xr-x.  4 root  root  4096 Sep 28 17:51 ftp
    [root@ ~ 11:48:58]#mkdir -p /var/ftp/upload
    [root@ ~ 11:49:44]#chmod o+w /var/ftp/upload
    #2.修改配置文件
    [root@ ~ 11:50:23]#vim /etc/vsftpd/vsftpd.conf
    anonymous_enable=YES         #允许匿名登陆
    anon_upload_enable=YES       #允许匿名上传文件
    anon_mkdir_write_enable=YES   #允许匿名写文件
    #3.重启服务
    [root@ ~ 11:52:49]#systemctl restart vsftpd
    
    
  • 客户端

    [root@lin ~]# touch 20200928
    [root@lin ~]# echo "pinglinglab">> 20200928 
    						  #客户端当前目录下创建一个文件,准备用来测试ftp上传
    [root@lin ~]# cat  20200928 
    
    [root@lin ~]# ftp 192.168.6.128  							 #ftp连接
    Connected to 192.168.6.128 (192.168.6.128).
    220 (vsFTPd 3.0.2)
    Name (192.168.6.128:root): anonymous						#匿名登陆
    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,6,128,157,190).
    150 Here comes the directory listing.
    -rw-r--r--    1 0        0              46 Jun 30 07:29 aa
    -rw-r--r--    1 0        0             545 Aug 08 09:02 fstab
    drwxr-xr-x    2 0        0            4096 Apr 01 04:55 pub
    drwxr-xrwx    2 0        0            4096 Sep 23 13:00 temp
    drwxr-xrwx    2 0        0            4096 Sep 28 15:49 upload
    226 Directory send OK.
    ftp> put 20200928											#尝试上传文件			
    local: 20200928 remote: 20200928
    227 Entering Passive Mode (192,168,6,128,226,156).
    553 Could not create file.									#尝试失败
    ftp> cd upload											#进入能上传文件的目录
    250 Directory successfully changed.
    ftp> put 20200928										#尝试上传文件
    local: 20200928 remote: 20200928
    227 Entering Passive Mode (192,168,6,128,164,48).
    150 Ok to send data.
    226 Transfer complete.
    12 bytes sent in 6.2e-05 secs (193.55 Kbytes/sec)
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,90,67).
    150 Here comes the directory listing.
    -rw-------    1 14       50             12 Sep 28 16:07 20200928 #上传成功
    226 Directory send OK.
    ftp> mkdir ccc											#尝试创建目录
    257 "/upload/ccc" created
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,127,243).
    150 Here comes the directory listing.
    -rw-------    1 14       50             12 Sep 28 16:07 20200928
    drwx------    2 14       50           4096 Sep 28 16:07 ccc  #创建目录成功
    226 Directory send OK.
    ftp> 
    
    

14.4 让某普通用户对某个目录具有上传权限

  • 服务器端

    #修改好指定ftp登陆目录下upload目录权限,也可以chmod o+w /var/ftproot
    [root@ ~ 12:35:38]#ll /var/ftproot/
    total 4
    drwxrwxr-x+ 2 root root 4096 Sep 29 00:34 upload
    
    [root@ ~ 12:33:35]#vim /etc/vsftpd/vsftpd.conf 
    local_enable=YES
    local_root=/var/ftproot  #普通用户指定登陆这个服务器下的目录
    write_enable=YES         #写允许
    
  • 用户端

    [root@lin ~]# ftp 192.168.6.128
    Connected to 192.168.6.128 (192.168.6.128).
    220 (vsFTPd 3.0.2)
    Name (192.168.6.128:root): vuser           #登陆vuser普通用户
    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,6,128,231,154).
    150 Here comes the directory listing.
    drwxrwxr-x    2 0        0            4096 Sep 28 12:50 upload
    226 Directory send OK.   
    ftp> put 20200928						   #尝试根目录上传文件
    local: 20200928 remote: 20200928
    227 Entering Passive Mode (192,168,6,128,77,108).
    553 Could not create file.                 #尝试失败
    ftp> cd upload                             #进入upload目录
    250 Directory successfully changed.
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,46,243).
    150 Here comes the directory listing.
    226 Directory send OK.
    ftp> put 20200928						   #尝试上传文件
    local: 20200928 remote: 20200928
    227 Entering Passive Mode (192,168,6,128,169,53).
    150 Ok to send data.
    226 Transfer complete.                     #上传文件成功
    12 bytes sent in 4.1e-05 secs (292.68 Kbytes/sec)
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,189,184).
    150 Here comes the directory listing.
    -rw-r--r--    1 1020     1022           12 Sep 28 16:34 20200928
    226 Directory send OK.
    ftp> 
    
    

14.5 虚拟用户

所谓虚拟用户就是,所有虚拟用户会统一映射为一个指定的系统普通账号:访问共享位置,即为此系统普通用户的家目录,当然每个虚拟用户也可被赋予不同的访问权限,通过匿名用户的权限控制参数进行指定

  • 服务器端
  1. 创建用户数据库文件

    [root@ /etc/vsftpd 10:16:10]#vim vuser.txt
    lpz              #第一个用户
    123456			#第一个用户密码
    csx				#第二个用户
    123456
    
  2. 将用户数据库文件格式化

    #  创建专门一个文件存放用户密码,但是该文件需要用hash格式
    [root@ /etc/vsftpd 10:20:20]#db_load -T -t hash -f vuser.txt vuser.db
    [root@ /etc/vsftpd 10:21:26]#chmod 600 vuser.*
    [root@ /etc/vsftpd 10:21:40]#ll |grep vuser
    -rw------- 1 root root 12288 Sep 29 10:21 vuser.db
    -rw------- 1 root root    23 Sep 29 10:17 vuser.txt
    
    
  3. 创建系统普通用户+FTP登陆访问的目录

    参考章节:14.0
    
  4. 主配置文件调用vsftpd.conf,创建PAM模块配置文件,用于登陆验证

    [root@ /etc/vsftpd 10:21:51]#vim /etc/pam.d/vsftpd.db
    auth required pam_userdb.so db=/etc/vsftpd/vuser
    account required pam_userdb.so db=/etc/vsftpd/vuser
    
    [root@ /etc/vsftpd 10:25:39]#vim /etc/vsftpd/vsftpd.conf 
    #必调参数
    anonymous_enable=NO			
    local_enable=YES
    guest_enable=YES
    guest_username=vuser
    pam_service_name=vsftpd.db  #原先是vsftpd,都是在/etc/pam.d下的文件
    
    #以下统一调整登陆虚拟用户权限(也可以为不同虚拟用户设置不同权限,后面有讲,此主配置文件处就不需要设置参数)
    local_root=/var/ftproot    #统一登陆目录 
    anon_upload_enable=YES		#允许上传
    anon_mkdir_write_enable=YES #允许创建文件
    anon_other_write_enable=YES #允许删除文件
    #重启服务
    [root@ /etc/vsftpd 11:35:57]#systemctl restart vsftpd
    
    
  • 客户端测试

    #登陆测试
    [root@lin ~]# ftp 192.168.6.128
    Connected to 192.168.6.128 (192.168.6.128).
    220 (vsFTPd 3.0.2)
    Name (192.168.6.128:root): lpz      #lpz用户登陆
    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,6,128,210,106).
    150 Here comes the directory listing.
    drwxrwxrwx    2 0        0            4096 Sep 28 16:34 upload
    226 Directory send OK.
    ftp> put 20200929					#尝试根目录上传文件
    local: 20200929 remote: 20200929
    227 Entering Passive Mode (192,168,6,128,163,157).
    553 Could not create file.			#尝试失败
    ftp> cd upload						#进入upload目录
    250 Directory successfully changed.
    ftp> put 20200929					#尝试上传文件
    local: 20200929 remote: 20200929
    227 Entering Passive Mode (192,168,6,128,60,198).
    150 Ok to send data.				#尝试成功
    226 Transfer complete.
    12 bytes sent in 0.000118 secs (101.69 Kbytes/sec)
    ftp> ls								#查看upload目录下文件
    227 Entering Passive Mode (192,168,6,128,58,13).
    150 Here comes the directory listing.
    -rw-r--r--    1 1020     1022           12 Sep 28 16:34 20200928
    -rw-------    1 1017     1019           12 Sep 29 03:46 20200929
    226 Directory send OK.
    ftp> 
    
    
    
  • 服务器端:虚拟用户不同权限设置

  1. 给每一个虚拟用户创建一个独立的权限配置文件

    [root@ /etc/vsftpd 11:45:36]#mkdir -p /etc/vsftpd/vuser.d
    [root@ /etc/vsftpd 11:55:59]#cd /etc/vsftpd/vuser.d
    [root@ /etc/vsftpd/vuser.d 11:56:13]#vim lpz
    anon_upload_enable=YES           #允许lpz用户上传文件
    anon_mkdir_write_enable=YES 
    anon_other_write_enable=YES 
    #local_root=/var/lpzroot
    #/var/lpzroot目录不能有写权限
    #/var/lpzroot目录下为lpz用户单独创建共享目录,权限改为766,让Other有读写权限
    
    [root@ /etc/vsftpd/vuser.d 11:58:23]#vim csx
    anon_other_write_enable=YES  #允许csx用户删除文件
    
  2. 修改vsftpd服务主配置文件

    [root@ /etc/vsftpd/vuser.d 12:01:34]#vim /etc/vsftpd/vsftpd.conf 
    user_config_dir=/etc/vsftpd/vuser.d
    [root@ /etc/vsftpd/vuser.d 12:06:10]#systemctl restart vsftpd
    
  • 客户端

    #1.csx账号测试
    #登入测试:成功
    [root@lin ~]# ftp 192.168.6.128
    Connected to 192.168.6.128 (192.168.6.128).
    220 (vsFTPd 3.0.2)
    Name (192.168.6.128:root): csx
    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.
    
    #查看upload目录文件
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,161,255).
    150 Here comes the directory listing.
    -rw-r--r--    1 1020     1022           12 Sep 28 16:34 20200928
    -rw-------    1 1017     1019           12 Sep 29 03:46 20200929
    226 Directory send OK.
    
    #上传文件测试:失败
    ftp> put 20200930
    local: 20200930 remote: 20200930
    227 Entering Passive Mode (192,168,6,128,216,141).
    550 Permission denied.
    
    #删除文件测试:成功
    ftp> delete 20200928
    250 Delete operation successful.
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,173,11).
    150 Here comes the directory listing.
    -rw-------    1 1017     1019           12 Sep 29 03:46 20200929
    226 Directory send OK.
    ftp> 
    
    #创建目录测试:测试成功,不能创建目录
    ftp> mkdir aaaaa
    550 Permission denied. m
    
    
    #2.lpz账号设置
    
    #登入测试:成功
    [root@lin ~]# ftp 192.168.6.128
    Connected to 192.168.6.128 (192.168.6.128).
    220 (vsFTPd 3.0.2)
    Name (192.168.6.128:root): lpz
    331 Please specify the password.
    Password:
    230 Login successful.
    Remote system type is UNIX.
    Using binary mode to transfer files.
    
    ftp> !ls #查看本机文件
    20200930  nginx-1.14.0	nginx-1.14.0.tar.gz  nginx.conf  objs  Script_File
    #上传文件测试:测试成功,不能上传到根目录
    ftp> put 20200930
    local: 20200930 remote: 20200930
    227 Entering Passive Mode (192,168,6,128,33,145).
    553 Could not create file.
    
    #上传文件测试:成功
    ftp> cd upload
    250 Directory successfully changed.
    ftp> put 20200930  #上传文件
    local: 20200930 remote: 20200930
    227 Entering Passive Mode (192,168,6,128,86,219).
    150 Ok to send data.
    226 Transfer complete.
    12 bytes sent in 0.000154 secs (77.92 Kbytes/sec)
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,26,192).
    150 Here comes the directory listing.
    -rw-------    1 1017     1019           12 Sep 29 03:46 20200929
    -rw-------    1 1017     1019           12 Sep 29 05:35 20200930
    226 Directory send OK.
    
    #创建目录测试:成功
    ftp> mkdir aaaaaaaaaaaaaaa
    257 "/upload/aaaaaaaaaaaaaaa" created
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,148,130).
    150 Here comes the directory listing.
    -rw-------    1 1017     1019           12 Sep 29 03:46 20200929
    -rw-------    1 1017     1019           12 Sep 29 05:35 20200930
    drwx------    2 1017     1019         4096 Sep 29 05:35 aaaaaaaaaaaaaaa
    226 Directory send OK.
    
    #下载文件测试:失败测试, 需要修改:anon_world_readable_only=NO即可 (默认YES)
    ftp> get 20200929
    local: 20200929 remote: 20200929
    227 Entering Passive Mode (192,168,6,128,106,201).
    550 Failed to open file.
    
    #删除文件测试:成功
    ftp> delete 20200929
    250 Delete operation successful.
    ftp> ls
    227 Entering Passive Mode (192,168,6,128,157,228).
    150 Here comes the directory listing.
    -rw-------    1 1017     1019           12 Sep 29 05:35 20200930
    drwx------    2 1017     1019         4096 Sep 29 05:35 aaaaaaaaaaaaaaa
    226 Directory send OK.
    ftp> 
    

14.6 报错解决

  1. 客户端登陆报错:

    500 OOPS: bad bool value in config file for: anon_upload_enable
    Login failed.
    421 Service not available, remote server has closed connection

    解决:每个参数设置后面不能带有空格,排查指定参数anon_upload_enable所在文件,修改格式,重启服务成功;登陆根目录不能有写权限

  • 1
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值