vsftpd虚拟目录一键部署

1 vsftpd一键部署脚本

#!/bin/bash
# This script is auto install vsftpd for CentOS6  #
# Enable pasv modue, port range: 40000-40080      #

t=`date "+%F %T"`
f1="vsftpd"
f2=`rpm -qa | grep -i $f1`
log="/var/log/vsftp.log"
ftp_port="21"

n1=`rpm -qa | grep -i $f1 | wc -l`
if [ $n1 -ne 0 ];then
  echo "$f2 has already installed."
  exit
fi

echo -e "============Starting install $f1...============="
yum -y install $f1 2>&1 >> /dev/null
if [ $? -eq 0 ];then
  n2=`rpm -qa | grep -i $f1 | wc -l`
  if [ $n2 -ne 0 ];then
    echo $t >> $log
    echo -e "\033[40;32m$f1 install OK.\n\033[40;37m"
    echo "$f1 install successful" >> $log
    echo "===============================" >> $log
  fi
else
  echo -e "\033[40;32m$f1 install failed! Please check.\n\033[40;37m"
  exit
fi

#set vsftpd.conf
echo ""
echo "==============Setting vsftpd.conf...==============="
cd /etc/vsftpd
mv vsftpd.conf vsftpd.conf.bak
touch vsftpd.conf
cat > vsftpd.conf << EOF
anonymous_enable=no
local_enable=YES
chroot_list_enable=yes
chroot_local_user=yes
chroot_list_file=/etc/vsftpd/chroot_list
ascii_upload_enable=YES
ascii_download_enable=YES
write_enable=YES

local_umask=022
dirmessage_enable=YES
xferlog_enable=YES
connect_from_port_20=YES
xferlog_std_format=YES

listen=YES
listen_port=

pam_service_name=vsftpd
userlist_enable=YES
tcp_wrappers=YES

guest_enable=YES
guest_username=ftp
user_config_dir=/etc/vsftpd/vuser_conf

pasv_enable=YES
pasv_min_port=40000
pasv_max_port=40080
pasv_promiscuous=YES
EOF

sed -i "s/listen_port=/listen_port=$ftp_port/" vsftpd.conf
if [ $? -eq 0 ];then
  echo ""
  echo -e "\033[40;32mSet vsftpd.conf OK.\n\033[40;37m"
  echo ""
else
  echo -e "\033[40;32mSet vsftpd.conf falied! Please check.\n\033[40;37m"
  exit
fi

service vsftpd start

new_port=`netstat -natlp | grep -i vsftpd | awk '{print $4}' | awk -F":" '{print $2}'`
if [ $new_port -eq $ftp_port ];then
  echo ""
  echo -e "FTP running success! Current port is:\033[40;32m [$new_port] \n\033[40;37m"
else
  echo ""
  echo "Start FTP failed! Please check."
fi
chkconfig $f1 on

2 虚拟目录创建脚本

#!/bin/bash
# Auto create vsftpd virtual user                #

t=`date "+%F %T"`
f1="vsftpd"
f2=`rpm -qa | grep -i $f1`
log="/var/log/vsftp.log"
ftp_port="21"
passwd_file="/etc/vsftpd/vuser_passwd.txt"

n1=`rpm -qa | grep -i $f1 | wc -l`
if [ $n1 -eq 0 ];then
  echo "$f2 not installed! Please check."
  exit
fi

add_user() {
echo -e -n "Please input ftp username: "
read user
echo -e -n "Please input ftp password: "
read passwd
echo "Default directory is: /data/ftp_data/$user"
echo -e -n "Please reconfirm [Y/y]: "
read var
if [ "$var"x = "y"x -o "$var"x = "Y"x ];then
  dir="/data/ftp_data/$user"
else
  echo "Input Error!"
  exit
fi
}

echo "====================Starting configure virtual user=================="
add_user
if [ -z "$user" ] || [ -z "$passwd" ] || [ -z "$dir" ];then
  echo "Error: input not null! Please run script and enter again."
  exit
fi

if [ -z $passwd_file ];then
  touch $passwd_file
fi

num=`cat $passwd_file | grep -i $user | wc -l`
if [ $num -gt 0 ];then
  echo "Error, $user exist!"
  exit
fi

echo $user >> $passwd_file
echo $passwd >> $passwd_file

n2=`rpm -qa | grep -i db4 | wc -l`
if [ $n2 -lt "2" ];then
  yum -y install db4 db4-utils
fi

db_load -T -t hash -f $passwd_file /etc/vsftpd/vuser_passwd.db
if [ $? -eq 0 ];then
  echo ""
  echo "db_load execute success."
  echo ""
else
  echo ""
  echo "db_load execute failed! Please check."
  exit
fi

cd /etc/pam.d
cp vsftpd vsftpd.bak
echo "" > vsftpd
cat > vsftpd << EOF
auth required pam_userdb.so db=/etc/vsftpd/vuser_passwd
account required pam_userdb.so db=/etc/vsftpd/vuser_passwd
EOF

#set vuser config
f4="chroot_list"
if [ -z $f4 ];then
  touch -p /etc/vsftpd/$f4
fi

n3=`cat /etc/vsftpd/$f4 | grep -i $user | wc -l`
if [ $n3 -eq 0 ];then
  echo "$user" >> /etc/vsftpd/$f4
fi

mkdir -p /etc/vsftpd/vuser_conf
cd /etc/vsftpd/vuser_conf
touch $user
cat > $user << EOF
local_root=
write_enable=YES
anon_umask=022
anon_world_readable_only=NO
anon_upload_enable=YES
anon_mkdir_write_enable=YES
anon_other_write_enable=YES
EOF

sed -i "s#local_root=#local_root=$dir#" $user
if [ $? -eq 0 ];then
  echo $t >> $log
  echo "Virtual user create success." >> $log
  echo "=============================" >> $log
  echo "Virtual user create success."
  echo ""
  echo "========Vuser detail========="
  echo -e "Username: \033[40;32m $user \033[40;37m"
  echo -e "Password: \033[40;32m $passwd \033[40;37m"
  echo -e "FTP port: \033[40;32m $ftp_port \033[40;37m"
  echo -e "Root directory: \033[40;32m $dir \033[40;37m"
  echo "============================="
  echo ""
else
  echo "Add virtual user falied! Please check."
  exit
fi

mkdir -p $dir
chmod -R 755 $dir
chown -R ftp.ftp $dir

service vsftpd restart

new_port=`netstat -natlp | grep -i vsftpd | awk '{print $4}' | awk -F":" '{print $2}'`
if [ $new_port -eq $ftp_port ];then
  echo ""
  echo "FTP running success! Current port is: $new_port"
else
  echo ""
  echo "Start FTP failed! Please check."
fi

你可能感兴趣的:(vsftpd虚拟目录一键部署)