What is the useradd command in Linux?

The useradd is the command in Linux to create a new user locally on the computer. You can also find the adduser command, but this is a symbolic link of the useradd command.

file 'which adduser'

So you can use either useradd or adduser, both are fine. This command helps to create a user with passing values.

How to get help for the useradd command

I always suggest reading the manual page and help. So, It will give you an in-depth introduction to the command.

To get the manual page type the below command,

man useradd

But, to get the only command options, you can type the below command.

useradd --help

Syntax of the useradd command

Now you know how to get help with the useradd command. So, let’s talk about useradd syntax. The syntax of the useradd command is as below,

Syntax: useradd options user_name

Precondition

  • You must log-in with a root user or a user with sudo privilege to use the useradd command.

Creating a new user

You can create a new user after typing the useradd command followed by the user name.

For example, I’m creating a user with the name amit so I’m using the useradd command along with sudo privilege. If you want to create any other user with a different name, then you have to type useradd and then the user name.

sudo useradd amit
useradd command
Adding a new user with the name amit

The command will not show you any output. It will create the new user and its home directory (/home/amit), and copy files from the /etc/skel directory to the user’s directory. Also, within the home directory, users can read, write, edit, and delete files and directories. The user has full permission in his/her home directory.

At the time when you create any user with the useradd command, the user will be created, but it will be in the lock position, Which means the new user can not log in. If you want to log in with the new user, then you will need to set a password for the new users so that the user can log in. To do it, you have to use the passwd command.

passwd is the command to set the user password and also modify the password.

sudo passed amit
Changing the password for the user amit

It will ask you to enter the password and then it will confirm the password. that’s it.

By default in CentOS, members of the group wheel are granted sudo access.
If you want to give the newly created, administrative level permission, you need to add that user in the wheel group, So below is the syntax for that,

usermod -aG wheel username
usermod -aG wheel amit

** To check a user id, gid, groups name just type id username

useradd-aG-wheel
Adding a user in the wheel group and checking the user information

Creating a new user with a different home location

If you want to create a user with a home directory in a different location, then you have to give the exact path of the directory. I’m going to create a user name ram and location will be /data/ram then the command will look like below way,

sudo useradd -d /data/ram ram

So we have created a user ram in /data/ram. Also, to check the default home directory for the user ram, switch to ram user and type command pwd. And you will get the current directory location. As a result, you can see in the below picture as well.

useradd-d
Adding a new user in the different home directory

Conclusion

In this article, you learned about the useradd command with essential arguments. And you can see that it is not robust. So, I hope, you understand. But if you have any questions, you can ask in the comment section.

Also, you may like to read.

Leave a Reply

Your email address will not be published.