FUNDAMENTALS A Complete Guide for Beginners
While working in Linux distributions, you may want to add multiple user accounts to your system. Adding a single user is pretty simple, but when it comes to creating a number of users this task can become both exhausting and time-consuming. Therefore, the Linux operating system offers the advantage of creating multiple users for your system at once. Apart from the system’s default settings, you can also develop scripts for creating more than one user. However, To add, remove or modify a user in Ubuntu , you need to be a root user or have root/sudo privileges . Here, In this article, you will find the convenient answers to the question “How to Create Multiple Users in Linux?”.
Process Flow Chart to Create Multiple Users in Linux
[Distro Used Throughout the Tutorial: Ubuntu 22.04.1 LTS]
Watch the Video to Create Multiple Users in Linux
2 Methods to Create Multiple Users in Linux
In Linux, there are different ways of creating multiple users. As Linux offers user-defined scripts, the useradd and adduser commands can be utilized as per the user’s need for creating more than one user. However, there is a default command newusers that adds multiple users to the system by reading user credentials from a text file.
In this article, I will demonstrate both approaches to creating multiple users in Linux.
You can read our Comparative Analysis of Methods to distinguish between these two methods and best pick one for your need.
Method 01: Using a Command & Text File to Create Multiple Users in Linux
You can add multiple users in your system simultaneously using the newusers command. However, first, you must create a .txt file that will contain the necessary credentials of the users. Each user information has to be written in a new line following a specified syntax.
In this example, I will create four users at a single time using the newusers command. The users will have the following usernames: UserA, UserB, UserC, and UserD. I will set the other credentials such as user passwords, IDs, GIDs, etc for each user in the “new_user.txt” file. You can follow the instructions below to do the same.
Steps to Follow >
➊ Open the Ubuntu terminal.
➋ Create a .txt file to write the new user’s credentials by typing the given command.
nano new_users.txt
- nano: Creates/edits text files with Nano text editor.
- new_users.txt: Text file to write multiple user information.
➌ Type your desired multiple-user information following the syntax below.
pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell
- pw_name: User name of the new user to be created.
- pw_passwd: Password for the new user.
- pw_uid: New user’s ID.
- pw_gid: New user’s group ID.
- pw_gecos: Comments sections.
- pw_dir: User’s home directory.
- pw_shell: User’s default shell.
❹ Change the .txt file’s permissions using the following command and hit ENTER.
sudo chmod 600 new_users.txt
❺ Input the password of the currently logged-in user.
❻ Create desired users by typing the command given below.
sudo newusers new_users.txt
- sudo: Grants administrative privileges.
- newusers: Reads a file and creates new users using file contents.
- new_users.txt: Name of the .txt file containing multiple user credentials.
❼ Finally, press the ENTER button.
❽ Now, to check if the desired users are actually created, write the following command.
tail -n 4 /etc/passwd
- tail: Prints the last few/ specified lines from a file.
- -n 4: Specifies the number of lines to be printed.
- /etc/passwd: Contains user information.
❾ Again, hit ENTER.Upon completion of the given steps, you can see in the image that 4 new users: UserA, UserB, UserC, and UserD have been added to my system at the same time. Moreover, I have printed the last 4 lines from the “/etc/passwd” file using the tail command to show you the newly added accounts.
- How to Create a Jenkins User on Ubuntu? [2 Methods]
- How to Create MySQL User in Ubuntu? [2 Cases]
- Create User Without Home Directory in Ubuntu in Just 3 Steps
- How to Create a User in Docker Container Without Docker File?
Method 02: Using Bash Script to Create Multiple Users in Linux
Another approach to creating multiple users in Linux is by utilizing the bash scripting technique. However, in this case, the creation of users will not be simultaneous. In fact, users will be created in a one-by-one manner. You can loop either the adduser or useradd command inside your script to achieve this task.
In this example, I will utilize the useradd command within a for loop inside my script for creating multiple users. To make the method more interactive, I will also use the If condition with logical operators (==,||) and some additional commands such as read, echo, psswd, etc. You can follow the process below to do the same.
Step 1: Preparing a Script for Creating Multiple Users
To prepare a script that creates multiple users in a loop follow the instructions below.
Steps to Follow >
➊ At first, open a Ubuntu terminal window by pressing CTRL+ ALT + T
➋ Then, create a bin folder in your home directory by typing the following command.
mkdir bin
- mkdir: Creates a Directory.
- bin: User’s private bin directory.
However, you can skip this step if the directory is already created.
➌ After that, create a bash script file inside the bin directory with the command below.
nano bin/create_users.sh
- nano: Creates/edits text files with Nano text editor.
- create_users,sh: File for writing bash script of creating multiple users.
❹ Now, write the following script in the create_users.sh file for creating multiple users in 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
- read -p “Message” variable: Displays a message while taking input from the terminal and stores in the variable.
- echo: Displays a given text.
- $Variable: Returns the value stored in a Variable.
- for (( InitialValue; TestExpression; UpdateStatement)); do done: Runs the commands inside the do and done keywords in a loop from the InitialValue. Updates the initial value using UpdateStatement or comes out of the loop depending on the TestExpression.
- sudo: Grants administrative privileges.
- useradd: Adds a new user.
- if [ condition ]; then else fi: Checks a condition. If the condition is true, execute the command/commands after “then”. Otherwise, execute the command after “else”.
- passwd: Sets/Changes password.
❺ To save and exit from the script, press CTRL + S and CTRL + X respectively.
❻ Now, Type the following to make the script executable for the current user in your system.
chmod u+rwx bin/create_users.sh
- chmod: Changes folder permissions.
- u+rwx: Adds read, write and execute permissions for the current user.
- sh: Bash script file for creating multiple users.
❼ Finally, restart your system to add the newly created bin directory to the $PATH variable by typing the command below.
sudo reboot
Restarting the system by default runs the .profile script which adds the user’s private bin directory to $PATH and makes the files inside bin directory accessible for every shell session.
Step 2: Executing Script for Creating Multiple Users
After restarting the system you will be able to run the “create_users.sh” script from any path under the current user account. To learn how you can execute the script for creating multiple users follow the steps below.
Steps to Follow >
➊ At first, press CTRL+ALT+T to open the Ubuntu Terminal.
➋ Run the previously written script by simply typing the file name.
create_users.sh
➌ Type the number of users you want to create and hit ENTER. In this example, I will add 2 new users to my system. Therefore, I will type “2” and press ENTER.
❹ Create each user with or without passwords.
tail -n 2 /etc/passwd
- tail: Prints the last few/ specified lines from a file.
- -n 2: Specifies the number of lines to be printed.
- /etc/passwd: Contains user information.
❻ Again, hit ENTER.
After completing both Step1 and Step2, in the above image, you can see that I have successfully added two new users to my system. On top of that, this “create_users.sh” script is now executable at any time from any path of the current user.
This method is planned in such a way that you can easily add multiple users to your distribution even after closing the System or the Terminal. Furthermore, you may run the tail command on the “/etc/passwd” file to view the newly added users.
- How to Create an FTP User in Ubuntu? [Step-by-Step]
- Create a New SFTP User in Ubuntu with a New SSH Key
- How to Create Group and Add User in Ubuntu? [2 Cases]
- Create FTP User for Specific Directory in Ubuntu [4 Steps]
Comparative Analysis of Methods
Since this article introduces multiple methods for a single task, you might get confused about which one to pick. Therefore, in this section, I am presenting a comparative analysis of the two different approaches for your convenience. You can learn the advantages and disadvantages of the available methods and choose which is the best for you.
Methods | Pros | Cons |
---|---|---|
Method 1 |
|
|
Method 2 |
|
|
To sum up, it can be said that both methods come with their own pros and cons. As a consequence, the suitable method varies from user to user. If you want to create multiple users with the complete information you can go for Method 1. On the other hand, If you want to automate the creation of multiple users and new users having only the username with/without password suffices, then you can use the 2nd approach.
Complementary Information
Besides, knowing about creating multiple users in Linux, you will find the below information helpful.
How to Add Multiple Users in a Group in Linux
You can add multiple users to a group in Linux using the gpasswd command in Linux. You must run this command as a root user using the sudo keyword to achieve this task. In this example, I will add the users: UserA, UserB and UserC to the group named “multi_users” available on my machine. To do the same you can follow the steps below.
Steps to Follow >
➊ Open the Ubuntu terminal.
➋ Then, type the following command in the command prompt.
sudo gpasswd -M UserA,UserB,UserC multi_users
- sudo: Grants administrative privileges.
- gpasswd -M: Adds multiple users to a group.
- UserA,UserB,UserC: List of multiple users to be added to the group.
- multi_users: Name of the group to add users.
➌ Press the ENTER button.
➍ Then, give the password of the current user and hit ENTER.
➎ Now, check the group members by typing the command below and hit ENTER.
sudo less /etc/group| grep "multi_users"
In the above image, you can see that I have successfully added multiple users to the group “multi_users”. I have also displayed the updated user list of the group by applying the less and grep command on the “/etc/group” file contents.
- How to Create a Root User in Ubuntu [Step-by-Step]
- Create a User in Ubuntu as Read-Only Access to Log Folder
- How to Create User Account in Ubuntu with Public Key
- Create A User in Ubuntu that Runs a Script and Logs Out [4 Steps]
Conclusion
In this article, I have illustrated two different ways of conveniently creating multiple users in Linux. You can use the conventional way of creating many users at the same time or you can modify your experience of adding a number of users by writing a script as per your requirement. Furthermore, this article provides you with explanatory steps to go through any of the aforementioned approaches. I hope, following these methods will help you become a power user of Linux.
People Also Ask
Can a Linux user be in multiple groups?
Yes. You can add a Linux user to as many groups as you want. There is no limited group number for each user in LInux.
How Linux is multi-user?
Linux is a multi-user operating system because it allows multiple users to access its resources at the same time. To clarify, system resources are the memory/ ram/ application programs, etc.
What is multi-user mode in Linux?
The multi-user mode in Linux indicates the Linux itself since it is a multi-use operating system. Multiple users at the same time can access the Linux system’s memory/ ram/ application programs, etc.
How do I list users in Linux?
To list the users in Linux, you can run the cat command on the “etc/passwd” file. It is a text file that contains all the user’s information.
What are the 3 types of users in Linux?
The types of users in Linux are as follows: 1)Super user: also known as the root user that has all the administrative privileges. 2)Normal user: Generally created users not having access to system files. 3) System user: Created to serve specific purposes.
How many users can be in a group Linux?
There is no specified number of users to be in a group. You can add as many users as you like to a group. Moreover, the Linux distribution supports an unlimited number of group members.
Relaed Articles
- How to Create User and Add to Sudo in Ubuntu? [2 Methods]
- 2 Ways to Create a User With Access to a Drive in Ubuntu
- How to Create a New User in Ubuntu by GUI? [Step-by-Step]
- 2 Ways to Create User Account in Ubuntu Using Terminal
- How to Create Home Directory for Existing User in Ubuntu
- Create User with UID and GID in Ubuntu? [3 Scenarios]
- How to Create a Sudo User in Ubuntu? [Step-by-Step]
I followed all directions, made 3 new users without password using the bash method. The new users are made, but it keeps asking for a password. So I cannot change to the new user for I do not know the password. What is the reason and how to change it? Another question: how to delete a new user )for I maybe have to do this to start over again. And can I change a password of an existing user?