usermod Command in Linux

Learn via video courses
Topics Covered

Overview

The usermod command in Linux is a crucial tool for system administrators. It allows them to modify existing user accounts, altering properties such as home directories, shell types, and group affiliations. Understanding how to use the usermod command in Linux can greatly enhance your ability to manage users effectively.

Syntax of usermod Command in Linux

The basic syntax of the usermod command in Linux is as follows:

Where:

  • usermod: This is the command itself, telling Linux to modify a user.
  • [options]: These are the different flags or parameters that you can use with the command to specify what modifications you want to make.
  • LOGIN: This is the username of the account you wish to modify.

Options in usermod Command in Linux

  1. -d, --home: This option changes the user's home directory.

    For example -

    Output:

    This command changes the home directory of 'username' to '/new/home/directory'.

  2. -s, --shell: This option changes the user's login shell.

    For example -

    Output:

    This command changes the login shell of 'username' to '/bin/ksh'.

  3. -a, --append: This option is used with -G and adds the user to the supplementary group(s).

    For example -

    Output:

    This command adds 'username' to 'group1' and 'group2' without removing them from any other groups.

Example Usages

  • Changing a user's home directory:

    Output:

    Explanation: This usage of the usermod command in Linux alters the home directory of the user.

  • Adding a user to additional groups:

    Output:

    Explanation: This usage of the usermod command in Linux adds the user to additional groups without removing them from their current groups.

Tips

  • Always make sure you have administrative rights or root access before attempting to modify user accounts with the usermod command in Linux. Without these rights, the command may not execute successfully.

  • When changing a user's home directory, it's a good practice to also move the contents of the old directory to the new one using the -m or --move-home option.

  • Be cautious when changing the login shell. Make sure the new shell is installed and configured correctly. A wrong shell can prevent the user from logging in.

Advanced Use Cases of usermod Command in Linux

  • Locking a user's password:

    Output:

    Explanation: This command locks the password of 'username', making it impossible for them to login until the password is unlocked.

  • Expire a user's password:

    Output:

    Explanation: This command sets the expiry date of 'username''s password to 1 day after the account was created, effectively expiring the password immediately.

  • Change user's primary group:

    Output:

    Explanation: This command changes 'username''s primary group to 'new_group'.

Conclusion

  • The usermod command in Linux is a powerful tool for managing user accounts.

  • It can modify various properties of a user account including the home directory, login shell, and group affiliations.

  • Advanced uses of the command include locking and expiring passwords, and changing the primary group of a user.

  • Remember to use the usermod command with caution, as incorrect usage can lead to user login issues.