Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

Shell script practice-creating / deleting accounts in batches

2025-01-19 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Servers >

Share

Shulou(Shulou.com)06/02 Report--

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

[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

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

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 0

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Servers

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report