How to Rename a User, Change a UID, and Rename a Group in Linux.

Learn how to rename Linux users, change UIDs, update home directories, and rename groups using usermod and groupmod. This practical guide covers essential commands, verification steps, file ownership, and security considerations for Linux system administrators.

How to Rename a User, Change a UID, and Rename a Group in Linux.
Image credit: Unknown. Please contact us if you are the owner.

Managing user accounts is an essential part of Linux system administration. Whether you're reorganizing user accounts, standardizing usernames, or changing user IDs (UIDs), Linux provides command-line utilities to perform these tasks.

In this tutorial, we'll explore how to use usermod to rename an existing user account, change its UID, and use groupmod to rename a group.

We'll also cover important precautions to take before modifying user accounts on a production server.

Let's get started.

1. Rename an Existing User Account

Linux provides the usermod command to modify an existing user account. You can use it to change a username, UID, home directory, login shell, and other account attributes.

Unlike useradd or adduser, which are used to create accounts, usermod modifies accounts that already exist.

Important: Avoid editing /etc/passwd or /etc/shadow manually with a text editor. Use the appropriate account-management commands to prevent inconsistencies.

Step 1: Make Sure the User Is Not Logged In

Before renaming an account, ensure that the user is not running active processes or services that depend on the existing username.

Check the user's processes with:

$ ps -u USER -f

Replace USER with the username you intend to rename.

You should also check whether the user has active login sessions.

$ who

If the account is running a service, scheduled task, or other workload, stop or coordinate that workload before proceeding.

Avoid blindly running pkill -u USER or pkill -9 -u USER. These commands can terminate multiple processes belonging to the user, potentially interrupting applications or causing data loss. SIGKILL should be a last resort.

For a personal account, log out of the account and perform the operation from a separate administrative account.

Step 2: Rename the Username With usermod

The basic syntax is:

$ sudo usermod -l NEW_USERNAME OLD_USERNAME

Here:

  • -l specifies the new login name.
  • NEW_USERNAME is the desired username.
  • OLD_USERNAME is the existing username.

For example, suppose you have an account named batman and want to rename it to snubmonkey.

Run:

$ sudo usermod -l snubmonkey batman

This changes the login name from batman to snubmonkey.

Important: Renaming a user does not automatically rename the user's home directory or change the account's UID or primary group.

If the original home directory was /home/batman, it will normally remain /home/batman until you change it separately.

Step 3: Verify the Username Change

To verify the updated account information, run:

$ id snubmonkey

You can also inspect the account entry:

$ getent passwd snubmonkey

These commands help confirm that the account exists under the new username.

2. Rename the User's Home Directory

If you also want to change the home directory from /home/batman to /home/snubmonkey, use the -d and -m options.

The -d option specifies the new home directory, while -m moves the contents of the existing home directory to the new location.

For example:

$ sudo usermod -d /home/snubmonkey -m snubmonkey

This updates the account's configured home directory and moves its contents to /home/snubmonkey.

Before running this command, ensure that the destination path is appropriate and does not contain unrelated data.

Afterward, verify the result:

$ getent passwd snubmonkey

The output should show /home/snubmonkey as the account's home directory.

Note: Renaming a home directory does not automatically update every reference to the old path. Check SSH configurations, service definitions, scripts, cron jobs, and application configurations that may contain the previous username or home directory.

3. Change a User's UID (User ID)

Every Linux user account has a numerical identifier called a UID. Linux uses this identifier to determine file ownership and enforce access permissions.

You can display a user's UID and group memberships with:

$ id snubmonkey

Example output:

uid=1003(snubmonkey) gid=1003(snubmonkey) groups=1003(snubmonkey),4(adm),24(cdrom),27(sudo)

In this example:

  • The UID is 1003.
  • The primary GID is 1003.
  • The user belongs to several supplementary groups.

Suppose you want to change the UID of snubmonkey from 1003 to 777.

The syntax is:

$ sudo usermod -u NEW_UID USERNAME

To assign the new UID, execute:

$ sudo usermod -u 777 snubmonkey

The -u option specifies the new UID.

The new UID must be non-negative and, under normal circumstances, must not already be assigned to another user. The -o option allows duplicate UIDs, but sharing a UID can create security and ownership problems and should generally be avoided.

UID ranges reserved for system accounts vary by distribution and configuration. Choose an appropriate UID for your environment rather than assuming that a particular number is universally available for regular users.

Verify the UID Change

Run:

$ id snubmonkey

You should now see the updated UID:

uid=777(snubmonkey) gid=1003(snubmonkey) groups=1003(snubmonkey),4(adm),24(cdrom),27(sudo)

Notice that the UID has changed to 777, while the primary GID remains 1003.

Changing the UID does not automatically change the user's group IDs.

Important: Check File Ownership After Changing a UID

When usermod changes a UID, it adjusts ownership for files in the user's home directory as documented by the utility. Files outside the home directory may require separate attention.

Before changing the UID, consider recording the existing ownership and identifying files belonging to the old UID.

For example, to find files on a particular filesystem owned by UID 1003, run:

$ sudo find / -xdev -uid 1003 -print 2>/dev/null

The -xdev option prevents find from crossing filesystem boundaries. As a result, this command searches only the filesystem containing /; it will not necessarily find files on separate mounted filesystems.

Repeat the search for other relevant filesystems if necessary.

Important: After changing the UID, files that still have the old numeric ownership may no longer appear to belong to snubmonkey. Review those files carefully before changing their ownership.

On a production server, also check shared storage, application data, scheduled jobs, containers, and service configurations that may depend on the original UID.

4. Rename a Group With groupmod

Linux also provides the groupmod command to modify existing groups.

For example, suppose you have a group named king and want to rename it to queen.

The syntax is:

$ sudo groupmod -n NEW_GROUP OLD_GROUP

To rename king to queen, run:

$ sudo groupmod -n queen king

The -n option specifies the new group name.

Verify the change with:

$ getent group queen

You can also inspect the user's group memberships:

$ id snubmonkey

Does Renaming a Group Change Its GID?

No. Renaming a group changes its name, but its numerical group ID (GID) remains unchanged.

This is important because Linux uses numerical UIDs and GIDs to track ownership, not just the displayed names.

If you also need to change a group's GID, groupmod provides the -g option. However, changing a GID may require additional ownership checks for files and directories associated with the old GID.

What About the -o Option?

The -o option permits a non-unique GID when used with groupmod -g.

It is not an option for renaming a group with -n alone.

For example, the following command changes the GID to 777 while permitting that GID to be shared:

$ sudo groupmod -o -g 777 queen

Use this only when duplicate GIDs are intentional and you understand the ownership implications. For ordinary group renaming, you do not need -o.

That's it!
You now know how to rename an existing Linux user, change a UID, update a home directory, and rename a group using standard account-management commands.

The key commands covered in this tutorial are:

  • usermod -l — Rename a user account.
  • usermod -d -m — Change a user's home directory and move its contents.
  • usermod -u — Change a user's UID.
  • groupmod -n — Rename a group.
  • id and getent — Verify account information.

Before making changes on a production system, ensure that the affected account is not running critical workloads, verify that the new username and UID are available, and check file ownership and application dependencies.

Remember: Changing a username does not automatically change the UID, GID, home directory, or every reference to the original account name. Verify each component you intend to modify.

Thanks for reading, and happy Linux administration.

Keep Us Caffeinated  ⦿ ⦿
Spotify Logo
↑
SNUBmonkey Join our readers