标签:
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.confanonymous_enable=NO # 拒绝匿名用户chroot_list_enable=YES # 使用户不能离开主目录ascii_upload_enable=YES # 设定支持ASCII模式的上传和下载功能ascii_download_enable=YESpam_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 # 创建用户密码文本,注意奇行是用户名,偶行是密码shawxxxxxxxxtaskxxxxxxxxx [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-authauth required pam_userdb.so db=/etc/vsftpd/vuser_passwdaccount 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=YESanon_umask=022anon_max_rate=5120000 # 限速anon_mkdir_write_enable=YESanon_other_write_enable=YESanon_upload_enable=YESanon_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.confpasv_enable=YES # 开启被动pasv_min_port=40000pasv_max_port=40080[root@task ~]# /etc/init.d/vsftpd restart |
7. 配置ssl
1 2 3 4 5 6 7 8 9 | ssl_enable=YES # 启用sslallow_anon_ssl=NO # 不允许匿名用户使用sslforce_local_data_ssl=YES # 强制数据传输使用sslforce_local_logins_ssl=YES # 强制登录认证使用sslssl_tlsv1=YESssl_sslv2=NOssl_sslv3=NOrsa_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 subprocessimport os, sys, time,re‘‘‘FTP主配置文件anonymous_enable=NO # 禁止匿名用户登录local_enable=YESwrite_enable=YESlocal_umask=022dirmessage_enable=YESxferlog_enable=YES # 开启日志connect_from_port_20=YESxferlog_std_format=YESascii_upload_enable=YES # 设定支持ASCII模式的上传
chroot_list_enable=YES # 使用户不能离开主目录listen=YESlisten_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/xferloguserlist_enable=YEStcp_wrappers=YESpasv_enable=YES # 开启被动pasv_min_port=40000 # 被动模式端口pasv_max_port=40080 # 被动模式端口‘‘‘‘‘‘虚拟用户配置文件local_root=/data/ftp/task # 虚拟用户数据文件目录write_enable=YESanon_umask=022anon_max_rate=5120000 # 限速5Manon_mkdir_write_enable=YES # 允许创建文件anon_other_write_enable=YESanon_upload_enable=YES # 允许上传anon_world_readable_only=NO
‘‘‘ ssl_enable=YES # 启用sslallow_anon_ssl=NO # 不允许匿名用户使用sslforce_local_data_ssl=YES # 强制数据传输使用sslforce_local_logins_ssl=YES # 强制登录认证使用sslssl_tlsv1=YESssl_sslv2=NOssl_sslv3=NOrsa_cert_file=/etc/vsftpd/server.crt # 证书信息rsa_private_key_file=/etc/vsftpd/server.key # 证书信息
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.\n‘) 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\n‘%self.user) # 这里有BUG:因为定义FTP虚拟用户时,用户名一行(奇数行),密码一行(偶数行),为了换行,python在写入时,添加了换行符‘\n‘,导致实际生成的文件,密码在第三行,第二行为空行 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\n‘) 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\n‘%(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‘ |
标签:
原文地址:http://www.cnblogs.com/opsedu/p/5809712.html