• CentOS平台部署vsftp(基于虚拟用户)


    1. 安装FTP

    1
    2
    [root@task ~]# yum install vsftpd –y
    [root@task ~]# chkconfig vsftpd on          # 配置开机启动

    2. 配置ftp(修改主配置文件)

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    [root@task ~]# cd /etc/vsftpd/
    [root@task vsftpd]# vim vsftpd.conf
    anonymous_enable=NO         # 拒绝匿名用户
    chroot_list_enable=YES      # 使用户不能离开主目录
    ascii_upload_enable=YES     # 设定支持ASCII模式的上传和下载功能
    ascii_download_enable=YES
    pam_service_name=vsftpd     # PAM认证文件名。PAM将根据/etc/pam.d/vsftpd进行认证
    guest_enable=YES        # 设定启用虚拟用户功能
    guest_username=ftp      # 指定虚拟用户的宿主用户,CentOS中已经有内置的ftp用户了
     
    user_config_dir=/etc/vsftpd/vuser_conf      #设定虚拟用户个人vsftp的CentOS FTP服务文件存放路径。存放虚拟用户个性的CentOS FTP服务文件(配置文件名=虚拟用户名)
    xferlog_file=/var/log/xferlog  # 配置vsftpd日志

    3. 配置pam认证

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    [root@task vsftpd]# yum install db4 db4-utils –y  # 安装Berkeley DB工具
    [root@task vsftpd]# vim /etc/vsftpd/vuser_passwd  # 创建用户密码文本,注意奇行是用户名,偶行是密码
    shaw
    xxxxxxxx
    task
    xxxxxxxxx
      
    [root@task vsftpd]# db_load -T -t hash -f /etc/vsftpd/vuser_passwd /etc/vsftpd/vuser_passwd.db      # 生成虚拟用户认证的db文件
      
    [root@task vsftpd]# vim /etc/pam.d/vsftpd  #编辑认证文件,全部注释掉原来语句,再增加以下两句
    #%PAM-1.0
    #session    optional     pam_keyinit.so    force revoke
    #auth       required    pam_listfile.so item=user sense=deny file=/etc/vsftpd/ftpusers onerr=succeed
    #auth       required    pam_shells.so
    #auth       include password-auth
    #account    include password-auth
    #session    required     pam_loginuid.so
    #session    include password-auth
    auth required pam_userdb.so db=/etc/vsftpd/vuser_passwd
    account required pam_userdb.so db=/etc/vsftpd/vuser_passwd

    4. 创建用户

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    # 创建虚拟用户配置文件
    [root@task vsftpd]# mkdir /etc/vsftpd/vuser_conf
     
    # 注意:文件名定义为vuser_passwd.txt里面的账户名,否则下面设置无效
    [root@task vsftpd]# vim  /etc/vsftpd/vuser_conf/shaw
    local_root=/data/ftp/shaw       # 指定用户家目录
    write_enable=YES
    anon_umask=022
    anon_max_rate=5120000               # 限速
    anon_mkdir_write_enable=YES
    anon_other_write_enable=YES
    anon_upload_enable=YES
    anon_world_readable_only=NO

    5. 设置FTP目录权限

    1
    2
    3
    4
    5
    6
    7
    [root@task vsftpd]# mkdir /data/ftp –p      # 创建虚拟用户数据存放目录
    [root@task vsftpd]# mkdir /data/ftp/shaw    # 创建用户数据目录
    [root@task vsftpd]# chmod -R 755 /data/
    # 建立限制用户访问目录的空文件
    [root@task ~]# touch /etc/vsftpd/chroot_list
    # 如果启用vsftpd日志需手动建立日志文件
    [root@task ~]# touch /var/log/xferlog

    6. 配置被动模式

    1
    2
    3
    4
    5
    [root@task ~]# vim /etc/vsftpd/vsftpd.conf
    pasv_enable=YES             # 开启被动
    pasv_min_port=40000
    pasv_max_port=40080
    [root@task ~]# /etc/init.d/vsftpd restart

    7. 配置ssl

    1
    2
    3
    4
    5
    6
    7
    8
    9
    ssl_enable=YES            # 启用ssl
    allow_anon_ssl=NO         # 不允许匿名用户使用ssl
    force_local_data_ssl=YES  # 强制数据传输使用ssl
    force_local_logins_ssl=YES # 强制登录认证使用ssl
    ssl_tlsv1=YES
    ssl_sslv2=NO
    ssl_sslv3=NO
    rsa_cert_file=/etc/vsftpd/server.crt       # 证书信息
    rsa_private_key_file=/etc/vsftpd/server.key # 证书信息

    8. FTP自动安装py

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    #!/bin/env python
    # -*- coding:utf-8 -*-
    '''
    基于CentOS 6 平台部署vsftpd(粗糙版,待完善。。。)
    '''
    import subprocess
    import os, sys, time,re
    '''
    FTP主配置文件
    anonymous_enable=NO                 # 禁止匿名用户登录
    local_enable=YES
    write_enable=YES
    local_umask=022
    dirmessage_enable=YES
    xferlog_enable=YES                  # 开启日志
    connect_from_port_20=YES
    xferlog_std_format=YES
    ascii_upload_enable=YES             # 设定支持ASCII模式的上传

    ascii_download_enable=YES           # 设定支持ASCII模式的下载

    allow_writeable_chroot=YES          # 新版vsftpd要加入此项

    chroot_list_enable=YES              # 使用户不能离开主目录
    listen=YES
    listen_port=43121                   # 监听端口
    pam_service_name=vsftpd             # PAM认证文件名。PAM将根据/etc/pam.d/vsftpd进行认证
    guest_enable=YES                    # 设定启用虚拟用户功能
    guest_username=ftp                  # 指定虚拟用户的宿主用户,CentOS中已经有内置的ftp用户了
    user_config_dir=/etc/vsftpd/vuser_conf      # 设定虚拟用户个人vsftp的CentOS FTP服务文件存放路径。存放虚拟用户个性的CentOS FTP服务文件(配置文件名=虚拟用户名)
    xferlog_file=/var/log/xferlog
    userlist_enable=YES
    tcp_wrappers=YES
    pasv_enable=YES                     # 开启被动
    pasv_min_port=40000                 # 被动模式端口
    pasv_max_port=40080                 # 被动模式端口
     
    '''
     
    '''
    虚拟用户配置文件
    local_root=/data/ftp/task       # 虚拟用户数据文件目录
    write_enable=YES
    anon_umask=022
    anon_max_rate=5120000           # 限速5M
    anon_mkdir_write_enable=YES     # 允许创建文件
    anon_other_write_enable=YES
    anon_upload_enable=YES          # 允许上传
    anon_world_readable_only=NO

    '''

    '''

    ssl_enable=YES            # 启用ssl
    allow_anon_ssl=NO         # 不允许匿名用户使用ssl
    force_local_data_ssl=YES  # 强制数据传输使用ssl
    force_local_logins_ssl=YES # 强制登录认证使用ssl
    ssl_tlsv1=YES
    ssl_sslv2=NO
    ssl_sslv3=NO
    rsa_cert_file=/etc/vsftpd/server.crt       # 证书信息
    rsa_private_key_file=/etc/vsftpd/server.key # 证书信息

    '''

    class Ftp_init:

        def __init__(self):
            self.user = ''
            self.passwd = ''
            self.ftpuser= 'task'
            self.ftpsswd = 'q.123456'
            self.ftpath = ''
            self.file = '/etc/vsftpd/vsftpd.conf'
            self.path = '/etc/vsftpd/'
            self.userconf_list = []
     
     
        def ftp_install(self):
            '''
            安装vsftpd
            :return:
            '''
            ret = subprocess.call('rpm -qa | grep vsftpd', shell=True)
            if ret == 0:
                print '#Info vsftpd is already installed.'
                return True
            else:
                subprocess.call('yum install vsftpd -y', shell=True)
                subprocess.call('chkconfig vsftpd on', shell=True)
                print '#Info vsftpd successful installation.'
     
     
        def modify_conf(self):
            '''
            修改vsfptd主配置文件
            :return:
            '''
            ret = subprocess.call('wget -c ftp://%s:%s@x.x.x.x/ftp_install/ftp.conf' % (self.ftpuser, self.ftpsswd),shell=True)     # 直接下载编辑好的FTP主配置文件
            if ret == 0:
                print '#Info ftp.conf download success.'
            else:
                print '#Info ftp.conf download failed.'
                return False
            with open('ftp.conf', 'r+') as h:
                with open(self.file, 'w+') as f:
                    f.write('# Example config file /etc/vsftpd/vsftpd.conf created by shaw. ')
                    for lines in h:
                        if 'xferlog_file' in lines:
                            subprocess.call('touch %s'%lines.split('=')[1],shell=True)
                        f.write(lines)
            os.remove('ftp.conf')
     
     
        def pam_auth(self):
            '''
            生产虚拟用户认证文件
            :return:
            '''
            rets = subprocess.call('rpm -qa | grep db4',shell=True)
            if rets == 0:
                print '#INFO db4 install success.'
            else:
                ret = subprocess.call('yum –y install db4 db4-utils',shell=True)
                if ret != 0:
                    print '#INFO db4 install failed.'
                    return False
            with open('%s/vuser_passwd'%self.path,'w+') as f:
                self.user = raw_input('请输入要创建的FTP用户名:').strip()
                self.passwd = raw_input('请输入FTP密码:').strip()
                f.write('%s '%self.user)                               # 这里有BUG:因为定义FTP虚拟用户时,用户名一行(奇数行),密码一行(偶数行),为了换行,python在写入时,添加了换行符' ',导致实际生成的文件,密码在第三行,第二行为空行
                f.write(self.passwd)
                subprocess.call('db_load -T -t hash -f %s/vuser_passwd %s/vuser_passwd.db'%(self.path,self.path),shell=True)
                f = open('/etc/pam.d/vsftpd','w+')
                f.write('auth required pam_userdb.so db=/etc/vsftpd/vuser_passwd ')
                f.write('account required pam_userdb.so db=/etc/vsftpd/vuser_passwd')
                f.close()
     
     
     
        def create_ftpath(self):
            '''
            创建虚拟用户目录,授权
            :return:
            '''
            self.ftpath = raw_input('请输入要创建的FTP根目录:').strip()
            subprocess.call('mkdir -p %s/%s'%(self.ftpath,self.user),shell=True)
            subprocess.call('touch %s/chroot_list'%self.path, shell=True)
            subprocess.call('chown -R ftp.ftp %s'%self.ftpath,shell=True)
     
     
     
        def create_user(self):
            '''
            编辑虚拟用户配置文件
            :return:
            '''
            os.system('mkdir %s/vuser_conf'%self.path)
            subprocess.call('wget -c ftp://%s:%s@x.x.x.x/ftp_install/vuser.conf'%(self.ftpuser, self.ftpsswd),shell=True)       # 直接下载编辑好的虚拟用户配置文件
            os.rename('vuser.conf','%s/vuser_conf/%s'%(self.path,self.user))
            with open('%s/vuser_conf/%s' % (self.path, self.user), 'r+') as h:
                for line in h:
                    self.userconf_list.append(line)
                self.userconf_list[0] = 'local_root=%s/%s '%(self.ftpath,self.user)
            with open('%s/vuser_conf/%s' % (self.path, self.user), 'w+') as f:
                for i in self.userconf_list:
                    f.write(i)
     
     
    if __name__ == '__main__':
        val = Ftp_init()
        val.ftp_install()
        val.modify_conf()
        val.pam_auth()
        val.create_ftpath()
        val.create_user()
        try:
            subprocess.check_call('/etc/init.d/vsftpd restart',shell=True)
        except Exception as e:
            print 'VSFTP installed filed'







  • 相关阅读:
    测试开发进阶——Spring cloud——理解——微服务中微服务网关理解(转载)
    测试开发进阶——Spring cloud——理解——微服务中断路器模式理解(转载)
    测试开发进阶——Spring cloud——理解——微服务中负载均衡理解(转载)
    ORM 多表作业
    python-Matplotlib库
    beego Dockerfile
    java单例模式-饿汉式
    Java字符串连接:Java8字符串连接收集器Collectors.joining
    Odoo14学习笔记(11) 实例-简单的按年份月份查询报表
    002. git 分支管理
  • 原文地址:https://www.cnblogs.com/opsedu/p/5809712.html
Copyright © 2020-2023  润新知