search
[last updated: 2025-09-26]
Linux home page
Linux commands
-----
ps, df, raspi-config, reboot, shutdown, dpkg, cat /proc/version and other variations, uname, python --version
Changing a username in Linux involves several steps to ensure consistency across the system, including updating the username itself, the home directory, and the primary group. It is crucial to perform these actions while not logged in as the user whose name is being changed.
Here are the steps to change a username in Linux: Create a Temporary Admin User.
Create a new temporary user with administrative privileges (e.g., in the sudo group) to perform the changes. This ensures you can still log in and manage the system while the original user's account is being modified.
Code
sudo adduser temp_admin
sudo usermod -aG sudo temp_admin
Log Out and Log In as Temporary Admin:
Log out of the current user's session and then log in as the newly created temp_admin user. Change the Username and Home Directory.
Use the usermod command to change the username and simultaneously move and rename the home directory to match the new username.
Code
sudo usermod -l new_username -m -d /home/new_username old_username
Replace new_username with the desired new username and old_username with the current username. The -m flag moves the home directory content, and -d specifies the new home directory path.
Change the Primary Group Name (Optional but Recommended):
If the primary group name is the same as the old username, it is generally recommended to change it to match the new username for consistency.
Code
sudo groupmod -n new_username old_username
Log Out and Log In as the New User:
Log out of the temp_admin account and log back in using the new_username and its associated password. Remove the Temporary Admin User (Optional).
Once you have confirmed that the new user account is functioning correctly, you can delete the temporary admin account.
Code
sudo userdel -r temp_admin
The -r flag removes the user's home directory and mail spool as well.
Important Considerations:
Backup:
Before making any system-level changes, it is always advisable to back up important data.
Active Processes:
Ensure the user whose name is being changed is not actively logged in or running any processes, as this can lead to errors. Rebooting or dropping to a single-user mode can help prevent issues with active processes and file locks.
Application Data:
Some applications might store configuration files or data that reference the old username or home directory path. While the home directory move helps, some applications might require manual adjustments or reconfigurations to function correctly with the new path.
----------------------------------------------------------------------------
press enter
message should confirm it was remounted
enter desired new password
retype to confirm
message should confirm successful update
press enter
----------------------------------------------------------------------------
-----------------------------------------------------------------
eof