How to create new users on linux from the command line

In this manual, we will deal with the following topics:

  • How to create a new user in GNU/Linux operating systems from the command line.
  • How to assign a password to the new user.
  • How to assign or change a 'home' directory to the new user.
  • How to delete a user in GNU/Linux operating systems from the command line.

Below you will find the steps for each of the mentioned topics.

How to create a new user in GNU/Linux operating systems

In the first place, it will be an indispensable requirement to have SSH/terminal access to an account with superuser permissions. For example, the "root" user account.

Next, we will use the command useradd to create the user. The syntax is the following:

useradd -opciones nombredeusuario 

For the purposes of this manual, we will only consider the "-m" option that allows us to specify whether or not we want to create a "Home" directory with the same name as the user we created.

Examples:

Create a user with home directory: useradd -m nuevousuario

Create a user without home directory: useradd nuevousuario

How to assign a password to the new user

Changing the password of the new user (or any other user of the system) is as easy as executing the following command as superuser.

passwd nombredeusuario

Next we will be asked to enter the password and confirm with the "ENTER" key. Then we will be asked to repeat it for security reasons.

Important: during the process of password assignment, the keyboard will not seem to work, however what happens is that the characters entered are invisible. You should type the password normally, being aware that you simply cannot see what is being written.

How to assign or change a home directory to the new user

If during the process of creating the new user we have not specified a home directory (or if we want to modify the directory of an existing user), we can use the command "usermod".

The syntax is the following:

usermod -m -d /home/directoriodestino/ nombredeusuario

The "-d" option allows you to specify the path of a non-existent directory. It is important that this directory does not exist previously.

The option "-m" allows to indicate that all the content must be moved to the new directory. Useful in the case of an existing user.

How ​​to delete a user

If we want to delete a user we will do it with an account with superuser permissions. For example, the "root" user account.

We will use the userdel command with the following syntax:

userdel -options username

Most of the time, the -r option is used to delete the main directory and the user's mail queue, leaving the command as follows:

userdel -r username

For example we are going to create a user called "usertest" with its own home, then we will assign a password (which will not be seen on the screen for security reasons of the operating system itself), later we will change that user and see what is the directory in the one we entered when making the change. Finally we are going to list the user's directory to see with what permissions it has been created:

enter image description here

Your privacy is important to us
We use our own cookies for the proper functioning of the site. In addition, third-party cookies are used for analytical purposes only. This information is not associated with any person so that no personally identifiable data is stored, but is only information that is collected to identify the session, with the aim of facilitating the analysis of the website. You can change your preferences at any time by logging back into this website. For more information about our cookie policy you can visit the Cookie information link. Click the "Accept" button or continue browsing to give us your consent to the mentioned cookies or access more detailed information at Manage cookies.
i