Create multiple users using shell script in Linux
Last Updated :
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.
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.
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

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.
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

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
//here we are giving the executable permission
//of the file to the user.
chmod u+x /usr/sbin/createuser.sh
Similar Reads
Deleting a User in Linux using Python Script
Deleting a user from your system or server via a python script is a very easy task. You just need to pass the username of the user and the script will remove the details and all the files of that user.This python script uses userdel Linux command to delete the user.You can directly use userdel comma
2 min read
How to Create a Shell Script in linux
Shell is an interface of the operating system. It accepts commands from users and interprets them to the operating system. If you want to run a bunch of commands together, you can do so by creating a shell script. Shell scripts are very useful if you need to do a task routinely, like taking a backup
7 min read
How to Run a Shell Script in Linux
Shell scripts are a powerful way to automate tasks and manage system processes in Linux. These scripts, written in shell programming languages like Bash, allow users to execute a sequence of commands efficiently. In this guide, we'll show the steps to check a shell script in Linux before running it,
4 min read
Shell Scripts to Find How Many Users are Logged In
Every operating system provides a feature of multiple user accounts. Linux-based operating systems have some commands or functionalities to check user accounts' details and change them. This ability is mainly used by the admin account user that is the root user, to provide permissions and access to
7 min read
How to Create a File in the Linux Using the Terminal?
In this article, we will learn to create a file in the Linux/Unix system using the terminal. In the Linux/Unix system, there are the following ways available to creating files. Using the touch commandUsing the cat commandUsing redirection operatorUsing the echo commandUsing the heredocUsing the dd c
4 min read
Shell Script to Demonstrate Wait Command in Linux
Wait command is one of the process management commands. There are different process commands in Linux mainly 5 commands are widely used which are ps, wait, sleep, kill, exit. ps is an acronym for process status. It displays information about the active processes. wait command will suspend execution
4 min read
How to add multiple users to a group at once in linux?
Managing user groups in Linux is an essential part of system administration. Often, you'll find the need to add multiple users to a specific group simultaneously. This article provides a detailed explanation of how to accomplish this task, covering every aspect, and includes examples for clarity.Und
5 min read
Create a password generator using shell scripting
Creating a strong password is often a time-consuming task and even after creating a good password by yourself it gets brute-forced by hackers. In this article, we will learn how to create a strong password which fulfills all requirements including symbols, capitals length etc using a simple shell sc
1 min read
Implementing Directory Management using Shell Script
Directory management constitutes the functions dealing with organization and maintenance of various directories. Directories usually contain files of any type, but this may vary between file systems. The content of a directory does not affect the directory object itself. Some of the directory functi
3 min read
Shell Script to Automate System Updates and Upgrades in Linux
Shell Scripts are often used for automating repetitive tasks that we can perform on a daily basis. With shell scripts, you can automate tasks like renaming a bunch of files or making backup scripts and many more complex tasks. As we all know, we often need to update and upgrade Linux packages and de
4 min read