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 Multiple Users using Bash Script
#!/usr/bin/env bash #Simple Script to Create Multiple Users in Linux #Users list in a file. USERFILE=$1 if [ "USERFILE" = "" ] then echo "Please specify the input users file!" exit 35 elif test -e $USERFILE then for user in `cat $USERFILE` do echo "Creating the "$user" user." useradd -m $user ; echo "$user:Pass" | chpasswd done exit 40 else echo "Invalid input users file" exit 45 fi
To run the script successfully, you need to make it executable with
chmod +x create_user.sh command. Red more Linux command lines.
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
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.