Managing User Accounts

12 min read·Jan 1, 2025

In Unix-like operating systems, a user represents an individual who can log in to the system, run processes, access files, and perform various tasks based on their permissions.

Each user has a unique username, a unique identifier, and a unique home directory where they can store personal files and configurations.

A brief word on sudo

The sudo command (short for "superuser do") is a command that allows a permitted user to execute a command as the superuser known as "root", which is a special user account with unrestricted access to all files and commands on the system.

As most of the commands presented in this lesson are designed to modify the system's configuration — which is a sensitive operation — they can only be executed with superuser privileges using the sudo command as follows:

$ sudo command

Where command is the restricted command you want to execute.

When executed, you will be prompted to enter your password, which should grant you the right to run the specified command:

$ sudo command
Password:

Creating users in interactive mode

To add a new user to the system in interactive mode, you can use the adduser command as follows:

$ sudo adduser username

Where username is the unique name of the user you want to create.

By default, this command will:

  1. Create a new user with the specified username.
  2. Create a new group with the same name as the username and add the user to this group.
  3. Create a home directory for the user located at /home/username.

When executed, this command will prompt you to type and confirm the password of the new user:

New password:
Retype new password:

Note: For security reasons, the passwords won't be displayed in the terminal window as you type.

You will then be prompted to type additional information such as the user's full name or phone number:

Enter the new value, or press ENTER for the default
	Full Name []:
	Room Number []:
	Work Phone []:
	Home Phone []:
	Other []:

Note: These fields are optional and can be skipped simply by pressing the ENTER key.

To this final question, you can either press the ENTER key to create the user with the specified information or type n (short for no) followed by ENTER to start the form from the beginning:

Is the information correct? [Y/n]

Creating users in non-interactive mode

To create a new user in non-interactive mode, which is usually more suitable for scripting purposes, you can use the useradd command as follows:

$ sudo useradd username

By default, this command will:

  1. Create a new user with the specified username.
  2. Create a new group with the same name as the username and add the user to this group.

Note: Unlike the adduser command, the useradd won't automatically create a home directory nor set a password for the specified user.

Creating a user with a home directory

To create a new user with a home directory located at /home/username, you can use the useradd command with the -m flag (short for --create-home) as follows:

$ sudo useradd -m username

Listing existing users

In Unix-like operating systems, every user on the system is listed in the /etc/passwd file, which is a text-database that stores essential information about user accounts such as their username, groups, home directory, and more.

To display this file, you can use the cat command as follows:

$ cat /etc/passwd

Alternatively, to verify the existence of a single user on the system, you can use the id command as follows:

$ id username

The /etc/passwd file format

Each line of the /etc/passwd file defines a user account and consists of 7 fields separated by a colon character (:):

username:password:uid:gid:description:home:shell

Where:

  • username is the name of the user account.
  • password is the password of the user account, which is usually displayed as an x for security reasons. Note that the actual password is stored in the /etc/shadow file in its encrypted form.
  • uid or user ID is a unique numerical identifier assigned to each user.
  • gid or group ID is the numerical identifier of the user's primary group.
  • description is an optional string providing additional information about the user like their full name or contact information.
  • home is the path to the user's home directory.
  • shell is the path to the user's login shell binary.

Example

For example, let's consider the following entry:

johndoe:x:1000:1001::/home/johndoe:/bin/bash

Where:

  • jonhdoe is the name of the user account.
  • x is the password placeholder.
  • 1000 is the user's ID.
  • 1001 is the user's primary group ID.
  • /home/johndoe is the path to the user's home directory.
  • /bin/bash is the user's default login shell.

Filtering the entries of the /etc/passwd file

To fetch the account information of a specific user, you can filter the lines of the /etc/passwd file using the grep command as follows:

$ cat /etc/passwd | grep username

Example

For example, this command will output the line of the /etc/passwd file containing the "johndoe" keyword:

$ cat /etc/passwd | grep "johndoe"
johndoe:x:1000:1001::/home/johndoe:/bin/bash

Changing the password of a user

To change the password of a user on the system, you can use the passwd command as follows:

$ sudo passwd username

Where username is the name of the user you want to change the password of.

When executed, this command will prompt you to type and confirm the password of the user:

New password:
Retype new password:

Changing the login shell of a user

By default:

  • The adduser command sets the login shell of the newly created user to the value of the DSHELL variable specified in the /etc/adduser.conf configuration file.
  • The useradd command sets the login shell of the newly created user to the value of the SHELL variable specified in the /etc/default/useradd configuration file.

To change the login shell of a user, you can use the chsh command with the -s flag (short for --shell) as follows:

$ sudo chsh -s shell username

Where:

  • shell is the path to the shell's binary.
  • username is the name of the user you want to change the shell of.

Note: Shell binaries are usually located in the /bin directory.

Example

This command will change the login shell of the johndoe user to Bash, whose binary is located at /bin/bash:

$ sudo chsh -s /bin/bash johndoe

Removing a user

To remove a user account from the system, you can use the userdel command as follows:

$ sudo userdel username

By default, this command will remove the user from the /etc/passwd file, the /etc/shadow file, the /etc/group file, and from any additional groups.

Removing a user's home directory

To also remove a user's home directory, which contains all of its personal files, you can use the userdel command with the -r flag (short for --remove) as follows:

$ sudo userdel -r username

Note: The files located in other system directories will have to be searched for and deleted manually.

Removing a user's personal files

To remove all the files belonging to a specific user on the system, you can use the deluser command with the --remove-all-files flag as follows:

$ sudo deluser --remove-all-files username

Alternatively, if the deluser command is not available, you can combine the find command with the rm command as follows:

$ sudo find / -user username -exec rm -rf {} \;

Where:

  • find / will search recursively in every directory starting from the root directory.
  • -user is used to specify the name of the user the files must belong to.
  • -exec is used to execute a command on every file matched by the find command.
  • {} is used as a placeholder argument for the specified command; in this case rm -rf.
  • \; is used as a command delimiter.

⚠️ WARNING: This command should be used with extreme caution as it will immediately and irrevocably erase all the files and directories matched by the find command from the system.

Summary

Here's a summary of what you've learned in this lesson:

  • The sudo command allows you to run system-sensitive commands as the root user.
  • The adduser command is used to create users in interactive mode.
  • The useradd command is used to create users in non-interactive mode.
  • The useradd -m command is used to create users with a home directory located at /home/username.
  • The passwd command is used to change the password of a user.
  • The chsh -s command is used to change the login shell of a user.
  • The userdel command is used to delete a user from the system.
  • The userdel -r command is used to delete a user and its home directory.
  • The /etc/passwd file stores user accounts information like username, groups, home directory paths, and more.
  • The /etc/shadow file stores user accounts encrypted passwords.

Enjoying the courses?

I've made these courses completely free so anyone can learn from them. If they've helped you and you'd like to actively support the work behind BackendBrewery, you can leave a tip:

Support BackendBrewery
Managing User Accounts in Bash | Backend Brewery