Get the App
SLTechnology News&Howtos  ›  Servers  › 

Shell script practice-creating / deleting accounts in batches

Shulou Source: shulou.com Published: 2022-06-02 13:20:42 09月14日 Update

前言:该脚本即可批量创建用户,又可批量删除用户,具体针对哪个用户进行操作,是在一个文本文件中进行定义的。并且使用此脚本创建的用户,首次登陆必须修改密码。

[root@localhost ~]# vim create_user.sh usergroup="" #if your account need secondary group,add herepwmech="openssl" #"openssl" or "account" is needed.homeperm="no" # if "yes" then I will modify home dir permission to 711usepw="123456" #this is all user init password# 1.check the accountadd.txt fileaction="${1}" #"create" is useradd and "delete" is userdelif [ ! -f accountadd.txt ]; then echo "There is no accountadd.txt file,stop here" exit 1fi[ "${usergroup}" != "" ] && groupadd -r ${usergroup}rm -f outputpw.txtusernames=$(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]" ;; esacdone[root@localhost ~]# cat accountadd.txt #将需要创建的用户名写入该文件lv1lv2lv3lv4#进行测试[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/bashlv2:x:1005:1006::/home/lv2:/bin/bashlv3:x:1006:1007::/home/lv3:/bin/bashlv4:x:1007:1008::/home/lv4:/bin/bash[root@localhost ~]# sh create_user.sh delete #执行delete选项,进行删除deleting lv1deleting lv2deleting lv3deleting lv4

-------- 本文至此结束,感谢阅读 --------

Tags: User success password token identity update authentication script file preface text is in user name so far secondary login test actual combat batch account number Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno Shulou Technology NVidia Apple OPPO Reno vpn