The useradd command lets a superuser create a new user account in Linux.
It is a low level utility that doesn’t do a lot of things by default but provides several options to create users with various configuration.
Here’s the syntax of useradd command:
useradd [options] username
Let’s see how to use useradd command.
Useradd command examples
Keep in mind that you need to be root or a sudo user in order to use this command.
Add a new user in Linux with useradd command
You can use the useradd command without any options like this:
It will create the user account but:
- the user’s home directory won’t be created
- the password has to be set separately
- the default shell for the user will be sh
You can also create home directory while creating the user.
Add a user with home directory with useradd command
The -m option of useradd command allows to copy all files from your system skeleton directory (/etc/skel) to the newly created home directory.
In other words, you can create a user with home directory using the -m option as well.
useradd -m new_username
You can also specify an existing directory as the home directory of the newly created user with option -d.
useradd -d Path_to_Existing_Directory new_username
Add a new user with different shell
The default shell for a user created with useradd command is sh. These days sh shell is hardly used when there is bash and zsh.
A user can change his/er default shell but you can also create the user with a different default shell with the option -s.
For example, if you want the new user to use bash as the default shell, here’s what you can do:
useradd -s /bin/bash new_username
Add a new user with different group
Usually, when you create a new user, a group with the same name as the user is created. The new user is added as the member of this group.
With the option -g, you can add a new user to an already existing group as its default group.
useradd -g Existing_Group_Name_or_ID new_username
Suppose you are creating an account for a developer. Adding all the developers to a ‘dev group’ could be a strategy.
You can also add the user to additional (existing) groups with option -G.
useradd -G group_1 group_2 new_username
So if you are creating a sudo user, you can simply add the user to the sudo group while creating it.
Add a new user with specific user ID (UID)
You may also create a new user with a specific user ID with the option -u of useradd command:
useradd -u uid new_username
Bonus Tip: Combine multiple options to create a new user with different parameters
You can combine multiple options together to create a new user in Linux with a predefined configuration.
useradd -d /home/abhishek -s /bin/bash -g my_group
As you can see, the useradd command by default doesn’t add much. This is why some people prefer to use the adduser command. You can read about difference between useradd and adduser, if interested.
I hope you found the useradd command examples useful. You may also want to learn to delete users with userdel command. Questions and suggestions are welcome.