User account creation is one of the basic task for Linux administrator.
We can easily achieve this by running the useradd command.
But what is the solution if you would like to create the bulk users in Linux?
Is there any ways to perform this action?
Yes, we have option in Linux to perform this action. There are two ways we can achieve this as per my knowledge.
We are going to explain these in this article.
Everyone knows the user information was residing in /etc/passwd
file.
It’s a text file that contains the essential information about each user.
When we create a new user, the new user details will be appended into this file.
While creating a new users the below four files will be modified.
/etc/passwd:
User details will be updated in this file./etc/shadow:
User password info will be updated in this file./etc/group:
Group details will be updated of the new user in this file./etc/gshadow:
Group password info will be updated of the new user in the file.
It can be done through the below two commands.
useradd:
Create a new user or update default new user information using shell script.newusers:
update and create new users in batch.
If you would like to read the related articles about user’s then you can navigate to the following articles.
- How to Update/Change Users Password in Linux Using Different Ways
- How To Force User To Change Password On Next Login In Linux
- How to Reset/Change User’s Password in Linux Using passwd Command?
- How To Check Password Complexity/Strength And Score In Linux?
- 6 Easy Ways to Check User Name And Other Information in Linux
- 3 Methods to List All The Users in Linux System
- Four Methods To Add A User To Group In Linux
Method-1: How To Create Bulk User Accounts In Linux Using newusers Command?
The newusers command reads a given file and uses this information to update a set of existing users or to create new users.
Each line is in the same format as the standard password file.
Common syntax for bulk users creation.
# newusers [File_Name]
Before performing this, we need to get the latest GID and UID from the passwd file.
# tail -5 /etc/passwd gdm:x:121:125:Gnome Display Manager:/var/lib/gdm3:/bin/false daygeek:x:1000:1000:daygeek,,,:/home/daygeek:/bin/bash sshd:x:122:65534::/run/sshd:/usr/sbin/nologin thanu:x:1001:1001::/home/thanu:/bin/sh renu:x:1002:1002:Renu,,9600106327,:/home/renu:/bin/bash
Create a file called user-add.txt
and the details should be in the following format. Each user line should be in the separate line.
User_Name:Password:UID:GID:Comments:User_Home_Directory:Users_Shell_Name
I have added the five users in the file. List out the users using cat command.
# cat user-list.txt 2gadmin:2gadmin123:1003:1003::/home/2gadmin:/bin/bash testuser:testuser123:1004:1004::/home/testuser:/bin/bash demouser:demouser123:1005:1005::/home/demouser:/bin/bash sudha:sudha123:1006:1006::/home/sudha:/bin/bash suresh:suresh123:1007:1007::/home/suresh:/bin/bash
Run the newusers
command with the filename to create bulk users in Linux system.
# newusers user-list.txt
We can double confirm this by running the following command.
# grep "2gadmin\|testuser\|demouser\|sudha\|suresh" /etc/passwd 2gadmin:x:1003:1003::/home/2gadmin:/bin/bash testuser:x:1004:1004::/home/testuser:/bin/bash demouser:x:1005:1005::/home/demouser:/bin/bash sudha:x:1006:1006::/home/sudha:/bin/bash suresh:x:1007:1007::/home/suresh:/bin/bash
Method-2: How To Create Bulk User Accounts In Linux Using useradd Command With Shell Script?
Use the below script if you would like to create a multiple users in Linux system.
I have added below three users in the file. Use any file command to List out the users which was there in the file.
# cat user-list1.txt user1 user2 user3
Create a following small shell script to achieve this.
# vi user-add.sh #!/bin/sh for user in `more user-list1.txt` do echo "$user" useradd $user echo "$user@123" | passwd --stdin "$user" chage -d 0 $user done
Set an executable permission to user-add.sh
file.
# chmod +x user-add.sh
Finally run the script to achieve this.
# sh user-add.sh user1 Changing password for user user1. passwd: all authentication tokens updated successfully. user2 Changing password for user user2. passwd: all authentication tokens updated successfully. user3 Changing password for user user3. passwd: all authentication tokens updated successfully.
We can double confirm this by running the following command.
# grep "user1\|user2\|user3" /etc/passwd user1:x:1008:1008::/home/user1:/bin/sh user2:x:1009:1009::/home/user2:/bin/sh user3:x:1010:1010::/home/user3:/bin/sh