How do I create a user under Linux?

how to create useradd linux user

Creating a new user under Linux is one of the basic actions you need to know how to perform as a system administrator.

Linux presents itself as a multi-user operating system, where several people can connect, but also where a service can benefit from its own user. So, for a variety of reasons, including security, you need to know how to create one or more user accounts on Linux (Debian, Ubuntu, Fedora, AlmaLinux, etc.).

In this tutorial, we will learn how to create a user under Linux using the command useradda command for creating users.

1. Understanding the useradd command

How to order useradd is a command-line tool used to create new users on Linux and Unix. This command requires elevated privileges on the machine, either as root or with delegation via sudo.

Here's how to create a new user named "tutomaker":

sudo useradd tutomaker

This simple example creates a user with the default parameters defined by the system. The user's default configuration is obtained by reading the parameters specified at command level (none here) and the values defined in the configuration files /etc/default/useradd and /etc/login.defs.

Once created, the new user is registered in several files that are important for the system: /etc/passwd, /etc/shadow, /etc/group and /etc/gshadow.

To check that the user has been created, you can use the following command:

sudo id tutomaker

This command will display information such as the UID (User ID) and GID (Group ID) of the user.

At present, this user has no password. It is therefore impossible to connect to this account. To set a password, call the command passwd followed by the user's name.

sudo passwd tutomaker

The password must be entered once, then confirmed a second time.

2. Creating a user with a home directory

By default, the useradd does not create a home directory for the new user. However, it is generally advisable to do so for each user, as this directory serves as the user's home directory (depending on how the newly created user is used).

You can delete the user :

sudo userdel tutomaker

Then recreate it, this time specifying that we wish to create a personal folder:

sudo useradd -m tutomaker

The -m (or --create-home) ensures the creation of a personal directory, corresponding to the /home/tutomaker. This directory will contain initialization files copied from the /etc/skelThe latter serves as a skeleton for user directories.

To check the contents of this home directory, you can use the following command:

sudo ls -al /home/tutomaker/

Users have their own space to store their files.

3. Creating a user with customized options

How to order useradd allows you to customize user creation by specifying various parameters at command level. We can define a specific location for the home directory, force the UID, or define the login shell.

  • Exemple de création avec un répertoire personnalisé :
sudo useradd -m -d /opt/tutomaker tutomaker

This command creates a user with a home directory located at /opt/tutomaker instead of /home/tutomaker. This custom directory is specified with the option -d.

  • Exemple de création avec un UID spécifique :
sudo useradd -u 2000 tutomaker

Here, the -u specifies that the user's UID will be 2000instead of letting the system automatically assign the next available UID.

So, if the id is called again, we obtain the following:

uid=2000(tutomaker) gid=2000(tutomaker) groups=2000(tutomaker)
  • Exemple de création avec un shell spécifique :
sudo useradd -s /usr/bin/zsh tutomaker

The -s is used to define the user's login shell, in this case /usr/bin/zsh instead of the default shell. In other words, if Bash is used by default, it will be set aside for this user in favor of another shell (which must first be installed on the system).

  • Exemple de création avec un commentaire

It may be useful to add additional information such as the user's full name or contact details. This is made possible by the -c which allows you to write a comment associated with this user. This value will be visible in the /etc/passwd.

We're going to create the user " tutomaker "by associating the comment " tutobox.fr demo user" . Here is the command:

sudo useradd -c "Demo user tutobox.fr" tutomaker

Reading the file /etc/passwd shows the presence of this comment:

cat /etc/passwd

The last line of the file (since this is the last user created) contains the user's name, comment and other information such as home directory and shell.

tutomaker:x:1001:1002:Demo user tutobox.fr:/home/tutomaker:/bin/sh

4. Conclusion

After reading this article from the Computer Tutorials box, you'll have mastered the basics of creating users under Linux using the command useradd.

If you work with Linux machines, you're bound to have to create a new user at some point... Now you know how to do it, by playing with the options.

To go further, you can consider modifying the default settings present in the " /etc/default/useradd" .

You may also like...

Leave a Reply

Your email address will not be published. Required fields are marked *