(go) Bulk add users and set random passwords through shell scripts and how the production environment is added in bulk

Source: Internet
Author: User

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

    1. #!/bin/bash
    2. #test add user and set passwd
    3. For n in ' seq Ten '
    4. Do
    5. Useradd user$n;
    6. echo user$n | passwd ' echo user$n |md5sum |cut-c1-8 '
    7. 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.

  1. #!/bin/bash
  2. #adduser and set passwd
  3. UserList= (
  4. User21:123456
  5. User22:456789
  6. User23:876543
  7. )
  8. function Adduser_setpasswd_sudo() {
  9. User=' echo $ |awk-f: ' {print '} '
  10. passwd=' echo $ |awk-f: ' {print $} '
  11. Useradd -m -s /bin/bash $user
  12. Echo "$user: $passwd"| CHPASSWD
  13. Sed -i "/${user}/d" /etc/sudoers
  14. echo "${user} all=nopasswd:all,!/usr/bin/passwd,/usr/bin/passwd[a-za-z]*,!/usr/bin/passwd root,!/bin/su" >> /etc/sudoers
  15. }
  16. For N in ${userlist[@]}
  17. Do
  18. Adduser_setpasswd_sudo $n
  19. Done

3. Delete the account you want to cancel and remove sudo permissions

    1. #!/bin/bash
    2. Cat < <EOF
    3. ##############################################################
    4. Delete username and cancle sudo privileges.
    5. ##############################################################
    6. Eof
    7. Read -p "Please input username which want to del:" username
    8. Userdel -R $username
    9. Sed -i "/${username}/d" /etc/sudoers
    10. echo "Delete $username finished."

(go) Bulk add users and set random passwords through shell scripts and how the production environment is added in bulk

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.