How to Create Multiple Users in Linux?

How to Create Multiple Users in Linux?

When working within complex systems such as within large-scale computer networks or IT operations involving multiple applications or software running simultaneously under different user's control. so, Linux OS offers to create multiple users for your system at once.

Method 1: Using a Command & Text file to Create multiple Users in Linux.

Step 1: Open the Linux terminal

Step 2: Create .txt file to write the new user's credentials

nano new_users.txt

step 3: Type your desired multiple-user information in syntax below.

user_name:user_password:Uid:Gid:comment:home_dir:def_shell

here, User_name: user name of the new user to be created

user_password: password for the new user

Uid: New user's ID

Gid: New user's group ID

comment: Comment section or User Full Name

home_dir: User's home directory

def_shell: User's default shell

Step 4: change the .txt file's permissions using the following command

sudo chmod 600 new_users.txt

Step 5: Input the password of the currently logged in user.

Step 6: Create desired users by typing the command bleow and press Enter.

sudo newusers new_users.txt

Step 7: Now, check if the desired users are created.

tail /etc/passwd

Method 2: Using Bash Script to Create Multiple Users in Linux.

Step 1: Open the terminal or Press CTRL + ALT + T

Step 2: Create a directory name bin in your home directory

mkdir bin

Step 3: After that , Create a bash Script file inside the bin directory

nano bin/create_users.sh

Step 4: Now, write the following script in the create_users.sh file for creating multiple users in a loop.

#!/bin/bash

read -p "Enter number of Users to create:" num
echo "------------------------------------------------------"
for (( i=1; i<=$num; i=i+1)); do
read -p "Enter Username no.$i: " uname
sudo useradd $uname
read -p "Enter password for user no.$i? [Y/N] " flag
if [[ "$flag" == "Y" || "$flag" == "y" ]]; then
sudo passwd $uname
echo "Successfully created New User: $uname with password."
echo "------------------------------------------------------"
else
echo "Successfully created New User: $uname without password."
echo "------------------------------------------------------"
fi
done

Step 5: To save and Exit from the Script, press CTRL + S and CTRL + X respectively

Step 6: Now, type the following command to permit the current user in your system.

chmod u+rwx bin/create_users.sh

Step 7: Finally restart your system to add the newly created bin directory to the $PATH variable

sudo reboot

Step 8: Now open your terminal or Press CTRL + ALT + T

Step 9: Run the previously written script by typing the file name.

create_users.sh

Step 10: After entering the data, to check if the desired number of users is created.

tail /etc/passwd

Complementary Information: Adding Multiple Users in a Group

Step 1: Open the Terminal

Step 2: Type the following command

  • To create a group for now we are creating a group named "Student"
sudo groupadd Student
  • To add multiple users type the following command.
sudo gpasswd -M UserA,UserB,UserC Group_Name

Step 3: To Check the Group Members

sudo less /etc/group| grep "Group_Name"
or 
sudo grep Student /etc/group

As you can see that the users are inside the group named Student.

That's all for now, Happy Coding!!!.