Three methods to create User accounts in Linux?

User account creation is one of the basic task for Linux administrator that everyone aware.

Here we are going to show you something interesting about the user creation in this article.It will help you to complete your task more quick and efficiently

We need to perform this task whenever a new employee is joining in our organization.But you don’t need to create an account for the new employee if the Linux system is integrated with AD for single sign on (SSO).

However, you need to assign the user in the corresponding local group based on his role and department.

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 three commands.

  • useradd: Create a new user or update default new user information.
  • adduser: Create a new user with all default parameters or update default new user information.
  • newusers: update and create new users in batch.

If you would like to read the related articles about user management then you can navigate to the following articles.

How to create an user accounts in Linux using useradd command?

The useradd command creates a new user account using the default values which was specified on the useradd -D command output.

The useradd command will update system files and also create the new user´s home directory and copy initial files.

# useradd thanu

If you create a new account, you should set the password to that account. Use the passwd command to set password.

# echo "new_password" | passwd --stdin thanu
Changing password for user thanu.
passwd: all authentication tokens updated successfully.

How to create an user accounts in Linux using adduser command?

adduser is not a standard Linux command. It creates a new user with all default parameters which is not available in the useradd command. However, it uses useradd command in the background to perform this task.

Also, it adds a password for the user while creating.

# adduser renu
Adding user `renu' ...
Adding new group `renu' (1002) ...
Adding new user `renu' (1002) with group `renu' ...
Creating home directory `/home/renu' ...
Copying files from `/etc/skel' ...
Enter new UNIX password: 
Retype new UNIX password: 
passwd: password updated successfully
Changing the user information for renu
Enter the new value, or press ENTER for the default
	Full Name []: Renu
	Room Number []: 
	Work Phone []: 9600106327
	Home Phone []: 
	Other []: 
Is the information correct? [Y/n] Y

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.

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

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 isthere in the file.

# cat user-list1.txt
user1
user2
user3

Create the following 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

About Magesh Maruthamuthu

Love to play with all Linux distribution

View all posts by Magesh Maruthamuthu

2 Comments on “Three methods to create User accounts in Linux?”

  1. I’m curious to find out what blog platform you have been utilizing?
    I’m experiencing some small security issues with my latest website and
    I would like to find something more safe. Do you have any solutions?

  2. I do agree with all of the concepts you have presented for your post.
    They’re really convincing and will certainly work.
    Nonetheless, the posts are too brief for beginners. May just you
    please extend them a bit from next time? Thanks for the post.

Leave a Reply

Your email address will not be published. Required fields are marked *