How to add User in Linux | useradd Command
Last Updated :
12 Jul, 2024
useradd is a command in Linux that is used to add user accounts to your system. It is just a symbolic link to adduser command in Linux and the difference between both of them is that useradd is a native binary compiled with the system whereas adduser is a Perl script that uses useradd binary in the background. It makes changes to the following files:
- /etc/passwd
- /etc/shadow
- /etc/group
- /etc/gshadow
- creates a directory for new user in /home
Syntax of `useradd` command in Linux
The basic syntac for the `useradd` command is as follows.
useradd [options] [User_name]
Working with `useradd` Command
1. How to Add a User in Linux
This command will add the user named “test_user”.
sudo useradd test_user

2. How to Add User by Specifying a home directory path for the new user
To give a home directory path for new users, we use the following command in Linux.
sudo useradd -d /home/test_user test_user

This will set the home directory of the us”/home/test_user”.
3. How to Create a User with a Specific User ID (UID)
To create a new user with a custom UID, we use the following command.
sudo useradd -u 1234 test_user

This will create a new user with the user-id “1234” and the name “test_user”.
4. How to Create a User with a Specific Group ID (GID)
To create a new user and assign a specific group ID, use the following command
sudo useradd -g 1000 test_user

This will create a new user with the group id “1000” and the name “test_user”.
5. How to Create a User Without a Home Directory
To create a user without a home directory, we use the following command.
sudo useradd -M test_user

This will create the user with the name “test_user” and that too without a home directory.
6. How to Create User with an Expiry Date
To set an expiry date for a user account, we use the following command.
sudo useradd -e 2020-05-30 test_user

This will create the user named “test_user” with the expiry date of 30th May 2020.
To add a comment or description for a user, we use the following command.
sudo useradd -c "This is a test user" test_user

This will create a user with a short comment or description of the user.
8. How to Create a User with Changed Login Shell
To create a user with a different login shell, we use the following command.
sudo useradd -s /bin/sh test_user

This will create a user named “test_user” with the default shell /bin/sh.
9. How to Set an Unencrypted Password for the User
To set an unencrypted password for the user, we use the following command.
sudo useradd -p test_password test_user

This will create a new user with the name “test_user” and an unencrypted password “test_password”.
10. Displaying Help
To display help, we use the following command.
sudo useradd --help

This command will display the help section of the useradd command.
Conclusion
In this article we have discussed the `useradd` command in Linux which is a powerful tool for creating user accounts with various customizations. Overall, we can say that by understanding its options and syntax, system administrators can efficiently manage user accounts on their Linux system.
How to add User in Linux | useradd Command – FAQs
How to add a user in Linux?
To add a new user in Linux, use the useradd command followed by the username. For example, to add a user named “newuser”:
sudo useradd newuser
After creating the user, set a password for the new user:
sudo passwd newuser
What is the command for adduser in Linux?
The adduser command is a more user-friendly way to add a new user and is often a symbolic link to useradd. To add a user named “newuser”:
sudo adduser newuser
This command will prompt for additional information and set up the user’s home directory and password.
How to set user ID in Linux?
To set a specific user ID (UID) while creating a new user, use the -u option with the useradd command. For example, to create a user “newuser” with UID 1001:
sudo useradd -u 1001 newuser
How to set username in Linux?
To set or change a username in Linux, you generally need to rename the user. Use the usermod command to change the username. For example, to change the username from “olduser” to “newuser”:
sudo usermod -l newuser olduser
Ensure to update the home directory if necessary:
sudo usermod -d /home/newuser -m newuser
How to list users in Linux?
To list all users in Linux, you can view the /etc/passwd file, which contains user account information:
cat /etc/passwd
For a cleaner list of usernames, you can cut the first field (username) from the /etc/passwd file:
cut -d: -f1 /etc/passwd
Alternatively, use the getent command to list users:
getent passwd