How to Create or delete Users in Linux – With examples

Linux is a powerful multi-user operating system that enables administrators to manage user accounts efficiently through the command line. Whether you’re managing a server or your own local system, knowing how to create or delete user accounts is essential. This guide will walk you through the process of managing user accounts in Linux with practical examples and pro tips to make the process smoother.

Contents

Creating a New User in Linux

The most common command to create a new user is useradd. This tool adds a user to the system, along with options to customize the user’s environment.

Basic Syntax:

sudo useradd [options] username

Example:

sudo useradd alice

This creates a user named alice, but without a home directory or password. To ensure the user has a home directory and an initial password, you can use the following commands:

  • Create a home directory:
    sudo useradd -m alice
  • Assign a password:
    sudo passwd alice

The -m flag ensures that a home directory is created at /home/alice, and passwd sets the user’s password, prompting you to enter it securely.

Additional Options for useradd:

To customize the user account further, you can use additional flags such as:

  • -d – Specify a different home directory
  • -s – Define the default shell (e.g., /bin/bash)
  • -G – Add the user to supplementary groups

Example with Custom Options:

sudo useradd -m -d /custom/home/alice -s /bin/bash -G sudo alice

This creates the user alice with:

  • A custom home directory at /custom/home/alice
  • The default shell as /bin/bash
  • Membership in the sudo group, giving administrative privileges

Deleting a User in Linux

If a user is no longer needed, you can remove them using the userdel command. This removes the user’s account and login access.

Basic Syntax:

sudo userdel username

Example:

sudo userdel alice

This deletes the user alice but preserves her home directory and files. If you want to completely remove the user and their home directory, use the -r flag:

sudo userdel -r alice

Important Note: Always double-check before using -r as this will permanently delete all the user’s personal files.

Checking User Information

If you want to verify that a user has been added or deleted successfully, you can view the /etc/passwd file, which stores user information.

Command:

cat /etc/passwd | grep username

This allows you to see if the user entry exists. No results mean the user has been successfully deleted.

Other Useful Commands

  • id username – Displays the user’s UID, GID, and groups
  • getent passwd username – Provides user details using the name service switch
  • groups username – Lists all groups the user belongs to

Tips for Managing Users

  • Always assign strong passwords to new users for better security
  • Avoid deleting users immediately—consider locking the account instead using usermod -L username
  • Use group-based permission management to streamline user access to resources

Conclusion

Managing users is a core skill for anyone working with Linux systems. Whether you’re adding a new team member to a project or deprecating inactive accounts, the useradd and userdel commands are your go-to tools. By understanding how to use their various options, you can create customized, secure environments that are both efficient and maintainable.

Familiarity with these commands and best practices not only improves system security but also enhances collaboration and resource management on multi-user systems. Don’t forget to review permissions and groups regularly to ensure your user management strategy stays effective over time.