How to change an username on Linux Ubuntu
To change a username in Ubuntu cleanly and safely, follow these steps from a different admin account (not the one you’re renaming) — or from a root shell.
⚠️ Important:
- You can’t rename the user while logged in as that user.
- Make a backup of any important data before proceeding.
🧑💻 Step-by-Step: Renaming a User in Ubuntu
1. Log in as a different admin user (or root)
If you only have one account, you can:
- Boot into recovery mode, or
- Use
sudo -i
to get a root shell from another user with sudo rights.
2. Check the current username
whoami
Or list all users:
cut -d: -f1 /etc/passwd
3. Change the username
Replace oldusername
with the current username and newusername
with the desired name:
sudo usermod -l newusername oldusername
-l
changes the login name.- This does not rename the home directory automatically.
4. Rename the home directory (optional but recommended)
sudo usermod -d /home/newusername -m newusername
-d
specifies the new home directory.-m
moves the old home contents to the new folder.
If you prefer to rename manually:
sudo mv /home/oldusername /home/newusername
sudo usermod -d /home/newusername newusername
5. Update group name (optional, but tidy)
If you want the primary group to match the new username:
sudo groupmod -n newusername oldusername
6. Update any references
You may need to:
- Update
sudoers
if you added the user explicitly there. - Check scripts, cron jobs, or services that referenced the old name.
- Log out and back in as the new user.
7. Reboot (recommended)
sudo reboot
After reboot, log in with:
newusername
✅ Example
Renaming john
to andrew
:
sudo usermod -l andrew john
sudo usermod -d /home/andrew -m andrew
sudo groupmod -n andrew john
sudo reboot