linux安装FTP服务虚拟用户配置脚本分享(终极脚本)

 #!/bin/bash
 #2019年10月24日 10:57:12
 #by vftpd server
 #wangxuejin www.cnbugs.com
 ##############################
 FTP_YUM="yum install -y"
 FTP_DIR="/etc/vsftpd"
 FTP_DB="vsftpd_login"
 FTP_USR="ftpuser"
 USR_CONF="vsftpd_user_conf"
 FTP_VIR="$*"
 FTP_PAS="Aa123456"
 if [ $# -eq 0 ];then
     echo -e "\033[32m-----------------------------\33[0m"
     echo -e "\033[32musage:{/bin/bash $0 test1|test2 test3|list.txt|help}"
     exit 0
 fi
 if [ $1 == "help"  ];then
     echo -e "\033[32m-----------------------------\33[0m"
     echo -e "\033[32musage:{/bin/bash $0 test1|test2 test3|list.txt|help}"
     exit 0
 fi
 $FTP_YUM vsftpd >>/dev/null
 rpm -qa|grep vsftpd
 systemctl restart vsftpd.service
 $FTP_YUM  pam libdb-utils libdb --skip-broken 
 useradd -s /sbin/nologin ${FTP_USR} 
 if [ $1 != "list.txt" ];then
 touch $FTP_DIR/${FTP_USR}s.txt
 for i in $(echo $FTP_VIR)
 do
 cat $FTP_DIR/${FTP_USR}s.txt|grep $i
 if [ $? -ne 0 ];then
 cat>>$FTP_DIR/${FTP_USR}s.txt<<EOF
 $i
 $FTP_PAS
 EOF
 if [ ! -d $FTP_DIR/$USR_CONF ];then
     mkdir -p $FTP_DIR/${USR_CONF}/
 fi
 mkdir -p /home/${FTP_USR}/$i
 touch $FTP_DIR/${USR_CONF}/$i
 cat>$FTP_DIR/${USR_CONF}/$i<<EOF
 local_root=/home/${FTP_USR}/$i
 write_enable=YES
 anon_world_readable_only=YES
 anon_upload_enable=YES
 anon_mkdir_write_enable=YES
 anon_other_write_enable=YES
 EOF
 fi
 done
 db_load -T -t  hash -f $FTP_DIR/${FTP_USR}s.txt $FTP_DIR/${FTP_DB}.db
 chmod 700 $FTP_DIR/${FTP_DB}.db
 cat>/etc/pam.d/vsftpd<<EOF
 auth     required    pam_userdb.so   db=$FTP_DIR/${FTP_DB}
 account    required    pam_userdb.so   db=$FTP_DIR/${FTP_DB}
 EOF
 cat $FTP_DIR/vsftpd.conf|grep guest_
 if [ $? -ne 0 ];then
 cat>>$FTP_DIR/vsftpd.conf<<EOF
 #config virtual user FTP
 pam_service_name=vsftpd
 guest_enable=YES
 guest_username=${FTP_USR}
 user_config_dir=$FTP_DIR/${USR_CONF}
 virtual_use_local_privs=YES
 EOF
 fi
 chown -R ${FTP_USR}:${FTP_USR} /home/${FTP_USR}
 systemctl restart vsftpd.service
 systemctl stop firewalld
 systemctl disable firewalld
 setenforce 0
 sed -i 's/enforcing/disable/g' /etc/selinux/config
 else
 touch $FTP_DIR/${FTP_USR}s.txt
 for i in $(cat list.txt|grep -v pwd)
 do
 cat $FTP_DIR/${FTP_USR}s.txt|grep $i
 cat list.txt >>$FTP_DIR/${FTP_USR}s.txt
 if [ ! -d $FTP_DIR/$USR_CONF ];then
     mkdir -p $FTP_DIR/${USR_CONF}/
 fi
 mkdir -p /home/${FTP_USR}/$i
 touch $FTP_DIR/${USR_CONF}/$i
 cat>$FTP_DIR/${USR_CONF}/$i<<EOF
 local_root=/home/${FTP_USR}/$i
 write_enable=YES
 anon_world_readable_only=YES
 anon_upload_enable=YES
 anon_mkdir_write_enable=YES
 anon_other_write_enable=YES
 EOF
 done
 db_load -T -t  hash -f $FTP_DIR/${FTP_USR}s.txt $FTP_DIR/${FTP_DB}.db
 chmod 700 $FTP_DIR/${FTP_DB}.db
 cat>/etc/pam.d/vsftpd<<EOF
 auth     required    pam_userdb.so   db=$FTP_DIR/${FTP_DB}
 account    required    pam_userdb.so   db=$FTP_DIR/${FTP_DB}
 EOF
 cat $FTP_DIR/vsftpd.conf|grep guest_
 if [ $? -ne 0 ];then
 cat>>$FTP_DIR/vsftpd.conf<<EOF
 #config virtual user FTP
 pam_service_name=vsftpd
 guest_enable=YES
 guest_username=${FTP_USR}
 user_config_dir=$FTP_DIR/${USR_CONF}
 virtual_use_local_privs=YES
 EOF
 fi
 chown -R ${FTP_USR}:${FTP_USR} /home/${FTP_USR}
 systemctl restart vsftpd.service
 systemctl stop firewalld
 systemctl disable firewalld
 setenforce 0
 fi

脚本运行方法如下:

如需创建test1和test2两个FTP用户,运行方法如下:

[root@wxj 20191027]# ./vsftpd.sh_v6 test3 test4

如果定义文件list.txt参数,运行方法如下

vim list.txt
test11
pwd1234
test22
pwd2234

运行方法如下:

[root@wxj 20191027]# ./vsftpd.sh_v6 list.txt

备注:密码必须以pwd开头,否则密码会当用户创建

发表评论

登录... 后才能评论