In linux, shell batch add users and set random password scripts

Source: Internet
Author: User

A friend asked me how to create users and set passwords in batches. I simply wrote two scripts for him to let him know and modify the scripts based on the actual situation, after all, the username and password he needs are in the actual production environment.

There are two cases: one is to test the for loop to add users in batches, and the other is to add users in batches according to the actual production environment. Write the following cases respectively:
1. for, add users of the specified type and set random passwords.
Script role: Batch add user1-10 users and set random 8-digit random passwords

#! /Bin/bash
# Test add user and set passwd
For n in 'seq 10'
Do
Useradd user $ n;
Echo user $ n | passwd 'echo user $ n | md5sum | cut-c1-8'
Done
2. Batch add users and set corresponding passwords in the actual production environment
Script: add specified users in batch in the production environment and set the corresponding password.
Train of Thought: put all the usernames and passwords to be added into an array, set a function to intercept the usernames and passwords, add users, and set passwords. Finally, you can use a loop to call this function.

#! /Bin/bash
# Adduser and set passwd
 
Userlist = (
User21: 123456
User22: 456789
User23: 876543
)
Function adduser_setpasswd (){
User = 'echo $1 | awk-F: '{print $1 }''
Passwd = 'echo $1 | awk-F: '{print $2 }''
Useradd-m-s/bin/bash $ user
Echo "$ user: $ passwd" | chpasswd
}
 
For n in $ {userlist [@]}
Do
Adduser_setpasswd $ n
Done

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.