在实际中,我们常常需要在一台机子上批量创用户,并设置初始密码。下面的脚本就可以实现批量创建用户:
#!/bin/sh
if [ "$#" != "4" ];then
echo "we need more parameter,please run this shell like:"
echo "sudo ./newuser.sh number username userid usergroup"
exit 1;
fi
if [ "$USER" != "root" ];then
echo "must run with root user, exit..."
exit 1
fi
number=`expr $1 + 1`
username=$2
userid=$3
usergroup=$4
user=1
while [ $user -ne $number ]
do
if [ $user -lt 10 ];then
name="$username"0"$user"
else
name=$username$user
fi
homedir=/home/$name
echo "$name:x:$userid:$usergroup:$name:$homedir:/bin/bash">>newuser.conf
echo "$name:$name">>passwd.conf
echo "$name">>username.conf
user=`expr $user + 1`
userid=`expr $userid + 1`
done
newusers newuser.conf
chpasswd < passwd.conf
#users=`cat username.conf` #此注释部分是给创建的用户添加执行sudo命令的权限
#for user in $users
#do
# echo "$user ALL=(ALL) ALL">>/etc/sudoers
#done
#users=`cat username.conf`
#for user in $users #此注释部分是批量删除创建的用户
#do
# deluser $user
# rm -r /home/user
#done
rm newuser.conf
rm passwd.conf
rm username.conf
|
运行形式如下:
sudo ./newuser.sh number username userid usergroup
说明:
number 需要添加用户的个数
username 用户名的前缀,比如如果username=rj0601,则创建的用户名行如rj060101,rj060102...
userid 批量用户的初始id,比如userid=2000,则批量创建的用户的id依次为2001,2002...
usergroup 用户所属组(批量创建的用户属于同一组)
阅读(3291) | 评论(1) | 转发(0) |