- How to Create Multiple User Accounts in Linux?
- Synatx
- User Details
- Giving Permissions to the User Details File
- Run newusers Command
- Verify Added Users
- How to Create Multiple User Accounts in Linux
- Create multiple users and set passwords for each in Ubuntu Linux
- Type of Users in Linux
- Administrative Root Users
- Regular Users
- Service Users
- Create Multiple Users
- Set Passwords for Multiple User Accounts
- How to Create Multiple User Accounts in Linux
- How to Create Multiple User Accounts in Linux
How to Create Multiple User Accounts in Linux?
Adding a single new user to a Linux system can be achieved through the useradd command. But system admins often get request to add many users. So Linux provides a different to do a bulk addition of many users to a system.This is the newusers command.
Synatx
sudo newusers user_deatils.txt user_details.txt is the file containing the details of all the usernames to be added.
User Details
Below we see the structure of user_details.txt file.
UserName:Password:UID:GID:comments:HomeDirectory:UserShell
So we create a file with below details to add many usres.
~$ cat MoreUsers.txt uname1:pwd#@1:2112:3421:storefront:/home/uname1:/bin/bash uname3:pwd#!@3:2112:3525:backend:/home/uname3:/bin/bash uname4:pwd#$$9:9002:4721:HR:/home/uname4:/bin/bash
Giving Permissions to the User Details File
Before we sue the user details file to add new users, we should give permission to it to be read by other processes.
sudo chmod 0600 MoreUsers.txt
Lets verify the existing users in the system by going to the /etc/passwd file.
ubuntu@ubuntu:~$ tail -5 /etc/passwd
Running the above code gives us the following result −
pulse:x:117:124:PulseAudio daemon. /var/run/pulse:/bin/false rtkit:x:118:126:RealtimeKit. /proc:/bin/false saned:x:119:127::/var/lib/saned:/bin/false usbmux:x:120:46:usbmux daemon. /var/lib/usbmux:/bin/false ubuntu:x:1000:1000:ubuntu16LTS. /home/ubuntu:/bin/bash
Run newusers Command
next we run the newusers command to add these usernames.
sudo newusers MoreUsers.txt
Verify Added Users
Now we verify that indeed those users are added , by going again to the /etc/passwd file.
Running the above code gives us the following result −
……….. …………. ubuntu:x:1000:1000:ubuntu16LTS. /home/ubuntu:/bin/bash uname1:x:2112:3421:storefront:/home/uname1:/bin/bash uname3:x:2112:3525:backend:/home/uname3:/bin/bash uname4:x:9002:4721:HR:/home/uname4:/bin/bash
How to Create Multiple User Accounts in Linux
The two utilities for adding or creating user accounts in Unix/Linux systems are adduser and useradd. These commands are designed to add a single user account in the system at a time. What if you have multiple users accounts to be created? That’s when you need a program such as newusers.
Newusers is a useful command line utility used to update and create new user accounts at a single time. It is intended to be used in IT environments with large systems where a system administrator needed to update or create multiple user accounts in batch. It reads information from stdin (by default) or a file to update a set of existing user accounts or to create new users.
In this article, we will explain how to create multiple user accounts in batch mode using Newusers utility in Linux systems.
To create users in a batch, you can provide their information in a file in the following format, same as the standard password file:
pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell
- pw_name: username
- pw_passwd: user’s password
- pw_uid: user’s ID
- pw_gid: user’s group ID
- pw_gecos: defines comments sections.
- pw_dir: defines the home directory of the user.
- pw_shell: defines user’s default shell.
Attention: You should protect the input file since it contains unencrypted passwords, by setting the appropriate permissions on it. It should only be readable and writable by root.
For example, to add the user accounts ravi and tecmint, you can create a files called users.txt as shown.
Next, add the user accounts details in the file in the following format.
ravi:213254lost:1002:1002:Tecmint Admin:/home/ravi:/bin/bash tecmint:@!#@%$Most:1003:1003:Tecmint:/home/tecmint:/bin/bash
Save the file and set the required permissions on it.
Now run the newusers command with the input file to add the above user accounts at once.
First, newusers program tries to create or update the specified accounts, and then write these changes to the user or group databases. In case of any errors except in the final writes to the databases, no changes are committed to the databases. This is simply how the newusers command works.
If the previous command is successful, check the /etc/passwd and /etc/groups files to confirm that the user accounts have been added as shown.
$ cat /etc/passwd | grep -E "ravi|tecmint"
For more information, see the newuser man page.
You might also like to check these following related articles.
In this article, we’ve explained how to create multiple users in Linux using newusers program. Use the feedback form below to ask any questions or share your comments with us. If you know of any similar utilities out there, let us know as well.
Create multiple users and set passwords for each in Ubuntu Linux
Sometimes we are tasked with creating more than one or multiple users at the same time. We can’t start creating the users one by one when tasked set before us is to be accomplished within a limited time. Hence, this short guide is aim at showing you how to create multiple users at the same time and set a password for each one of them.
Before we move on let’s discuss the types of users we have available in Linux.
Type of Users in Linux
Linux being a multi-user OS that is based on the Unix concepts of file ownership and permissions to provide security at the file system level has two types of users that’s; root users (system users), regular users, and service users.
Administrative Root Users
Primarily, Linux administrative root account is automatically created when you install Linux, and it has administrative privileges for all services on Linux OS. The root account is also known as superuser. The root user or superuser also has the ability to override any file ownership and permission restrictions. In real-time scenarios, this means that the superuser has the right to access anything on its own server. This user is used to make system-wide changes and must be kept secure. It is also possible to configure other user accounts with the ability to assume “superuser rights”. In fact, creating a normal user that has sudo privileges for system administration tasks is considered the best practice.
Regular Users
Regular users have the necessary privileges to perform standard tasks on a Linux OS such as running word processors, databases, and Web browsers. They can store files in their own home directories. Since regular users do not normally have administrative privileges, they cannot accidentally delete critical operating system configuration files. Hence, the need to apply caution before using the sudo (superuser do) command.
Service Users
Services such as Apache, Docker, Kubernetes, Squid, mail, games, and printing have their own individual service accounts. These accounts exist to allow each of these services to interact with your computer.
The list of users can be found in the /etc/passwd or awk -F’:’ » /etc/passwd while /etc/gshadow is used to store users’ passwords.
Users and Passwords in Linux
To understand it better, there are two standard users in Linux that is; system users and regular users. From the above, the administrative user or super user or root user, and service users are under system users created by default immediately the Liinux OS is set up while the regular users are created by the system administrator
Having introduced briefly the types of users available in Linux, let’s go over to the main task of the day which is creating multiple users at the same time and setting passwords for each of them.
Create Multiple Users
To create multiple users at the same time we will use the following step.
Step 1: Create a directory and list down the names of users you which to create in it by running touch /opt/useradd . In the screenshot below, we created the /opt/useradd , used vim /opt/useradd to edit and add the names of the users we want to add as admin1, admin2, admin3, admin4 and admin5 respectively. Of course, the /opt/ directory is a reserved directory so it’s read-only. While writing down the list of users in Vim editor you can’t write to it using the normal :wq! command but :w !sudo tee % command as shown in the screenshot below.
To be sure the names have been written and saved, let’s view the /opt/useradd directory by typing cat /opt/useradd
Step 2: As the next step, let’s run ‘for loop’ as shown below to create the users
Step 3: Run the command below to view the list of newly added users and respective IDs. Not each user in Linux is mapped to a unique ID when created.
for i in `cat /opt/useradd` ; do sudo useradd $1 ; done
Note this awk -F':' '' /etc/passwd of cat /etc/passwd command can also be used to view the list of user accounts
Set Passwords for Multiple User Accounts
We are done with creating multiple users at the same time. From the above we have created 5 users at the same time, now it’s time to secure their accounts by setting passwords for each of the accounts.
To set different passwords to each user, interactively run the below command
for i in `cat /opt/useradd` ; do sudo passwd $i ; done
We have successfully updated passwords for all the users we created. You can go ahead and switch users using the su username command. For example, let’s switch users to one of the users we just created. Let’s say admin1 that’s su admin1 and use the whoami command to confirm that it’s admin1 account.
That’s all. In this post we have learned about different types of users in Linux, creating multiple user accounts and setting passwords for each of them at the same time.
How to Create Multiple User Accounts in Linux
Often system administrators need to create multiple users. But the typical commands for user creation, adduser and useradd, allow you to create only one user at a time. So what to do in such cases? You can use newuser for this purpose. Newuser command allows you to create as well as update multiple new user accounts at a time. It allows you to input user information via file or standard input (stdin), or update a set of users. In this article, we will learn how to create multiple user accounts in Linux.
How to Create Multiple User Accounts in Linux
Here are the steps to create multiple user accounts in Linux. For this purpose, you need to create a file with user information, mentioned in the following format.
pw_name:pw_passwd:pw_uid:pw_gid:pw_gecos:pw_dir:pw_shell
- pw_name: username
- pw_passwd: user’s password
- pw_uid: user’s ID
- pw_gid: user’s group ID
- pw_gecos: defines comments sections.
- pw_dir: defines the home directory of the user.
- pw_shell: defines user’s default shell.
For example, let us create a simple file with user information.
Add the following information for a couple of users.
test:213254lost:1002:1002:Test Admin:/home/test:/bin/bash test2:@!#@%$Most:1003:1003:Test 2:/home/test2:/bin/bash
Save and close the file. Modify its permissions so that it is secure. This is because it contains plain passwords for your user accounts.
Run the newuser command to read the list of users from users.txt file and create new users according to the information.
It is important to understand how the above command works. newuser will read the input file, create or update specified accounts, and then write these changes to their group or user databases. If there are any errors in the process, no changes are made to the database.
If the above command is successful, you can check /etc/passwd and /etc/groups for changes.
$ cat /etc/passwd | grep -E "test"
For more information about this command, you can check its man documentation.
In this article, we have learnt how to create multiple user accounts in Linux. You can use it to easily add multiple users, especially in an IT environment. It is much better than writing a shell script to loop through the list of usernames and passwords, and calling useradd or passwd command. This is because calling useradd only allows you to add users but not set their passwords, while using passwd allows requires you to interactively enter passwords for each user.