When administering an Ubuntu Linux system, you might encounter a situation where an employee changes their name, or you simply made a typo when initially creating a user profile. Instead of deleting the entire account and painstakingly migrating all their files, permissions, and configurations to a brand new user, Linux provides a straightforward way to safely rename an existing user account using the terminal.
The Golden Rule: Do Not Rename While Logged In
The most important rule of renaming a user in Linux is that the user cannot have any active processes running. You absolutely cannot rename the account you are currently logged into. You must log out of the target account completely and log in with a different administrator (sudo) account. If you do not have a second administrator account, you will need to create a temporary one, perform the rename, and then delete the temporary account later.
Step 1: Rename the Username
The core tool for modifying user properties in Linux is the usermod command. To change the login name (the username typed at the login screen), use the -l (login) flag.
Open your terminal and use the following syntax: sudo usermod -l new_username old_username
For example, to change the username from “jsmith” to “jdoe”, you would run:
sudo usermod -l jdoe jsmith
This updates the system’s password and shadow files, but it does not rename their home directory.
Step 2: Rename the Home Directory
For the system to function correctly and avoid confusing the user, the home directory should match the new username. To rename the folder and update the user’s profile to point to the new location, use the -d (directory) and -m (move) flags.
The syntax is: sudo usermod -d /home/new_username -m new_username
Following our previous example, you would run:
sudo usermod -d /home/jdoe -m jdoe
This command physically renames the /home/jsmith folder to /home/jdoe and moves all the files without breaking any internal file permissions.
Step 3: Update the Display Name (Real Name)
The login name has been changed, but the “Real Name” (the formatted name displayed on the graphical login screen, like “Jane Doe”) remains the old one. We need to update the GECOS field.
To change the display name, use the -c (comment) flag:
sudo usermod -c "Jane Doe" jdoe
Step 4: Rename the Primary Group
By default, Ubuntu creates a unique user group that exactly matches the username. Since we changed the user, we should change the group to match. We use the groupmod command with the -n (new name) flag.
sudo groupmod -n jdoe jsmith
The user has now been completely renamed across the entire Linux filesystem.