This article explains how to add a user in Linux using the useradd command.
The useradd command in Linux is used to create new user accounts on a system. It is a native binary compiled with the system, whereas adduser is a Perl script that utilises useradd in the background. When a new user is added, useradd makes changes to the following files:
- /etc/passwd
- /etc/shadow
- /etc/group
- /etc/gshadow
- Creates a user directory under /home (if specified)
Syntax of the useradd Command
The basic syntax of the useradd command is:
useradd [options] [username]
Let us manage users with the useradd command:
- Adding a New User in Linux
To create a new user, use the following command:sudo useradd test_user
This command creates a new user named test_user but does not set a password or create a home directory by default.
- Adding a User with a Specific Home Directory
To specify a custom home directory, use the -d flag:sudo useradd -d /home/test_user test_user
This sets /home/test_user as the user’s home directory.
- Creating a User with a Specific User ID (UID)
To assign a custom User ID (UID):sudo useradd -u 1234 test_user
This creates test_user with the UID 1234.
- Creating a User with a Specific Group ID (GID)
To assign a Group ID (GID) while creating a user:sudo useradd -g 1000 test_user
This assigns test_user to the group with GID 1000.
- Creating a User Without a Home Directory
To create a user without a home directory, use the -M option:sudo useradd -M test_user
This prevents the creation of /home/test_user.
- Creating a User with an Expiry Date
To set an expiry date for a user account:sudo useradd -e 2025-12-31 test_user
This sets the account expiry date to December 31, 2025.
- Adding a Comment or Description for a User
To include a comment describing the user:sudo useradd -c "This is a test user" test_user
This assigns “This is a test user” as the user description.
- Creating a User with a Custom Login Shell
To set a specific shell for a user:sudo useradd -s /bin/sh test_user
This assigns /bin/sh as the default shell instead of the system default.
- Setting an Unencrypted Password for the User
To assign a password (not recommended due to security concerns):sudo useradd -p test_password test_user
This sets test_password as the user’s password in an unencrypted format. It is advisable to use passwd instead:
sudo passwd test_user
- Displaying Help for useradd
To view available options and syntax:sudo useradd --help
This displays all possible flags and their descriptions.
This way, we can conclude that the useradd command is a powerful tool for creating and managing user accounts in Linux.
For assistance, reach out to our support team.