Creating and Managing User Accounts in Linux

Written by: Emmanuel Oyibo   |   Last updated: May 13, 2024

User accounts are essential for maintaining a secure and organized system. They dictate who can access your computer and what actions they’re allowed to perform.

Linux excels at providing precise control over user management, allowing you to create, customize, and remove users as needed.

Understanding user accounts is crucial as it’ll help you manage system users (which run background processes) and regular users (for everyday logins by people).

In this article, you’ll learn how to create and manage user accounts on Linux machines.

Creating New Linux Users

In Linux, the process of adding new users is remarkably straightforward. Let’s introduce the core commands and the concept behind them.

The useradd Command

useradd lies at the heart of user creation. Its basic structure is as follows:

$ useradd [options] <username>

Let’s take a quick look at some common options you can use with useradd:

  • -c "Comment": adds a descriptive comment about the user
  • -d /home/newuser: set the specified user’s home directory
  • m: creates the home directory if it doesn’t already exist
  • -s /bin/bash: specifies the default login shell

For example, let’s create a user named jane with a comment and home directory:

$ useradd -m -d /home/jane -c "Jane Doe" jane

The adduser Command (Debian/Ubuntu)

adduser provides a more interactive experience for user creation, as it guides you through the process. The syntax is as follows:

$ adduser [options] <username>

For a better understanding of the adduser command, see our detailed guide, adduser command in Linux.

Setting Password with passwd

After creating the user, you can set a password using the passwd command:

$ passwd <username>

However, we recommend using a strong password, as security is very vital in a Linux system.

Modifying Linux User Accounts

After user creation, it’s common to need to adjust their settings or information. Linux provides powerful tools for modifying existing user accounts. Let’s explore some of them.

The usermod Command

usermod is your go-to tool for making changes to existing users. Let’s take a look at its basic structure:

$ usermod [options] <username>

Here are some key options you can use with usermod:

  • -d /new/home: changes the user’s home directory
  • -l new_username: renames the user’s login name
  • -s /bin/zsh: changes the user’s default shell
  • -L: locks a user’s account (disable logins)
  • -U: unlocks a previously locked account

For example, to change the login name of john to jdoe and update his home directory:

$ usermod -l jdoe -d /home/jdoe john

The chage Command

chage is essential for specifically managing password expiration and aging. The basic syntax for this command:

$ chage [options] <username>

Now, let’s take a look at some common options for the chage command:

  • -M: sets the maximum number of days between password changes
  • -m: sets the minimum number of days between password changes
  • -w: sets the number of days before password expiry that the user receives a warning
  • -d: sets the last date since the password was changed (often used with a date format like YYY-MM-DD)
  • -E: sets the date the account will expire (use a date format or -1 to indicate no expiration)
  • -l: lists the current password aging settings for a user

For example, let’s set jdoe to change his password every 60 days, with a minimum of 10 days between changes and a 5-day warning period:

$ chage -M 60 -m 10 -w 5 jdoe

Deleting Linux Users

When a user account is no longer needed, you can remove it from your Linux system. However, always exercise caution when deleting users, as it can result in data loss.

The userdel Command

The foundation of user deletion is the userdel command. Its basic syntax is:

$ userdel [options] <username>

Now, let’s explore some common options used with userdel:

  • -r: removes the user’s home directory and their mail spool
  • -f: forces the deletion of the user account, even if they’re currently logged in or having running processes
  • -Z: removes any SELinux user mapping for the deleted user (for systems using SELinux)

For example, to delete the user jane, her home directory, and any SELinux mappings:

$ userdel -rfZ jane

The deluser Command (Debian/Ubuntu)

On Debian/Ubuntu-based systems, deluser offers more granular control over the deletion process. Here’s its basic syntax:

$ deluser [options] <username>

Also, you can use deluser --group <username> to remove a user from their primary group.

You can read up more about deluser in our other article, how to remove user on Ubuntu from terminal.

Managing Linux Groups

Groups in Linux provide an efficient way to organize users and manage permissions. By assigning users to groups, you can control access to files and resources collectively.

Adding Users to Groups

There are many ways to add users to groups in Linux. The usermod command with the -aG option allows you to add a user to one or more supplementary groups (groups in addition to their primary group).

Let’s take a look at the basic syntax:

$ usermod -aG <group1>,<group2>,... <username>

For example, let’s add the user jane to the groups, developers and webadmin:

$ usermod -aG developers,webadmin jane

To learn about other ways to add users to groups, see our other tutorial, how to add users to groups in Linux.

Monitoring User Activity

Linux offers several tools to keep track of who is logged into your system and what they’re doing. This is useful for system administration and security purposes.

Now, let’s briefly explore some of these tools:

  • who: This command provides a quick snapshot of currently logged-in users, their login time, and where they’re connected from.
  • w: This command offers more detailed information than who, including the processes each user is running.
  • last: This command displays a historical log of user logins and logouts. This can help track system usage patterns over time.

About The Author

Emmanuel Oyibo

Emmanuel Oyibo

Emmanuel is a Technical Writer and DevOps/Software Engineer with over 4 years of experience in streamlining technical documentation and automating workflows. He specializes in writing about Linux, Python, and DevOps concepts, utilizing his expertise to create clear guides and documentation. Emmanuel holds a B.Sc in Biochemistry and is passionate about the power of technology for problem-solving.



Please add comments below to provide the author your ideas, appreciation and feedback.

Leave a Reply

Leave a Comment