Linux for Radio Astronomy

How to change a username in Linux

Good question — changing a username in Ubuntu is possible, but it needs care to avoid breaking permissions or SSH keys. Below is the safe, step-by-step method for renaming a user account — whether local or remote (via SSH).


🧱 Before You Begin

Let’s assume:

  • Your current username = olduser
  • You want to rename it to = newuser

⚠️ Important:
You cannot rename the user while you are logged in as that user.
You must log in as root or another admin account (e.g. one you can sudo from).

If you only have one account, create a temporary admin user first.


🧩 Step 1. Create a temporary admin (if needed)

If you only have one user:

sudo adduser tempadmin
sudo usermod -aG sudo tempadmin

Then log out and back in as tempadmin (or SSH in as it).


🧾 Step 2. Change the username

Now run:

sudo usermod -l newuser olduser

This changes the login name, but not the home directory path yet.


📂 Step 3. Rename the home directory

Rename the user’s home folder and update references:

sudo usermod -d /home/newuser -m newuser

⚙️ Step 4. Fix file ownership (if needed)

Ensure everything in the home directory is owned by the new username:

sudo chown -R newuser:newuser /home/newuser

🪪 Step 5. Update display name (optional)

If you want a nicer “Full Name” in who or the login screen:

sudo chfn -f "New User Full Name" newuser

🔄 Step 6. Check and reboot

Verify:

id newuser

Then reboot:

sudo reboot

You can now log in as the new username.


🧹 Step 7 (optional): Remove the temp admin

Once you’ve confirmed everything works:

sudo deluser tempadmin
sudo rm -r /home/tempadmin

✅ Example Summary

sudo adduser tempadmin
sudo usermod -aG sudo tempadmin
# log in as tempadmin
sudo usermod -l andrew drt
sudo usermod -d /home/andrew -m andrew
sudo chown -R andrew:andrew /home/andrew
sudo chfn -f "Dr Andrew Thornett" andrew
sudo reboot

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.