How to Safely Change a Username in Ubuntu: Complete Step-by-Step Guide for Beginners

1. Introduction

There may be situations where you want to change your username while using Ubuntu. For example, you might want to do so for the following reasons:

  • To organize your system
  • For privacy protection or security reasons
  • To align with a new naming convention or project

Changing a username may seem simple, but in reality, it requires careful steps. If done incorrectly, you may lose access to the system or cause permission issues.

This guide explains in detail, step by step, how to safely and reliably change a username in Ubuntu, tailored for beginners and intermediate users. By following along, you will learn how to perform the change correctly without damaging your system.

2. Preparation

How to Confirm Administrator Privileges

You need administrator (sudo) privileges to change a username. To check if your current user has these privileges, run the following command:

id

If the output looks like the example below, you have administrator privileges:

uid=1000(john) gid=1000(john) groups=1000(john),27(sudo)

Point: Make sure that sudo is included in the groups list.

System Backup Recommendation

Since changing a username can affect the entire system, creating a backup beforehand is strongly recommended. Here is an example command to compress and back up the home directory:

sudo tar -cvpzf /path/to/backup/home-backup.tar.gz /home/target-username

Important: Store your backup in a safe location. If something goes wrong, you can restore your system using this backup.

Potential Impact of the Change

Changing a username may affect the following settings and applications:

  • SSH keys and authentication files
  • crontab scheduled tasks
  • Environment variables and paths referenced in scripts

Review these areas in advance and back up configurations as needed.

3. Steps to Change the Username

Step 1: Create a New Administrator User

If you plan to modify the current user, you need to create a new administrator user first. Use the following commands:

sudo adduser new-username
sudo usermod -aG sudo new-username

Example:
If the new username is “admin”:

sudo adduser admin
sudo usermod -aG sudo admin

After creating the user, log in with the new account to proceed.

Step 2: Log Out and Stop Processes of the Existing User

If the target user is logged in, errors may occur. Stop all running processes:

sudo pkill -u old-username

To verify: Check if processes remain:

ps -u old-username

Step 3: Change the Username

Use the usermod command to change the username:

sudo usermod -l new-username old-username
sudo groupmod -n new-group old-group

Example:
Old username “john” → new username “doe”:

sudo usermod -l doe john
sudo groupmod -n doe john

Step 4: Update the Home Directory

After changing the username, update the home directory name:

sudo mv /home/old-username /home/new-username
sudo usermod -d /home/new-username new-username

Example:

sudo mv /home/john /home/doe
sudo usermod -d /home/doe doe

Step 5: Verify and Fix Permissions

Ensure the new user has full ownership of the new home directory:

sudo chown -R new-username:new-group /home/new-username

Example:

sudo chown -R doe:doe /home/doe

Step 6: Confirm the Changes

Verify that the username and home directory changes were applied correctly:

cat /etc/passwd | grep new-username
ls -l /home

Result: Ensure the new username and directory are displayed correctly.

4. Notes and Troubleshooting

Important Notes

1. Logging Out Before Changing the Username

Make sure the target user is logged out before making changes. Otherwise, the changes may not apply properly.

How to check:

who | grep old-username

2. Impact on SSH Connections

Changing a username also requires updating paths used by SSH configuration files (e.g., ~/.ssh/authorized_keys). If old paths are referenced, SSH login will fail.

Solution:

  • Move the .ssh folder to the new user directory.
  • Review and correct permissions.
sudo chown -R new-username:new-group /home/new-username/.ssh
chmod 700 /home/new-username/.ssh
chmod 600 /home/new-username/.ssh/authorized_keys

3. Impact on Scheduled Tasks (crontab)

A username change may prevent scheduled jobs from running.

Check current crontab:

sudo crontab -u old-username -l

Reconfigure tasks:

sudo crontab -u new-username -e

Troubleshooting

1. Error: Permission denied

Cause: Missing required privileges.
Solution: Always prepend commands with sudo.

sudo usermod -l new-username old-username

2. Error: user is currently used by process

Cause: Processes for the old user are still running.
Solution:

  • Terminate processes:
sudo pkill -u old-username
  • Verify no remaining processes:
ps -u old-username

3. Unable to Log In After the Change

Cause: Incorrect username or password settings.
Solution:

  • Log in with another administrator account and review settings.
  • Edit /etc/passwd if necessary.
sudo nano /etc/passwd

4. Home Directory Not Recognized

Cause: Incorrect directory assigned during usermod execution.
Solution:

sudo usermod -d /home/new-username new-username
sudo chown -R new-username:new-group /home/new-username

5. FAQ

Q1. What if the system does not work correctly after changing the username?

A:
Boot into recovery mode and manually review /etc/passwd or /etc/group. Correct the username if necessary.

Q2. Do I need to regenerate SSH keys?

A:
No. Existing keys can be reused, but ensure they are placed correctly in the new user’s .ssh directory with proper permissions.

Q3. Does this affect environment variables?

A:
Yes. If environment files such as ~/.bashrc or ~/.profile reference old paths, update them accordingly.

Q4. What if multiple users exist on the system?

A:
Apply changes only to the intended user to avoid affecting others.

6. Summary

Changing a username in Ubuntu may appear complex, but with proper preparation and careful execution, it can be done safely and effectively. This guide covered the essential points to ensure system stability while changing a username.

Main Takeaways

  1. Importance of Preparation
    Backing up and confirming administrator privileges prevents unexpected issues.
  2. Clear Step-by-Step Instructions
    Each command and process was explained to help users perform the operation smoothly.
  3. Notes and Troubleshooting
    We provided solutions to commonly encountered errors.
  4. FAQ for Common Concerns
    Answers covered SSH settings, environment variables, and login issues.

Next Steps

After changing the username, verify the following:

  1. Test login using the new username
    Check both SSH and local logins.
  2. Update related configurations
    Scripts or scheduled tasks referencing the old username must be updated.
  3. Retain your backup
    Keep it until everything works perfectly.

Wishing you a smooth and enjoyable Linux experience!