How to Change Username in Linux Using usermod

Learn how to change username in Linux with usermod, rename the home directory, update the primary group, verify UID/GID ownership, and fix common username rename errors safely.

Published

Updated

Read time 8 min read

Reviewed byDeepak Prasad

How to Change Username in Linux Using usermod

Changing a username in Linux is more than replacing one word in /etc/passwd. A proper Linux username rename should update the login name, move the home directory when required, keep the UID and GID consistent, and avoid running the command while the target user is still logged in. The safest normal method is to use usermod for the user account and groupmod if the user's primary group has the same old name.

Tested On: The commands were tested on an Ubuntu Linux system using standard shadow-utils account management tools. The same usermod, groupmod, getent, and ownership checks apply to most Linux distributions, including Ubuntu, Debian, Rocky Linux, AlmaLinux, and RHEL, though default shells and UID/GID values may differ.


Before You Change a Linux Username

Do not rename the same user account you are currently using. Log in as root or another sudo-enabled user first. If you are connected over SSH as the target user, open a separate administrator session before changing the username.

Check the current user entry:

bash
getent passwd olduser

Sample output:

text
olduser:x:1000:1000::/home/olduser:/bin/bash

Check the user's UID, GID, and groups:

bash
id olduser

Sample output:

text
uid=1000(olduser) gid=1000(olduser) groups=1000(olduser)

Before changing the username, plan to run the actual rename from a different administrator account. The next method starts by checking whether the target user still has active sessions.


Method 1: Change Username with usermod

This is the recommended method for local Linux users because usermod updates the account database in a controlled way. Use it when you want to rename olduser to newuser and move /home/olduser to /home/newuser.

Before running usermod, make sure the target user is logged out. Run the rename from root or another sudo-enabled account, then check whether olduser still has active sessions or processes:

bash
pgrep -u olduser -a

If this command prints output, stop those sessions cleanly before continuing. For a desktop user, log out from the graphical session. For a server user, close SSH sessions or stop services running under that account during a maintenance window.

Rename the Login Name and Home Directory

The -l option changes the login name. By itself, it does not rename the home directory. That is why the command below also uses -d to set the new home path and -m to move the existing home directory contents:

bash
sudo usermod -l newuser -d /home/newuser -m olduser

What each option does:

  • -l newuser changes the login name from olduser to newuser.
  • -d /home/newuser sets the new login directory.
  • -m moves the old home directory contents to the new path.
  • olduser is the existing account name being modified.

In testing, the account database changed from olduser to newuser, and the home directory moved to the new path:

text
newuser:x:1000:1000::/home/newuser:/bin/bash
drwxr-x--- 2 1000 1000 100 Jun 11 09:52 /home/newuser

The numeric owner values are expected in low-level output because Linux stores ownership by UID and GID. The displayed username changes when name service lookup resolves UID 1000 to newuser.

Rename the Matching Primary Group

On many Linux systems, a new local user gets a private primary group with the same name as the username. For example, olduser may have a primary group named olduser. After changing the Linux username, rename that group for consistency:

bash
sudo groupmod -n newuser olduser

Verify the group entry:

bash
getent group newuser

Sample output:

text
newuser:x:1000:

This does not change the numeric GID. It only changes the group name shown by commands such as id, ls -l, and getent group.

Verify the New Username

After changing the username in Linux, confirm that the account, group, and home directory agree:

bash
getent passwd newuser
getent group newuser
id newuser
ls -ld /home/newuser

Sample output:

text
newuser:x:1000:1000::/home/newuser:/bin/bash
newuser:x:1000:
uid=1000(newuser) gid=1000(newuser) groups=1000(newuser)
drwxr-x--- 2 newuser newuser 100 Jun 11 09:52 /home/newuser

Also confirm that the old username is gone from the local account database:

bash
getent passwd olduser
getent group olduser

If these commands return no output, the old local user and group names are no longer present.

Fix Home Directory Ownership If Needed

In most cases, changing only the username does not break file ownership because Linux permissions are tied to numeric UID and GID values, not the text username. Files already owned by UID 1000 remain owned by UID 1000; the displayed name simply changes after the account is renamed.

Check the new home directory:

bash
ls -ld /home/newuser

If the owner or group is wrong, fix it with:

bash
sudo chown -R newuser:newuser /home/newuser

