Linux Tutorial

Linux File/Directory Management

Linux Packaging And Compression

Vim Text Editor

Linux Text Processing

Linux Software Installation

Linux User/User Group Management

Linux Permission Management

Linux Filesystem Management

Linux Advanced Filesystem Management

Linux System Management

Linux Backup and Recovery

Linux System Service Management

Linux System Log Management

Linux Boot Management

LAMP/LNMP Environment

SELinux Management

Linux usermod Command: Modify User Information

The usermod command in Linux is used to modify user accounts. It allows administrators to change various user settings, such as home directories, default shells, and primary groups. In this tutorial, we'll cover how to use the usermod command and its options.

Changing a user's home directory

To change a user's home directory, use the -d flag followed by the new home directory path:

sudo usermod -d /new/home/directory username

You can also use the -m flag in conjunction with the -d flag to move the contents of the current home directory to the new one:

sudo usermod -m -d /new/home/directory username

Changing a user's default shell

To change a user's default shell, use the -s flag followed by the path to the new shell:

sudo usermod -s /bin/tcsh username

Adding a user to a group

To add a user to one or more secondary groups, use the -a and -G flags followed by a comma-separated list of group names:

sudo usermod -a -G group1,group2 username

Changing a user's primary group

To change a user's primary group, use the -g flag followed by the new primary group:

sudo usermod -g new_primary_group username

Changing a user's login name

To change a user's login name, use the -l flag followed by the new login name:

sudo usermod -l new_login_name old_login_name

Locking a user account

To lock a user account and prevent the user from logging in, use the -L flag:

sudo usermod -L username

Unlocking a user account

To unlock a previously locked user account, use the -U flag:

sudo usermod -U username

Modifying a user account with multiple options

You can combine multiple options to modify a user account with specific settings:

sudo usermod -d /new/home/directory -s /bin/tcsh -a -G group1,group2 -g new_primary_group username

In conclusion, the usermod command is a versatile tool for managing user accounts in Linux. By understanding its various options, you can modify user accounts to suit your system's requirements and maintain a secure and organized user environment.

  1. How to use the Linux usermod command:

    • Description: The usermod command is used to modify user account attributes in Linux.
    • Code:
      # Example: Modifying user attributes
      sudo usermod options username
      
  2. Modifying user attributes in Linux with usermod:

    • Description: usermod allows for the modification of various user attributes, such as username, home directory, login shell, etc.
    • Code:
      # Example: Modifying user attributes
      sudo usermod -l newname -d /newhome -s /bin/bash oldname
      
  3. Changing user details like username and home directory:

    • Description: The -l option changes the username, and the -d option changes the home directory.
    • Code:
      # Example: Changing username and home directory
      sudo usermod -l newname -d /newhome oldname
      
  4. Adding and removing users from groups using usermod:

    • Description: The -G option adds the user to specified groups, and -g sets the primary group.
    • Code:
      # Example: Adding and removing users from groups
      sudo usermod -G group1,group2 -g newprimarygroup username
      
  5. Setting expiration date for a user with usermod:

    • Description: The -e option sets the expiration date for the user account.
    • Code:
      # Example: Setting expiration date for a user
      sudo usermod -e 2023-12-31 username
      
  6. Adjusting login shell and password aging with usermod:

    • Description: The -s option sets the login shell, and -e and -I options adjust password aging policies.
    • Code:
      # Example: Adjusting login shell and password aging
      sudo usermod -s /bin/zsh -e 2024-12-31 -I 30 username
      
  7. Modifying UID and GID of a user in Linux:

    • Description: The -u option sets the UID, and -g sets the GID of the user.
    • Code:
      # Example: Modifying UID and GID
      sudo usermod -u 1001 -g 1001 username
      
  8. Batch modifying user attributes with usermod:

    • Description: Multiple modifications can be combined in a single usermod command for efficiency.
    • Code:
      # Example: Batch modifying user attributes
      sudo usermod -l newname -d /newhome -s /bin/bash -G group1,group2 -e 2023-12-31 username
      
  9. usermod examples for different scenarios in Linux:

    • Description: usermod can be adapted for various scenarios, such as changing multiple attributes simultaneously.
    • Code:
      # Example: Adapting usermod for different scenarios
      sudo usermod -l newname -G group1,group2 -s /bin/bash username
      
  10. Troubleshooting common issues with usermod: