How To Create Multiple Users Using Shell Script.
In Linux, we create users for multiple purposes, so in Linux, it’s very common to make new users depending on the tasks. So sometimes we need to create more than one user or multiple users. We can’t do it one by one as it would be very time-consuming, so we can use automated scripts to make our tasks easy.
Method 1: Using Terminal
In this method, we will not be using any script, we will just use a command to make multiple users in simple steps.
Step 1: Create a file and list down the names of users in it.
Step 2: Run for loop given below
Step 3: To view the created users simply type “id” in place of useradd
Step 4: To give different passwords to each user, interactively use “passwd” in place of useradd.
Method 2: Using Shell script
Step 1: First, we have created a text file containing all the names of users. and saved it at a specific location.
Step 2: After that we write a script to automate the task.
Step 3: We wrote the following script
Tags used in the script :
userfile — gave the location of the file with all the usernames it contains.
username — read the file using ‘cat’, translate all the uppercase letters to lowercase letters, because we never know in what format the user has given the name.
password — will be your username @123.
We ran a loop for usernames, following useradd command with all usernames.
echo — ‘wc -l’ count lines in those files, and prints the number of files.
tail — Used to check all the details.
Step 4: Give permissions to the script file. u+x, the only user will be able to execute this file