How to use the command usermod (with examples)

How to use the command usermod (with examples)

The usermod command is used to modify a user account on a Linux system. It allows you to change various attributes of a user, such as the username, user ID, user shell, group membership, and home directory.

Use case 1: Change a username

Code:

sudo usermod --login new_username username

Motivation: The motivation for changing a username may be to align it with a user’s real name or to comply with a specific naming convention. It is useful in scenarios where a user wants to update their username without creating a new account.

Explanation:

  • --login: This option specifies the new username.
  • new_username: The new username that you want to set.
  • username: The current username that you want to change.

Example output:

$ sudo usermod --login johnsmith jsmith

This command will change the username of the user “jsmith” to “johnsmith”.

Use case 2: Change a user ID

Code:

sudo usermod --uid id username

Motivation: The motivation for changing a user ID may be to align it with a new system or to manage user privileges. This is useful when a user needs to have a specific user ID.

Explanation:

  • --uid: This option specifies the new user ID.
  • id: The new user ID that you want to set.
  • username: The username of the user whose ID you want to change.

Example output:

$ sudo usermod --uid 1001 jsmith

This command will change the user ID of the user “jsmith” to 1001.

Use case 3: Change a user shell

Code:

sudo usermod --shell path/to/shell username

Motivation: The motivation for changing a user shell may be to provide a different default shell to a user. This is useful when a user wants to use a specific shell for their account.

Explanation:

  • --shell: This option specifies the new shell path.
  • path/to/shell: The path to the new shell that you want to set.
  • username: The username of the user whose shell you want to change.

Example output:

$ sudo usermod --shell /bin/zsh jsmith

This command will change the shell of the user “jsmith” to /bin/zsh.

Use case 4: Add a user to supplementary groups

Code:

sudo usermod --append --groups group1,group2,... username

Motivation: The motivation for adding a user to supplementary groups is to grant additional access rights to specific groups. This is useful when a user needs to be a member of multiple groups to have the necessary permissions.

Explanation:

  • --append: This option appends the user to the specified groups instead of replacing the current group membership.
  • --groups: This option specifies the list of supplementary groups to add the user to.
  • group1,group2,...: The comma-separated list of supplementary groups.
  • username: The username of the user you want to add to the groups.

Example output:

$ sudo usermod --append --groups admins,developers jsmith

This command will add the user “jsmith” to the supplementary groups “admins” and “developers”.

Use case 5: Change a user home directory

Code:

sudo usermod --move-home --home path/to/new_home username

Motivation: The motivation for changing a user’s home directory may be to move their files to a different location or to update the user’s workspace. This is useful when a user wants to have their files stored in a different directory.

Explanation:

  • --move-home: This option moves the content of the current home directory to the new home directory.
  • --home: This option specifies the new home directory path.
  • path/to/new_home: The path to the new home directory.
  • username: The username of the user whose home directory you want to change.

Example output:

$ sudo usermod --move-home --home /home/new_home jsmith

This command will change the home directory of the user “jsmith” to “/home/new_home” and move the content of the current home directory to the new location.

Conclusion:

The usermod command is a powerful tool for managing user accounts on a Linux system. By using different options and arguments, you can easily modify various attributes of a user, such as the username, user ID, user shell, group membership, and home directory. These use cases demonstrate how to utilize the usermod command to make changes to user accounts efficiently.

Related Posts

How to use the command 'mktemp' (with examples)

How to use the command 'mktemp' (with examples)

The ‘mktemp’ command is used to create temporary files or directories.

Read More
How to use the command 'htpdate' (with examples)

How to use the command 'htpdate' (with examples)

The htpdate command is used to synchronize the local date and time by retrieving the date and time via HTTP headers from web servers.

Read More
AWS Lambda Command Line Interface (CLI) Examples (with examples)

AWS Lambda Command Line Interface (CLI) Examples (with examples)

Run a function aws lambda invoke --function-name name path/to/response.json Motivation: The command is used to run a specific Lambda function and capture the output in a JSON file.

Read More