Bulk add users and set random passwords through shell scripts and how the production environment is added in bulk
Original: http://www.21yunwei.com/archives/4773
A friend asked me how to batch create user and set the password, I simply wrote him two scripts, let his own reference and his own according to the actual situation to modify, after all, he needs the user name and password are the actual production environment.
There are two cases in which a test for loop is added in bulk and one is bulk added based on the actual production environment. Write each case as follows:
1,for adds the specified type of user and sets a random password.
Script function: Bulk add user1-10 users and set random 8-digit random password
- #!/bin/bash
- #test add user and set passwd
- For n in ' seq Ten '
- Do
- Useradd user$n;
- echo user$n | passwd ' echo user$n |md5sum |cut-c1-8 '
- Done
2, Actual production environment batch add user and set corresponding password
Scripting: Add the specified user in bulk and set the corresponding password in the production environment.
Idea: Put all the username and password to be added in an array, set a function to intercept the user name and password and to add the user, set the password. This function can then be added with a loop call.
- #!/bin/bash
- #adduser and set passwd
- UserList= (
- User21:123456
- User22:456789
- User23:876543
- )
- function Adduser_setpasswd_sudo() {
- User=' echo $ |awk-f: ' {print '} '
- passwd=' echo $ |awk-f: ' {print $} '
- Useradd -m -s /bin/bash $user
- Echo "$user: $passwd"| CHPASSWD
- Sed -i "/${user}/d" /etc/sudoers
- echo "${user} all=nopasswd:all,!/usr/bin/passwd,/usr/bin/passwd[a-za-z]*,!/usr/bin/passwd root,!/bin/su" >> /etc/sudoers
- }
- For N in ${userlist[@]}
- Do
- Adduser_setpasswd_sudo $n
- Done
3. Delete the account you want to cancel and remove sudo permissions
- #!/bin/bash
- Cat < <EOF
- ##############################################################
- Delete username and cancle sudo privileges.
- ##############################################################
- Eof
- Read -p "Please input username which want to del:" username
- Userdel -R $username
- Sed -i "/${username}/d" /etc/sudoers
- echo "Delete $username finished."
(go) Bulk add users and set random passwords through shell scripts and how the production environment is added in bulk