zoukankan      html  css  js  c++  java
  • Linux基础学习-使用vsftpd服务传输文件

    使用vsftpd服务传输文件

    1 安装vsftpd

    [root@qdlinux ~]# yum install vsftpd
    Loaded plugins: product-id, search-disabled-repos, subscription-manager
    This system is not registered with an entitlement server. You can use subscription-manager to register.
    Resolving Dependencies
    --> Running transaction check
    ---> Package vsftpd.x86_64 0:3.0.2-22.el7 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    =======================================================================================================================
     Package                    Arch                       Version                           Repository               Size
    =======================================================================================================================
    Installing:
     vsftpd                     x86_64                     3.0.2-22.el7                      dvd                     169 k
    
    Transaction Summary
    =======================================================================================================================
    Install  1 Package
    
    Total download size: 169 k
    Installed size: 348 k
    Is this ok [y/d/N]: y
    Downloading packages:
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : vsftpd-3.0.2-22.el7.x86_64                                                                          1/1 
      Verifying  : vsftpd-3.0.2-22.el7.x86_64                                                                          1/1 
    
    Installed:
      vsftpd.x86_64 0:3.0.2-22.el7                                                                                         
    
    Complete!
    [root@qdlinux ~]# 
    
    

    2 安装ftp lftp客户端软件

    [root@qdlinux ~]# yum install ftp lftp -y
    Loaded plugins: product-id, search-disabled-repos, subscription-manager
    This system is not registered with an entitlement server. You can use subscription-manager to register.
    Resolving Dependencies
    --> Running transaction check
    ---> Package ftp.x86_64 0:0.17-67.el7 will be installed
    ---> Package lftp.x86_64 0:4.4.8-8.el7_3.2 will be installed
    --> Finished Dependency Resolution
    
    Dependencies Resolved
    
    =======================================================================================================================
     Package                  Arch                       Version                             Repository               Size
    =======================================================================================================================
    Installing:
     ftp                      x86_64                     0.17-67.el7                         dvd                      61 k
     lftp                     x86_64                     4.4.8-8.el7_3.2                     dvd                     752 k
    
    Transaction Summary
    =======================================================================================================================
    Install  2 Packages
    
    Total download size: 812 k
    Installed size: 2.5 M
    Downloading packages:
    -----------------------------------------------------------------------------------------------------------------------
    Total                                                                                  8.7 MB/s | 812 kB  00:00:00     
    Running transaction check
    Running transaction test
    Transaction test succeeded
    Running transaction
      Installing : lftp-4.4.8-8.el7_3.2.x86_64                                                                         1/2 
      Installing : ftp-0.17-67.el7.x86_64                                                                              2/2 
      Verifying  : ftp-0.17-67.el7.x86_64                                                                              1/2 
      Verifying  : lftp-4.4.8-8.el7_3.2.x86_64                                                                         2/2 
    
    Installed:
      ftp.x86_64 0:0.17-67.el7                                lftp.x86_64 0:4.4.8-8.el7_3.2                               
    
    Complete!
    
    

    3 启动服务并加入开机自启动

    [root@qdlinux ~]# systemctl start vsftpd
    [root@qdlinux ~]# systemctl enable vsftpd
    Created symlink from /etc/systemd/system/multi-user.target.wants/vsftpd.service to /usr/lib/systemd/system/vsftpd.service.
    
    

    4 查看监听端口状态

    [root@qdlinux ~]# ss -lntup | grep vsftpd
    tcp    LISTEN     0      32       :::21                   :::*                   users:(("vsftpd",pid=1411,fd=3))
    
    

    使用匿名开放模式

    服务启动后默认访问的目录为/var/ftp,且匿名用户没有上传、创建、重命名、删除功能.但是具有下载功能.

    服务开启后直接访问

    image

    创建文件夹被拒绝

    image

    重命名失败

    image

    我们来修改配置文件让匿名用户可以上传文件,编辑配置文件vim /etc/vsftpd/vsftpd.conf,修改为如下内容

    anon_upload_enable=YES
    anon_umask=022
    
    

    现在依然无法上传文件,这是因为ftp的权限问题导致的,我们将其修改为ftp用户权限.

    注意:默认匿名用户家目录/var/ftp的权限是755,这个权限是不能改变的,切记!

    [root@qdlinux var]# ll -d ftp
    drwxr-xr-x 3 root root 30 Aug 13 21:32 ftp
    
    [root@qdlinux var]# chown -Rf ftp /var/ftp/pub
    
    [root@qdlinux var]# ll ftp
    total 0
    -rw-r--r-- 1 root root  0 Aug 13 21:32 1.txt
    drwxr-xr-x 2 ftp  root 19 Aug 13 21:32 pub
    
    

    现在已经可以上传文件了,但是只能上传到ftp/pub目录中,因为刚才给的就是这个目录的权限.现在就做到了只能上传文件和下载的目的,但是不能创建文件夹.

    上传文件

    image

    重命名失败

    image

    创建文件夹被拒绝

    image

    [root@qdlinux var]# ll ftp/pub
    total 0
    -rw-r--r-- 1 ftp root 0 Aug 13 21:32 2.txt
    -rw-r--r-- 1 ftp ftp  0 Aug 13 21:51 new.txt
    [root@qdlinux var]# 
    

    如果需要开启匿名用户可以创建文件夹功能,修改配置文件如下所示.

    anon_upload_enable=YES
    anon_mkdir_write_enable=YES
    anon_umask=022
    
    

    现在匿名用户可以上传文件和下载文件以及创建文件夹.但是不能重命名.

    如果需要开启匿名用户上传下载和创建文件夹以及重命名和删除功能,修改配置文件如下所示.

    anon_upload_enable=YES
    anon_mkdir_write_enable=YES
    anon_other_write_enable=YES
    anon_umask=022
    
    

    现在匿名用户可以做到上传和下载,创建文件夹和删除文件夹和文件以及重命名.

    上传文件
    image

    新建文件夹
    image

    这是匿名用户的最大权限了,一般匿名访问的话,只要做到下载功能就可以了。网站提供什么用户下载什么。


    使用本地用户模式

    示例演示公司内部有一台FTP和WEB服务器,FTP的功能主要用于维护网站内容,用于上传文件、创建目录、更新网页等等。公司有两个部门负责维护任务,他们分别用team1和team2账号进行管理,要求仅允许team1和team2账号登录ftp服务器,单不能登录本地系统,并将这两个账号的根目录限制为/var/www/html,不能进去该目录以外的任何目录。

    前期准备首先安装apachevsftpd,具体安装方法这里就不介绍了。

    创建测试账号team1和team2

    [root@qdlinux ~]# useradd -s /sbin/nologin team1 
    [root@qdlinux ~]# useradd -s /sbin/nologin team2
    [root@qdlinux ~]# echo "123456" | passwd --stdin team1
    Changing password for user team1.
    passwd: all authentication tokens updated successfully.
    [root@qdlinux ~]# echo "123456" | passwd --stdin team2
    Changing password for user team2.
    passwd: all authentication tokens updated successfully.
    
    

    接下来修改配置文件,这里在RHEL6中和RHEL7中配置有些不同

    anonymous_enable=NO
    local_enable=YES
    chroot_local_user=YES
    local_root=/var/www/html
    allow_writeable_chroot=YES
    chroot_list_enable=YES
    chroot_list_file=/etc/vsftpd/chroot_list
    
    

    可以把不限制的账户放到这里面,创建用户列表文件,因为此账户会访问到其他目录.但这样做事很危险的.

    [root@qdlinux ~]# touch /etc/vsftpd/chroot_list
    [root@qdlinux ~]# vim /etc/vsftpd/chroot_list 
    teamleader
    

    修改apache的根目录权限/var/www/html

    [root@qdlinux ~]# ll -d /var/www/html/
    drwxr-xr-x 2 root root 6 May  9  2017 /var/www/html/
    [root@qdlinux ~]# chmod -R o+w /var/www/html/
    
    

    访问测试

    image

    不限制用户访问

    image

    不限制用户访问,用户可以随意到任何目录

    image

    限制用户访问,用户只能在此目录中,在安全方面相对要好很多.

    image

    文件上传后文件拥有者权限

    image


    虚拟用户模式

    示例演示为了宣传公司的产品信息,计划搭建FTP服务器,为客户提供相关资料的下载,仅允许下载产品信息,禁止上传。公司的合作单位和可以在FTP服务器上上传和下载,但是不能删除数据。

    创建用户进行FTP认证的用户数据库文件,其中奇数行为账户名,偶数行为密码。编辑vim /etc/vsftpd/vuser.list

    ftp
    123456
    vip
    123456
    
    

    由于明文的信息既不安全,也不符合让vsftpd服务程序直接加在的格式,因此需要使用db_load命令用哈希算法将明文信息文件转成数据库文件。在RHEL6中需要安装db4-utils,在RHEL7中libdb-utils

    [root@qdlinux ~]# rpm -qf `which db_load`
    libdb-utils-5.3.21-20.el7.x86_64
    [root@qdlinux ~]# db_load -T -t hash -f /etc/vsftpd/vuser.list /etc/vsftpd/vuser.db
    [root@qdlinux ~]# file /etc/vsftpd/vuser.db 
    /etc/vsftpd/vuser.db: Berkeley DB (Hash, version 9, native byte-order)
    
    

    降低数据库文件的权限避免其他人看到数据库文件的内容,然后把明文信息文件删除。

    [root@qdlinux ~]# chmod 600 /etc/vsftpd/vuser.db
    [root@qdlinux ~]# ll /etc/vsftpd/vuser.db
    -rw------- 1 root root 12288 Aug 14 01:19 /etc/vsftpd/vuser.db
    
    

    建立用于支持虚拟用户的PAM文件

    vim /etc/pam.d/vsftpd.vu

    auth    required        pam_userdb.so   db=/etc/vsftpd/vuser
    account required        pam_userdb.so   db=/etc/vsftpd/vuser
    

    建立用户存储文件的根目录以及虚拟用户映射的系统本地用户。

    [root@qdlinux ~]# useradd -d /var/ftp/share -s /sbin/nologin ftpuser
    [root@qdlinux ~]# useradd -d /var/ftp/vip -s /sbin/nologin ftpvip
    [root@qdlinux ~]# chmod -R 500 /var/ftp/share/
    [root@qdlinux ~]# chmod -R 700 /var/ftp/vip/
    
    

    修改配置文件

    anonymous_enable=NO
    local_enable=YES
    allow_writeable_chroot=YES
    pam_service_name=vsftpd.vu
    user_config_dir=/etc/vsftpd/vuserconfig
    max_clients=300
    max_per_ip=10
    
    
    

    建立虚拟账号配置文件

    [root@qdlinux ~]# mkdir /etc/vsftpd/vuserconfig
    [root@qdlinux ~]# touch /etc/vsftpd/vuserconfig/ftp
    [root@qdlinux ~]# touch /etc/vsftpd/vuserconfig/vip
    

    vim编辑/etc/vsftpd/vuserconfig/ftp中的内容

    guest_enable=YES
    guest_username=ftpuser
    anon_world_readable_only=NO
    anon_max_rate=50000
    
    

    vim编辑/etc/vsftpd/vuserconfig/vip中的内容

    guest_enable=YES
    guest_username=ftpvip
    anon_world_readable_only=NO
    write_enable=YES
    anon_mkdir_write_enable=YES
    anon_upload_enable=YES
    anon_max_rate=2000000
    

    测试ftp账户

    [root@qdlinux ~]# lftp 192.168.56.15 -u ftp,123456
    lftp ftp@192.168.56.15:~> ls          
    drwx------    5 0        0              97 Aug 13 18:14 grub2
    -r--r--r--    1 0        0        11607904 Aug 13 18:13 mariadb-server.rpm
    
    

    访问限速测试

    image

    image

    测试vip账户

    [root@qdlinux ~]# lftp 192.168.56.15 -u vip
    Password: 
    lftp vip@192.168.56.15:~> ls          
    -rw-------    1 1004     1004      5505024 Aug 13 18:33 kali-linux-2018.2-amd64.iso
    -rw-------    1 1004     1004     111017984 Aug 13 18:36 kali-linux-2018.2-amd641.iso
    -r--r--r--    1 0        0        11607904 Aug 13 18:30 mariadb-server.rpm
    
    

    上传和下载访问速度测试

    image

    image

  • 相关阅读:
    笔记手动排序
    笔记手动分页
    Spring定时任务Quartz配置之手动设置
    java 日期处理
    SQL Case when 的使用方法
    Hibernate八大类HQL查询集合
    Spring定时任务Quartz配置
    各个浏览器显示版本(IE,火狐)
    js转译html标签
    定时备份SQL SERVER的数据库并且把备份文件复制到另外一台服务器
  • 原文地址:https://www.cnblogs.com/qdlinux/p/9475425.html
Copyright © 2011-2022 走看看