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:
getent passwd olduserSample output:
olduser:x:1000:1000::/home/olduser:/bin/bashCheck the user's UID, GID, and groups:
id olduserSample output:
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:
pgrep -u olduser -aIf 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:
sudo usermod -l newuser -d /home/newuser -m olduserWhat each option does:
-l newuserchanges the login name fromoldusertonewuser.-d /home/newusersets the new login directory.-mmoves the old home directory contents to the new path.olduseris 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:
newuser:x:1000:1000::/home/newuser:/bin/bash
drwxr-x--- 2 1000 1000 100 Jun 11 09:52 /home/newuserThe 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:
sudo groupmod -n newuser olduserVerify the group entry:
getent group newuserSample output:
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:
getent passwd newuser
getent group newuser
id newuser
ls -ld /home/newuserSample output:
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/newuserAlso confirm that the old username is gone from the local account database:
getent passwd olduser
getent group olduserIf 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:
ls -ld /home/newuserIf the owner or group is wrong, fix it with:
sudo chown -R newuser:newuser /home/newuserFiles 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:
sudo find / -xdev -user newuser -lsIf 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:
usermod: user olduser is currently used by process 1234Check active processes:
pgrep -u olduser -aStop them cleanly where possible. If you are performing planned maintenance and are sure the processes can be terminated, you can use:
sudo pkill -u olduserThen run the rename command again:
sudo usermod -l newuser -d /home/newuser -m olduserFor 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:
sudo usermod -l newuser -d /home/newuser -m olduser
sudo groupmod -n newuser olduserThis 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.
- 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:
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.bakThe files involved are:
/etc/passwdfor the username, UID, GID, home directory, and shell./etc/groupfor group names and group membership./etc/shadowfor password aging and encrypted password fields./etc/gshadowfor 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
atjobs 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/olduseror 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:
sudo grep -R "/home/olduser" /etc 2>/dev/nullFor 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.

