Linux User Modifying

LinuxBeginner
Practice Now

Introduction

User management is a fundamental aspect of Linux system administration. Whether you're setting up a personal computer, managing a server, or working in a multi-user environment, understanding how to create, modify, and manage user accounts is essential.

In this lab, you will learn how to use the usermod command to modify user attributes such as username, home directory, and group membership. You'll gain practical experience with key commands used for user administration in Linux, providing you with the skills needed to efficiently manage user accounts in any Linux environment.

Creating and Renaming a User

In this step, you will create a new user and learn how to rename them. This is a common task when users change roles or when you need to standardize usernames across your system.

First, open your terminal. You should already be in the ~/project directory. Let's start by creating a new user called temporaryuser:

sudo useradd temporaryuser

This command creates a new user account in the system. By default, useradd doesn't create a home directory or set a password.

Now, let's check if the user was created successfully:

grep temporaryuser /etc/passwd

You should see an entry for temporaryuser in the output, confirming that the user has been created.

Next, we'll rename this user from temporaryuser to permanentuser using the usermod command:

sudo usermod -l permanentuser temporaryuser

The -l option specifies that we want to change the login name. After executing this command, the user previously known as temporaryuser will now be known as permanentuser.

Let's verify that the username has been changed:

grep permanentuser /etc/passwd

You should see the user entry with the new name permanentuser.

Now, let's create and assign a home directory for this user:

sudo usermod -d /home/permanentuser -m permanentuser

In this command:

  • -d specifies the new home directory path
  • -m creates the new home directory if it doesn't exist, or moves the content from the old home directory to the new one if both exist

Let's verify that the home directory has been created:

ls -ld /home/permanentuser

You should see the newly created home directory for permanentuser.

Adding a User to Groups

In Linux, groups are used to organize users and manage permissions. Adding a user to specific groups is an essential way to grant them access to certain resources or capabilities.

First, let's check what groups our user currently belongs to:

groups permanentuser

By default, when you create a user with useradd, the user is assigned to a primary group with the same name as the username.

Now, let's check if the sudo group exists on your system:

grep sudo /etc/group

You should see an entry for the sudo group. This special group allows its members to execute commands with superuser privileges using the sudo command.

Let's add our user to the sudo group:

sudo usermod -aG sudo permanentuser

In this command:

  • -a means "append," which ensures we're adding the user to the group without removing them from other groups
  • -G specifies the group(s) to which the user should be added
  • sudo is the name of the group
  • permanentuser is the user we're modifying

Let's verify that the user has been added to the sudo group:

groups permanentuser

The output should now include sudo in the list of groups.

You can also check the group membership by looking at the /etc/group file:

grep sudo /etc/group

The output should include permanentuser in the list of members of the sudo group.

Changing a User's Shell

Each user in Linux has a default shell that determines the command-line interface they use when logging in. In this step, you'll learn how to change a user's login shell.

First, let's check the current shell assigned to our user:

grep permanentuser /etc/passwd

Look at the last field in the output. It should show the current shell, which is typically /bin/sh for users created with useradd without additional options.

Now, let's change the shell to Bash, which is a more feature-rich shell:

sudo usermod -s /bin/bash permanentuser

The -s option specifies the new login shell for the user.

Let's verify that the shell has been changed:

grep permanentuser /etc/passwd

The end of the line should now show /bin/bash as the shell for permanentuser.

Different shells provide different features and user experiences. Bash is one of the most popular shells because it offers advanced features like:

  • Command completion
  • Command history navigation
  • Customizable prompts
  • Scripting capabilities with functions and control structures

Other common shells in Linux include:

  • /bin/sh - The Bourne Shell, a basic shell
  • /bin/zsh - Z Shell, with additional features beyond Bash
  • /bin/dash - Debian Almquist Shell, a lightweight shell

Users can switch between installed shells during their session using the chsh command or have their default shell changed by an administrator using usermod as we just did.

Setting Account Expiration and Viewing User Information

System administrators often need to create temporary accounts or ensure that accounts are periodically reviewed. In this step, you'll learn how to set an expiration date for an account and view comprehensive user information.

Setting Account Expiration

Let's set an expiration date for the permanentuser account. This is useful for temporary users or contractors who should only have access for a specific period.

sudo usermod -e 2023-12-31 permanentuser

The -e option sets the expiration date in the format YYYY-MM-DD. After this date, the account will be automatically disabled.

To verify the expiration date has been set:

sudo chage -l permanentuser

The chage -l command displays account aging information. The output will include an "Account expires" line showing the date you just set.

Viewing Detailed User Information

There are several commands to view user information in Linux. Let's explore them:

  1. Check user ID, group ID, and group memberships:
id permanentuser

This command shows the numeric user ID (UID), primary group ID (GID), and all groups the user belongs to.

  1. View password status and aging information:
sudo passwd -S permanentuser

This command shows the password status (locked, expired, etc.) and when it was last changed.

  1. View all users in the system:
cat /etc/passwd | grep -v nologin | grep -v false

This command filters the password file to show only users that can log in (excluding system users).

  1. View all groups on the system:
cat /etc/group

This shows all groups defined on the system, including system groups and user groups.

Understanding how to retrieve user information efficiently is crucial for system administration and troubleshooting user access issues.

Summary

In this lab, you have learned essential user management tasks in Linux:

  1. Creating a user and renaming them with the usermod -l command
  2. Setting up a home directory for a user with usermod -d -m
  3. Adding a user to supplementary groups with usermod -aG
  4. Changing a user's login shell with usermod -s
  5. Setting account expiration dates with usermod -e
  6. Viewing user information with commands like id, chage, and examining system files

These user management skills are fundamental for any Linux administrator. The usermod command provides a versatile toolset for modifying user attributes without having to delete and recreate user accounts.

By mastering these commands, you can efficiently manage user accounts in any Linux environment, whether it is a single-user workstation, a multi-user server, or an enterprise environment with many users requiring different levels of access.