Get the App
SLTechnology News&Howtos  ›  Servers  › 

How to add users in batch through shell script

Shulou Source: shulou.com Published: 2022-06-01 03:00:30 10月02日 Update

This article mainly introduces how to add users in batches through the shell script, which has a certain reference value, and interested friends can refer to it. I hope you can learn a lot after reading this article.

There are two cases where for adds specified types of users and sets random passwords. One is to test the for cycle to add users in batches, and the other is to add users in batches according to the actual production environment. Write down the cases separately as follows:

Script function: batch add user1-10 users and set random 8-digit random passwords

#! / bin/bash#test add user and set passwdfor n in `seq 10`douseradd user$n;echo user$n | passwd `seradd | md5sum | cut-C1-8`done

Add users and set corresponding passwords in batch in the actual production environment

Script function: add specified users in batch in production environment, set corresponding passwords, and prohibit sudo-s and sudo-I from switching to root.

Idea: put all the usernames and passwords you want to add into an array and set a function to intercept the username and password and add the user and set the password. Finally, you can add it by calling this function with a loop.

#! / bin/bash#adduser and setpasswd userlist= (user21:123456 user22:456789 user23:876543) function adduser_setpasswd_sudo () {user= `echo $1 | awk-F:'{print $1} '`passwd= `echo $1 | awk-F:' {print $2} '``useradd-m-s / bin/bash $userecho "$user:$passwd" | chpasswdsed-I "/ ${user} / d" / etc/sudoersecho "${user} ALLNOPASSWD Groupe ! / bin/bash "> > / etc/sudoers} for n in ${userlist [@]} doadduser_setpasswd_sudo $ndone

Delete the account to be cancelled and cancel the sudo permission

#! / bin/bashcat < < EOF### delete username and cancle sudo privileges.## # EOFread-p "Please input username which you want to del:" usernameuserdel-r $usernamesed-I "/ ${username} / d" / etc/sudoers echo "Delete $username finished." Thank you for reading this article carefully. I hope the article "how to add users in bulk through shell script" shared by the editor will be helpful to everyone. At the same time, I also hope you can support us and follow the industry information channel. More related knowledge is waiting for you to learn!

Tags: Users passwords scripts articles environment production functions functions actual user names loops values digits interests meanwhile ideas situations arrays more friends Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno OPPO Reno Docker Huawei Linux Microsoft