What is User Management in Linux?

Learn via video courses
Topics Covered

In Linux, a user is an account associated with an individual or a system process that allows them to access and interact with the operating system and its resources. Each user account or user has the following,

  • Unique username.
  • A User ID (UID) that serves as a numerical identifier.
  • Users are organized into groups, and each group has a group ID (GID).
  • Home directory.

User management in Linux refers to creating, modifying, and deleting user accounts, ensuring proper access control and security within the system.

The whoami command can identify the currently logged-in user in a Linux system.

More information about a user or group can be found using the id command.

The <username> is optional and if not specified, information about the currently logged-in user is displayed.

showing the output for the above command

The /etc/passwd file is a crucial system file found in Linux and acts as a user database that stores essential information about user accounts registered on the system. Each line in the /etc/passwd file represents a distinct user with the following syntax,

  • password: Encrypted password of the user account. Modern systems often store the actual encrypted password in the /etc/shadow file, which is accessible only by the root user.
  • User Info: Additional information about the user. It is not used by all systems and may be left empty.
  • Home Directory: Absolute path to the user's home directory.
  • Login Shell: Program that runs when the user logs in. It defines the user's command-line environment and behavior.

root

The root user, often referred to as the superuser, holds the authority to perform any action on the system. There are two ways to use the root privileges in Linux,

  • The su command or switch user is used to change the current account to another user account. The following command can be used to switch to the root user account,

root

The hyphen (-) used with su ensures that the new user environment is loaded with the target user's login environment variables. After executing this command, the user is prompted to enter the password of the root user account. If the correct password is provided, the terminal session is transformed into the root shell environment.

  • The sudo command is a powerful tool used to execute commands with elevated privileges temporarily. This enables authorized users to perform specific administrative tasks without having to log in as the superuser (root) entirely.

Let us consider the example where we have to see the files of the root folder and the normal user has no privileges for this action and we use sudo to achieve this.

superuser

useradd

The useradd command of user management in linux is used to create a new user account on the system.

Syntax:

  • [options]: Optional flags that modify the behavior of the command.
  • username: The desired username for the new user account.

Example:

Let's create a new user named "bob" with the useradd command:

command with the username

In case of successful user creation, a new user with the default configurations will be created. We can verify this by using the id command with the username.

If the user bob exists, the command will show the details associated with the user.

/etc/default/useradd

The /etc/default/useradd file is a configuration file used to set default options and parameters for the useradd command. When a new user account is created using the useradd command, the settings specified in the /etc/default/useradd file are applied if they exist, providing a consistent and predefined configuration for user accounts across the system.

The /etc/default/useradd file may contain various parameters that can be customized such as,

  • HOME: Default base directory for new user home directories.
  • SHELL: Default login shell for new user accounts.
  • SKEL: Directory to be copied into a new user's home directory during account creation. The default value is /etc/skel/.
  • CREATE_MAIL_SPOOL: Creates a mailbox for the new user in the /var/mail/ directory if set to yes.

userdel

The userdel command of user management in linux is used to delete user accounts from the system.

Syntax:

Example:

If the user bob is successfully deleted, the command will not display any output.

command will not display any output

usermod

The usermod command of user management in linux is used to modify existing user accounts on the system. Syntax:

Example:

Let's say we want to change the login shell for the user "bob" to /bin/zsh using the usermod command:

modify existing user accounts

In this example, we use the -s option to specify the new login shell (/bin/zsh) for the user bob. The zsh shell is an alternative to the default bash shell. When the user logs in again, the new login shell will take effect.

/etc/skel/

The /etc/skel/ directory serves as a template directory for new user accounts. When a new user is created using the useradd command, the contents of the /etc/skel/ directory are copied into the user's home directory.

The common contents of the /etc/skel/ directory include:

  • .bashrc: Default configurations for the Bash shell.
  • .bash_profile: Configure Bash behavior during the login process.
  • .profile: Shell configurations that are not specific to any particular shell but apply to all POSIX-compliant shells.
  • .bash_logout: Executed when a user logs out and may contain clean-up tasks.

Deleting Home Directories

When removing a user account, using the userdel command does not automatically delete the associated home directory and files. To delete a user account along with their home directory, you can use the userdel command with the -r option in linux user management.

Syntax:

Replace username with the actual username of the user account you want to delete. The -r option deletes user account with its home directory recursively. The -m option can be used with useradd to create a dedicated home directory for the new user.

Example: To delete the user bob along with his home directory:

 dedicated home directory

You can verify whether the home directory of the deleted user is still present using the ls command.

If the home directory has been successfully deleted, the ls command will show an error stating that the directory does not exist.

Login Shell

In Linux, the login shell is a command-line interface that starts when a user logs into the system. It provides the user with an interactive environment to execute commands, run programs, and manage files and directories. Linux offers various login shells such as:

  • The Bash is the default login shell for many Linux distributions due to its rich feature set, including command history, tab completion, scripting support, and extensive customization options
  • The Zsh shell offers advanced features such as spelling correction, shared command history among multiple shells, and extensive completion options.
  • The Fish shell provides an attractive and interactive interface with syntax highlighting and tab completion. Fish has a modern scripting language and emphasizes ease of use for new users.
  • The Dash is a lightweight and fast login shell. It is commonly used in scripts and system startup processes due to low resource usage and efficiency.

chsh

The chsh command of of user management in linux is used to specify a different shell from the one currently set as their default login shell.

Syntax:

If the [username] is not specified, it defaults to the current user.

Example:

The chsh command can be used to change the shell of bob,

 command can be used to change

In this example, we use the -s option to specify the new login shell (/usr/bin/zsh) for the user bob. If the shell change is successful, you need to enter the user's password for authentication. After entering the correct password, the default login shell will be changed to the specified shell.

Conclusion

  • User management in Linux involves creating, modifying, and deleting user accounts on the system.
  • The root user is the superuser with full administrative privileges.
  • The useradd command creates a new user account, and the /etc/default/useradd file sets default options for user creation.
  • The userdel command is used to delete user accounts, but it does not automatically remove home directories to avoid data loss.
  • The usermod command allows modifying existing user accounts.
  • The /etc/skel/ directory serves as a template directory for new user accounts.
  • Common login shells in Linux include Bash, Zsh, Fish, and Dash each with unique features and capabilities.
  • The chsh command changes the default login shell for a user, and the -s option specifies the new shell path.