For a Linux-based OS, it usually involves creating user accounts, modifying existing accounts like changing their home directory, default shell, locking/unlocking one or more accounts, and removing user accounts. Before we investigate the commands and processes to carry out these tasks, let’s delve briefly into how user accounts can be classified in Linux. Also, note that unless explicitly specified, given commands will work in most of the common Linux distributions.

User Types

Root User

The root user is the administrator of OS with all permissions to perform operations. Usually, only root can install/uninstall or update basic system programs and libraries. It is the only user account with system-wide privileges. So, the root user is the most powerful user of the system.

Special User

These are the users without logins. They don’t have all the privileges of the root user. Depending on the account, they assume different specialized roles. These are created automatically at the time of any application installation. bin, sync, lp, mail, operator, squid are some of the examples of special users.

Common Users

Common users have full privileges only in their working directory, usually their home directory. They don’t have privileges to manage the system or install the software. They can’t perform these tasks without having special privileges via sudo.

Adding User

Debian/Ubuntu

On a Debian or Ubuntu-based system, there are a couple of options to add users from CLI. The first command is adduser, which is a Perl script and uses useradd command in the backend whose usage we’ll see later. Since adding a user is a privileged task, you would need to use sudo as prefix and username as argument. Other details can be specified as prompted. Except for username and password, the rest of the details are optional. We can verify that the user has been created by using id command.

CentOS/RHEL/Fedora (Including Debian/Ubuntu)

The next command, useradd will work across RHEL based OS distributions as well as works equally well on Ubuntu/Debian hosts. The simplest syntax (without any extra options) to create a new user is: Example: The useradd command supports multiple options that can be specified while creating the user, most common being user ID (UID), group ID (GID), default shell and home directory, etc. One such example is given below: You can verify the newly created user using id command:

Modifying User

One often needs to modify some property of existing users based on organization requirements, user requests, or system migrations. Most of these properties are easy to modify though we need to ensure how it’ll affect the user environment and access to files owned or accessed by the user.

Default Shell

The default shell is the CLI shell created when a user launches a new CLI session either locally or via SSH. Most modern systems have a default user Bash though it can vary based on Linux distribution or the user’s environment. To modify the default shell of a user, use: Example: As you can see in the above output, the shell has been changed from /bin/sh to /bin/bash for user janedoe.

Home Directory

Like default shell, a user’s home directory can be modified to a different location using: In the example below, the user home directory of the user janedoe has been changed to /data/janedoe: Before making the switch, ensure that the new directory has the right ownership and permissions. Otherwise, the user may face issues during login or working in the new home directory.

User ID

You can change the user ID of an existing user using: Example: Again, changing UID changes how Linux filesystem maps ownership and permission to a file or directory. Ensure that the user’s home directory and its contents and all other files anywhere in the system, originally owned by the user (with old UID), is changed to UID mapped. Not doing so can cause problems in the CLI session and file access by the user.

Default Group

The default group is usually the user’s default group ID, which gets created during user creation unless another GID is specified. Linux allows you to modify the default group of a user using usermod command as well. Here’s the syntax to use: Here’s one example: Again, make sure the new group ID is set on the user’s home directory, contents, and all other files or directories applicable to properly migrate their ownership permissions.

Adding/Removing Groups

Besides the default group, a user in Linux can be part of secondary groups. We can always add or remove additional groups a user belongs to using usermod command. Example: Similarly, to remove a user from one of the secondary groups, use gpasswd command as shown below: Example:

GECOS Comment

GECOS field in /etc/passwd contains user information or comment. We can modify this information for an existing user as: Example: Please note that if your comment or user details contain spaces, enclose that field in quotes as done in the above example.

Login Name

The user’s login name can also be changed using usermod command by using -l flag: Example: Remember to update user references as per new name wherever used. Even in commands like id, the new username should be specified.

Removing User

A user can be removed from Linux using userdel command. Example: To remove a user along with its home directory and mail spool, add -r flag as well. Specifically for Ubuntu-based systems, you can also user deluser command to remove a user: Similarly, to remove the home directory and mail spool as well, use: For detailed information and other supported options, refer to the main page of various commands using:

Conclusion

This article showed various aspects of user management in a Linux system. This includes an explanation of various categories of users and how to add and remove them. It also covers various options that help to modify the parameters of an existing user. Though it doesn’t cover all possibilities supported by various commands, it covers a lot of common administration tasks that a system administrator will encounter in day-to-day work. You may also be interested in reading: How to remove files and directories in Linux?

How to Add  Modify and Remove User in Linux  - 19How to Add  Modify and Remove User in Linux  - 68How to Add  Modify and Remove User in Linux  - 8How to Add  Modify and Remove User in Linux  - 50How to Add  Modify and Remove User in Linux  - 41How to Add  Modify and Remove User in Linux  - 31How to Add  Modify and Remove User in Linux  - 52How to Add  Modify and Remove User in Linux  - 44How to Add  Modify and Remove User in Linux  - 48How to Add  Modify and Remove User in Linux  - 85How to Add  Modify and Remove User in Linux  - 61How to Add  Modify and Remove User in Linux  - 88How to Add  Modify and Remove User in Linux  - 64How to Add  Modify and Remove User in Linux  - 25How to Add  Modify and Remove User in Linux  - 20How to Add  Modify and Remove User in Linux  - 10How to Add  Modify and Remove User in Linux  - 23How to Add  Modify and Remove User in Linux  - 38How to Add  Modify and Remove User in Linux  - 47How to Add  Modify and Remove User in Linux  - 50How to Add  Modify and Remove User in Linux  - 97How to Add  Modify and Remove User in Linux  - 70How to Add  Modify and Remove User in Linux  - 51