管理vsftpd虚拟用户脚本
当公司服务器上使用ftp用户越来越多的时候,为了偷懒来管理ftp,就使用shell脚本来解决这个问题.当然这是基于虚拟用户的,关于虚拟用户的安装配置,大家可以去参考我的这篇文章vsftpd配置虚拟用户.
1.定制虚拟用户模版配置文件
touch /etc/vsftpd/user_conf/vconf.tmp
vi /etc/vsftpd/user_conf/vconf.tmp
2.在以虚拟用户方式配置好Vsftp以后,就可以通过以下方式使用脚本了:
创建用户:sh uservsftpd.sh create tt或者./uservsftpd.sh create tt
删除用户:sh uservsftpd.sh delete tt或者./uservsftpd.sh delete tt
禁用用户:sh uservsftpd.sh disable tt或者./uservsftpd.sh disable tt
激活用户:sh uservsftpd.sh enable tt或者./uservsftpd.sh enable tt
修改密码:sh uservsftpd.sh passwd tt或者./uservsftpd.sh passwd tt
3.创建脚本赋权限
vi uservsftpd.sh
chmod +x uservsftpd.sh
1.定制虚拟用户模版配置文件
touch /etc/vsftpd/user_conf/vconf.tmp
vi /etc/vsftpd/user_conf/vconf.tmp
local_root=/ftphome/virtuser //指定虚拟用户的具体主路径。 anonymous_enable=NO //设定不允许匿名用户访问。 write_enable=YES //设定允许写操作。 anon_umask=022 //设定上传文件权限掩码。 anon_upload_enable=NO //设定不允许匿名用户上传。 anon_mkdir_write_enable=NO //设定不允许匿名用户建立目录。 idle_session_timeout=600 //设定空闲连接超时时间。 data_connection_timeout=120 //设定单次连续传输最大时间。 max_clients=2 //设定并发客户端访问个数。 max_per_ip=5 //设定单个客户端的最大线程数,这个配置主要来照顾Flashget、迅雷等多线程下载软件。 local_max_rate=50000 //设定该用户的最大传输速率,单位b/s.
2.在以虚拟用户方式配置好Vsftp以后,就可以通过以下方式使用脚本了:
创建用户:sh uservsftpd.sh create tt或者./uservsftpd.sh create tt
删除用户:sh uservsftpd.sh delete tt或者./uservsftpd.sh delete tt
禁用用户:sh uservsftpd.sh disable tt或者./uservsftpd.sh disable tt
激活用户:sh uservsftpd.sh enable tt或者./uservsftpd.sh enable tt
修改密码:sh uservsftpd.sh passwd tt或者./uservsftpd.sh passwd tt
3.创建脚本赋权限
vi uservsftpd.sh
chmod +x uservsftpd.sh
#!/bin/bash
#The script can create,deactivate,activate and delete virtual users of vsftpd.
#Author: rocdk890
USERFILE=/etc/vsftpd/vuser
USERDB=/etc/vsftpd/vuser.db
CONFBASE=/etc/vsftpd/user_conf
TMPCONF=/etc/vsftpd/user_conf/vconf.tmp
FTPBASE=/var/www/vhosts/wwwroot
FTPHOST=vuser
USERNAME=$2
if [ $# != 2 ];then
echo "Usage: $0 {create|disable|enable|passwd|delete} {username}" >&2
exit 1
fi
function check_username_exist() {
#Check if virtual user already exist
USERCOUNT=$(sed -n 'p;n' $USERFILE | grep -w $USERNAME | wc -l)
if [ $USERCOUNT -ne 0 ];then
echo "User $USERNAME ALREADY exist!" && exit
fi
}
check_username_notexist() {
#Check if virtual user not exist
USERCOUNT=$(sed -n 'p;n' $USERFILE | grep -w $USERNAME | wc -l)
if [ $USERCOUNT -eq 0 ];then
echo "User $USERNAME NOT exist!" && exit
fi
}
get_password() {
#Get the password
echo -n "Input password: "
read password
#Check if password is empty
if [ -z "$password" ];then
echo "Empty password!!" && exit
fi
}
update_userdb() {
#Delete the virtual user db
rm -f $USERDB
#Generate the virtual user db
db_load -T -t hash -f $USERFILE $USERDB
}
case "$1" in
'create' )
check_username_exist
get_password
#Write the username and password to $USERFILE
echo $USERNAME >> $USERFILE
echo $password >> $USERFILE
update_userdb
#Create the configure file of virtual user
cp $TMPCONF $CONFBASE/$USERNAME
#Replace the home directory name of virtual user
sed -i "s/virtuser/$USERNAME/g" $CONFBASE/$USERNAME
#Create the home directory of virtual user
mkdir $FTPBASE/$USERNAME
#Change the owner of home directory to OS user $FTPHOST
chown -R $FTPHOST:$FTPHOST $FTPBASE/$USERNAME
;;
'disable' )
check_username_exist
#Change the owner of home directory from $FTPHOST to root
chown root:root $FTPBASE/$USERNAME
#Change the permissions of home directory to read-only for root
chmod 700 $FTPBASE/$USERNAME
;;
'enable' )
check_username_exist
#Change the owner of home directory from root to $FTPHOST to root
chown $FTPHOST:$FTPHOST $FTPBASE/$USERNAME
#Change the permissions of home directory to 775 for $FTPHOST
chmod 775 $FTPBASE/$USERNAME
;;
'delete' )
check_username_notexist
#Get the row numbers of username and password of virtual user
ROWNUMBER=$(cat -n $USERFILE | sed -n 'p;n' | grep -w $USERNAME | awk '{print $1}' | head -n 1)
#Delete the username and password of virtual user from $USERFILE
sed -i "${ROWNUMBER}d" $USERFILE
sed -i "${ROWNUMBER}d" $USERFILE
update_userdb
#Delete the configure file of virtual user
rm -f $CONFBASE/$USERNAME
#Rename the home directory name of virtual user
mv $FTPBASE/$USERNAME $FTPBASE/$USERNAME.deleted
;;
'passwd' )
check_username_notexist
get_password
#Get the row numbers of username and password of virtual user
ROWNUMBER=$(cat -n $USERFILE | sed -n 'p;n' | grep -w $USERNAME | awk '{print $1}' | head -n 1)
PASSWORDNUMBER=$(expr $ROWNUMBER + 1)
sed -i "${PASSWORDNUMBER}d" $USERFILE
sed -i "${ROWNUMBER}a $password" $USERFILE
update_userdb
;;
*)
echo "Usage: $0 {create|disable|enable|passwd|delete} {username}" >&2
exit 1
;;
esac附件下载:
uservsftpd.rar 1.09KB


评论: