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
Adding a simple user using `useradd` command in Linux
This command will add the user named “test_user”.
sudo useradd test_user

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”.
Creating 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”.
Creating 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”.
Creating 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.
Creating 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.
Creating user with a comment
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.
Creating 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.
Setting 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”.
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.
Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape,
GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out -
check it out now!
Last Updated :
21 Jul, 2023
Like Article
Save Article