Files outside the home directory need separate review. This is useful when the old user owned application directories, web files, backups, cron output, or custom data paths:

bash
sudo find / -xdev -user newuser -ls

If you changed the UID or GID separately, ownership outside the home directory may need a broader cleanup. For a simple username rename where the UID stays the same, this is usually only a verification step.

Fix the "user is currently used by process" Error

If the target user is still logged in or has active processes, usermod can fail with an error like this:

text
usermod: user olduser is currently used by process 1234

Check active processes:

bash
pgrep -u olduser -a

Stop them cleanly where possible. If you are performing planned maintenance and are sure the processes can be terminated, you can use:

bash
sudo pkill -u olduser

Then run the rename command again:

bash
sudo usermod -l newuser -d /home/newuser -m olduser

For production servers, a reboot or maintenance window is safer than killing unknown user processes during active work.


Method 2: Change Username on Ubuntu Desktop

Ubuntu Desktop settings do not reliably perform a full local account rename with home directory migration. The common GUI path is to create a new user, copy files, and remove the old user after verification.

For a real username rename on Ubuntu, use the command line from another administrator account:

bash
sudo usermod -l newuser -d /home/newuser -m olduser
sudo groupmod -n newuser olduser

This is the same method used on Ubuntu Server and other Linux distributions that use the standard local /etc/passwd, /etc/group, and /etc/shadow account files.

NOTE
  • GUI method does not directly rename the user
  • It creates a new user instead

Method 3: Rename a User Without usermod

Manual edits should be a last resort. The local account files are sensitive, and a typo in /etc/passwd or /etc/shadow can break login access. If you must inspect the files, make backups first:

bash
sudo cp /etc/passwd /etc/passwd.bak
sudo cp /etc/group /etc/group.bak
sudo cp /etc/shadow /etc/shadow.bak
sudo cp /etc/gshadow /etc/gshadow.bak

The files involved are:

  • /etc/passwd for the username, UID, GID, home directory, and shell.
  • /etc/group for group names and group membership.
  • /etc/shadow for password aging and encrypted password fields.
  • /etc/gshadow for secure group fields.

For most systems, prefer usermod and groupmod instead of direct file editing because the tools update the account databases in a controlled way.


What usermod Does Not Change Automatically

When you use usermod -l, the login name changes, but some related resources may still need review:

  • Mail spool names may need manual handling, depending on local mail configuration.
  • Cron jobs and at jobs owned by the old account may need manual review.
  • Files outside the user's home directory are not renamed by path.
  • Application configs may still contain /home/olduser or the old username as plain text.
  • Remote identity systems such as LDAP, FreeIPA, or Active Directory must be changed in their own directory service, not only with local usermod.

Search for old home path references before closing the task:

bash
sudo grep -R "/home/olduser" /etc 2>/dev/null

For application data under /opt, /srv, or web roots, search only the relevant directories instead of scanning the entire filesystem.


Common Questions About Linux Username Changes

Can I rename the user I am logged in as?

No. Use root or another sudo-enabled account. If the target user has active processes, usermod may refuse to change the login name, home directory, or UID until those processes are stopped.

Does changing username also rename the home directory?

Not with usermod -l alone. Use -d /home/newuser -m with usermod to set the new home directory and move the old home directory contents.

Does the UID change when I rename a Linux user?

No. A username rename changes the text login name. The UID remains the same unless you explicitly change it with usermod -u.

Should I rename the group too?

If the user has a private primary group with the old username, rename it with groupmod -n newuser olduser. This keeps command output and ownership display consistent.


Summary

To change username in Linux safely, log in as another administrator, confirm the old user has no active sessions, run sudo usermod -l newuser -d /home/newuser -m olduser, and rename the matching primary group with sudo groupmod -n newuser olduser when needed. After the rename, verify the account with getent passwd, getent group, id, and ls -ld /home/newuser. The UID and GID normally remain unchanged, so file ownership is preserved, but home directory permissions, cron jobs, mail spool paths, and application configs should still be checked before considering the Linux user rename complete.


Official Documentation

Omer Cakmak

Linux Administrator

Highly skilled at managing Debian, Ubuntu, CentOS, Oracle Linux, and Red Hat servers. Proficient in bash scripting, Ansible, and AWX central server management, he handles server operations on …