How to Create Multiple Users with Bash Script in Linux

Writing a simple Linux bash script to create multiple users accounts helps you approach user account creation in Linux a bit faster. But this just a simple bash script to create multiple users. You are free to add more options and features this script. We would appreciate if your share your script with us.

This bash script creates users with default “Pass” password, so then you or users can change their password at first login.

The main purposes of this article is to introduce you to bash scripting with simple user creating bash script. To get a professional bash scripting skills in Linux, once you have to read a bout about bash scripting and practice 1K+ times a day!

Create User Account using Bash Script
Create User Account using Bash Script

Create Multiple Users using Bash Script

#!/usr/bin/env bash

#Simple Script to Create Multiple Users in Linux

#Users list in a file. 

if [ "USERFILE" = "" ]
	  echo "Please specify the input users file!"
	  exit 35
elif test -e $USERFILE
	for user in `cat $USERFILE`
		echo "Creating the "$user" user."
			useradd -m $user ; echo "$user:Pass" | chpasswd
	exit 40
	echo "Invalid input users file"
	exit 45

To run the script successfully, you need to make it executable with chmod +x command. Red more Linux command lines.

Create User with Bash Script
Create User with Bash Script

After creating the users with bash script, try to read the user’s name via passwd file in /etc directory.

[root@vma ~]# grep -E "Alhamis|Brandy|Irena" /etc/passwd
Create Multiple Users with Bash Script
Create Multiple Users with Bash Script


Creating Users with one Command Line

You can create multiple users in one command line with for loop or while loop.

for user in Alhamis Brandy Irena ; do useradd -m $user ; done

The same command goes with deleting user account. But just replace the useradd command with userdel command.

for user in Irena Alhamis Brandy ; do userdel -r $user ; done

To read more about the effects of adding users to a Linux system from Red Hat website. There are some interesting things will happen when you add a new user to a Linux system, you also change several files. The article covers a journey of learning about the Linux filesystem architecture and using basic knowledge to write a shell script to create Linux users.

The focus is on files located in the /etc directory, which stores the system configuration information. It demonstrate how to work with system files and to create a new user.

Leave a Comment