How to Change Username in Linux (usermod & Manual Method Explained)

How to Change Username in Linux (usermod & Manual Method Explained)

Changing a username in Linux is a common system administration task, whether for renaming users, maintaining naming conventions, or fixing incorrect usernames. This guide covers multiple methods including usermod, manual system file updates, and GUI-based approaches for Ubuntu.


The usermod command is the safest and most recommended way to change a username in Linux. It updates the username along with the home directory and associated metadata.

Most Linux distributions create a group with the same name as the user by default (using :contentReference[oaicite:0]{index=0}). So when you rename a user, you should also update the group name to maintain consistency.

Before proceeding, make sure you have appropriate privileges using sudo or root access. You can verify your permissions using this guide on checking sudo access.

Rename user and update home directory:

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

Explanation:

  • -l → specifies the new username
  • -d → sets the new home directory path
  • -m → moves the existing home directory content to the new location

After renaming the user, update the group name (if it matches the old username):

bash
sudo groupmod -n newuser olduser

Verify the changes:

bash
getent passwd newuser
id newuser

What happens to file ownership?

  • All files owned by the user are linked internally using UID (User ID), not the username.
  • Since the UID remains unchanged, file ownership is automatically preserved.
  • You do not need to manually update ownership for most files.

However, always verify critical directories:

bash
ls -ld /home/newuser

If required, you can fix ownership manually:

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

For more details on managing users, refer to:


Method 2: Rename User Manually (Advanced)

This method is useful when usermod cannot be used or when you need full control over how user details are updated. It involves manually modifying system files that store user and group information.

WARNING
This approach is risky if not done carefully. Always take a backup of critical files before making changes:
bash
sudo cp /etc/passwd /etc/passwd.bak
sudo cp /etc/group /etc/group.bak
sudo cp /etc/shadow /etc/shadow.bak

Step 1: Update /etc/passwd

The /etc/passwd file stores basic user information such as username, UID, home directory, and shell.

bash
sudo vi /etc/passwd

Change:

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

To:

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

Here:

  • Username is updated from oldusernewuser
  • Home directory path is also updated

Step 2: Update /etc/group

The /etc/group file stores group information. If a group exists with the same name as the user, update it as well:

bash
sudo vi /etc/group

Change any occurrence of olduser to newuser.

Step 3: Update /etc/shadow

The /etc/shadow file stores encrypted password and account-related details.

bash
sudo vi /etc/shadow

Update the username field from olduser to newuser while keeping the password hash unchanged.

Step 4: Rename home directory

After updating system files, rename the user’s home directory:

bash
sudo mv /home/olduser /home/newuser

Fix ownership:

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

What happens to file ownership?

  • Just like with usermod, Linux tracks ownership using UID and GID, not usernames.
  • Since UID/GID are unchanged, most file ownership remains intact.
  • However, after manual changes, it is recommended to verify and correct ownership where needed.

Check ownership:

bash
ls -ld /home/newuser

If inconsistencies are found:

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

Verify the changes

bash
id newuser
getent passwd newuser

Ensure:

  • Username is updated
  • UID/GID remain unchanged
  • Home directory path is correct
IMPORTANT
  • Do not perform these changes while logged in as the target user
  • Always use root or another sudo-enabled account
  • Incorrect edits in /etc/passwd or /etc/shadow can break login functionality

For safer alternatives, consider using the usermod command as explained in Method 1.


Method 3: Change Username in Ubuntu (GUI Method)

This method applies only to systems running GNOME desktop (default in Ubuntu Desktop). It will not work on server editions or other desktop environments like XFCE or KDE.

Steps:

  1. Go to Settings → Users
  2. Unlock with administrator password
  3. Create a new user with desired username
  4. Transfer files from old user
  5. Delete old user

Alternatively, use gnome-control-center:

bash
gnome-control-center users
NOTE
  • GUI method does not directly rename the user
  • It creates a new user instead

Method 4: Rename User in Active Session (Workaround)

Linux does not allow renaming a user while they are logged in. Attempting this results in:

bash
usermod: user olduser is currently used by process

Workarounds:

Kill active user processes:

bash
sudo pkill -u olduser

Or:

bash
sudo killall -u olduser

Then run:

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

Better approach:

bash
sudo reboot

Log in as root or another user, then perform the rename operation.

Verify after rename:

bash
id newuser

Summary

Switching to GNOME on Linux depends on whether it is already installed and which desktop environment is currently active. You can quickly verify your current desktop using environment variables like XDG_CURRENT_DESKTOP and switch to GNOME from the login screen if it is available.

If GNOME is not installed, you can install it using your package manager and set it as the default session using system tools like update-alternatives. However, for most system administration tasks (such as user management), using terminal-based commands is preferred over GUI tools.


Official Documentation

Refer to the following official resources for more details:

These documents provide deeper insights into GNOME usage, configuration, and system integration.

Omer Cakmak

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 OpenStack, KVM, Proxmox, and VMware.