Create multiple users using shell script in Linux - GeeksforGeeks

03 Aug, 2021
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. 
Here we have used a shell script to make multiple users in just one go.
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.
touch /opt/usradd
Step 2: Run for loop given below
for i in `cat /opt/usradd` ; do useradd $i ; done
Step 3: To view the created users simply type “id” in place of useradd
for i in `cat /opt/usradd` ; do id $i ; done
OR
awk - F: '{print $1}' /etc/passwd
Step 4: To give different passwords to each user, interactively use “passwd” in place of useradd.
for i in `cat /opt/usradd` ; do passwd $i ; done
Step 1: First, we have created a text file containing all the names of users. and saved it at a specific location.
create a text file userlist with all the usernames in it.

vim /tmp/userlist
Step 2: After that  we write a script to automate the task.
creating a .sh file to write the script in it.

vim /usr/sbin/createuser.sh
Step 3: We wrote the following script
//execute the Script using a bash shell
#!/bin/bash

//location of the txt file of usernames
userfile=/tmp/userlist 

//extracting usernames from the file one-by-one
username=$(cat /tmp/userlist | tr 'A-Z'  'a-z')

//defining the default password 
password=$username@123

//running loop  to add users
for user in $username
do
       //adding users '$user' is a variable that changes
       // usernames accordingly in txt file.
       useradd $user
       echo $password | passwd --stdin $user
done

//echo is used to display the total numbers of 
//users created, counting the names in the txt 
//file, tail to display the final details of 
//the process on both lines(optional)
echo "$(wc -l /tmp/userlist) users have been created" 
tail -n$(wc -l /tmp userlist) /etc/passwd
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 
//here we are giving the executable permission 
//of the file to the user.
chmod u+x /usr/sbin/createuser.sh
Linux-Shell-Commands
Shell Script
Linux-Unix
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
tar command in Linux with examples
Conditional Statements | Shell Script
Tail command in Linux with examples
'crontab' in Linux with Examples
diff command in Linux with examples
UDP Server-Client implementation in C
Cat command in Linux with examples
touch command in Linux with Examples
echo command in Linux with Examples
scp command in Linux with Examples