The useradd command in Linux: create new step by step users

Introduction

In any Linux system, user management is a key task for maintaining security and order. The commanduseraddallows to create user accounts quickly and flexibly from the command line. In this article we will see your syntax, the most useful options and practical examples for you to manage users as a professional.

Basic useradd syntax

The simplest way to useuseraddis:

sudo useradd nombre_de_usuario

This command creates a user with default values defined in/etc/default/useraddand/etc/login.defs. However, we rarely conform to the default values; therefore it is important to know the options that allow us to customize.

Most common options

  • -c «commentary»: Add a full description or name of the user (GECOS field).
  • -d directory _ home: Specifies the home directory of the user (default/home/usuario).
  • - s shell: Define the login shell (e.g./bin/bash).
  • -G group 1, group 2: Add the user to additional groups separated by commas.
  • -u UID: Assign a specific user ID (useful to avoid collisions).
  • -m: Create the home directory if it does not exist (by default it is omitted in some distributions).
  • -r: Create a system user (low UID, no home directory by default).
  • -e YYYY-MM-DD: Set the expiry date of the account.
  • - f days: Number of days after the password expires before the account is disabled.

Practical examples

1. Create a standard user with bash and home directory

sudo useradd -m -s /bin/bash juanp

This command creates the userjuanp, assigns him/bin/bashas shell and create your home directory in/home/juanp.

2. User with full name and group membership

sudo useradd -c "María López" -m -s /bin/bash -G sudo,dev marial

Here it is createdmarialwith the commentary «María López», is given access tosudoand the groupdevfor specific permits.

3. System user for a service

sudo useradd -r -s /usr/sbin/nologin servicio_web

The userservicio_webis created as a system account (low UID), without login shell (/sbin/nologin) and without home directory, ideal for running demons.

4. Set expiry date

sudo useradd -e 2025-12-31 temporal

The accounttemporalshall be automatically deactivated after 31 December 2025.

Good practices when using useradd

  • It always precedes the command withsudoIf you're not root.
  • Verifies that the UID and GID do not conflict with existing users by reviewing/etc/passwdand/etc/group.
  • After creating the user, set a password withpasswd nombre_usuarioor forces your change in the first login usingchage -d 0 nombre_usuario.
  • Documents the purpose of each account in a wiki or inventory file to facilitate audits.
  • Use specific groups to assign permissions (e.g.devops, dba) instead of grantingsudoIndiscriminate.

Conclusion

The commanduseraddis an essential tool for any Linux system manager. Dominating your options allows you to create user accounts tailored to the security and functionality needs of your environment. Practice with the examples shown, adapt the parameters to your case and always keep a clear record of the accounts you create.

This work is under aCreative Commons License Attribution 4.0 International for Francesc Roig francesc @ vivaldi.net.

EnglishenEnglishEnglish