- Three methods to create User accounts in Linux?
- How to create an user accounts in Linux using useradd command?
- How to create an user accounts in Linux using adduser command?
- How to create bulk user accounts in Linux using newusers command?
- How to create bulk user accounts in Linux using useradd command with Shell script?
- How to Create Normal User in Linux
- Tutorial Details
- Basic information you must know
- Creating a Normal User
- Set Password to New User
- Assign New User to Group
- Creating a Normal User with a Different Home Directory
- Creating a New User with a Specific User ID
- Creating a New User with a Specific Group ID
- Creating a New User without a Home Directory
- Creating a New User with an Account Expiration Date
- Creating a New User with a Password Expiry Date
- Creating a New User with Custom Comments
- Creating New User with a Different Login Shell
- Removing a New User Account
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.
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.
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 "[email protected]" | passwd --stdin "$user" chage -d 0 $user done
Set an executable permission to user-add.sh file.
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
How to Create Normal User in Linux
In Linux, we have a root user created by the system administrator and normal users, which are also created by the system administrator.
However, root user have more privileges than normal user accounts, which is why we suggest letting other people have normal user accounts instead of directly having a root account.
It will restrict them from doing a lot of miscellaneous and unintentional damage to the system, and you can also create users with an expiry date, limited permissions, and many more.
Also remember that only the root user or a user with sudo privileges has access to create a new normal user account in Linux.
Tutorial Details
Description | Creating Normal Users |
Difficulty Level | Moderate |
Root or Sudo Privileges | Yes |
Host System and Architecture | Ubuntu 22.10 (x64) |
OS Compatibility | Ubuntu, Manjaro, Fedora, etc. |
Prerequisites | useradd, passwd |
Internet Required | No |
Discussed Tools in this Article |
Basic information you must know
The useradd and adduser commands are used to create users in a Linux system.
The useradd command is a compiled binary and available in all Linux distributions; however, adduser is a Perl script that utilizes the useradd command to provide more rich features.
Creating a new account will populate the /etc/passwd , /etc/shadow , /etc/group , and /etc/gshadow files.
Creating a Normal User
Specify the username in the following command to create a normal user account.
$ sudo useradd [USERNAME]
Set Password to New User
A newly created user has an empty password that can be set using the passwd command.
$ sudo passwd [USERNAME] New password: Retype new password:
Assign New User to Group
After creating a user, if you want to assign that user to a group like sudo, specify the name of that group and username as shown.
$ sudo usermod -aG sudo [USERNAME]
Creating a Normal User with a Different Home Directory
Create a new directory at the “/home/” location and specify it to the following command to use it as the default home directory for your new user.
$ sudo useradd -d /home/[DIRECTORY-NAME] [USERNAME]
Creating a New User with a Specific User ID
You can specify a user ID for your newly created user in the range of 100 to 999, as shown.
$ sudo useradd -u 666 [USERNAME]
Creating a New User with a Specific Group ID
If you want to add your current user ID as group to the new user account than find out its ID by reading the “/etc/passwd” file and specify it to the following command.
$ sudo useradd -g [CURRENT-USER] [USERNAME]
Creating a New User without a Home Directory
Assign the -M flag to create a new user without a home directory, but in the next login, the root home directory or previous user’s home directory will be used as the default home directory for that user.
$ sudo useradd -M [USERNAME]
Creating a New User with an Account Expiration Date
To remove the new user account at a specified date, specify that date to the following command with username and the -e flag.
$ sudo useradd -e 2023-12-26 [USERNAME]
Creating a New User with a Password Expiry Date
The same password for the long run can be easily breached, so set the password expiry days after which users will be forced to change their password on that specific day.
$ sudo useradd -f [NUMBER-OF-DAYS] [USERNAME]
If you specify “5” to [NUMBER-OF-DAYS], you will be forced to change your password after 5 days.
Creating a New User with Custom Comments
Comments are helpful to let you know the purpose of that user account or a few details related to that account, which can be easily set using the -c flag.
$ sudo useradd -c "This is my comment" [USERNAME]
Creating New User with a Different Login Shell
By default, the bash shell is used by most Linux distributions, which will be the default shell for a new user, although you can easily install another shell like ZSH or Fish and specify this shell as the default for the new user account.
$ sudo useradd -s /bin/zsh [USERNAME]
Execute the echo $0 command to find the path of the current shell.
Removing a New User Account
In case you accidentally created a new user without any intention to use it further, you can remove it using the following command:
$ sudo userdel [USERNAME]
That’s all the possible ways to create a new user in Linux; if you have more, feel free to share them in the comment section.