前言:该脚本便可批量建立用户,又可批量删除用户,具体针对哪一个用户进行操做,是在一个文本文件中进行定义的。而且使用此脚本建立的用户,首次登录必须修改密码。vim
[root@localhost ~]# vim create_user.sh usergroup="" #if your account need secondary group,add here pwmech="openssl" #"openssl" or "account" is needed. homeperm="no" # if "yes" then I will modify home dir permission to 711 usepw="123456" #this is all user init password # 1.check the accountadd.txt file action="${1}" #"create" is useradd and "delete" is userdel if [ ! -f accountadd.txt ]; then echo "There is no accountadd.txt file,stop here" exit 1 fi [ "${usergroup}" != "" ] && groupadd -r ${usergroup} rm -f outputpw.txt usernames=$(cat accountadd.txt) for username in ${usernames} do case ${action} in "create") [ "${usergroup}" != "" ] && usegrp=" -G ${usergroup} " || usegrp="" useradd ${usegrp} ${username} echo ${usepw} | passwd --stdin ${username} #set password chage -d 0 ${username} [ "${homeperm}" == "yes" ] && chmod 711 /home/${username} echo "username=${username},password=${usepw}" >> outputpw.txt ;; "delete") echo "deleting ${username}" userdel -r ${username} ;; *) echo "Usage:$0[create | delete]" ;; esac done [root@localhost ~]# cat accountadd.txt #将须要建立的用户名写入该文件 lv1 lv2 lv3 lv4 #进行测试 [root@localhost ~]# sh create_user.sh create #执行create选项,进行建立 更改用户 lv1 的密码 。 passwd:全部的身份验证令牌已经成功更新。 更改用户 lv2 的密码 。 passwd:全部的身份验证令牌已经成功更新。 更改用户 lv3 的密码 。 passwd:全部的身份验证令牌已经成功更新。 更改用户 lv4 的密码 。 passwd:全部的身份验证令牌已经成功更新。 [root@localhost ~]# tail -n 4 /etc/passwd #查看是否建立成功 lv1:x:1004:1005::/home/lv1:/bin/bash lv2:x:1005:1006::/home/lv2:/bin/bash lv3:x:1006:1007::/home/lv3:/bin/bash lv4:x:1007:1008::/home/lv4:/bin/bash [root@localhost ~]# sh create_user.sh delete #执行delete选项,进行删除 deleting lv1 deleting lv2 deleting lv3 deleting lv4
———————— 本文至此结束,感谢阅读 ————————bash