How to Change Username on Ubuntu
Changing your username on Ubuntu might be necessary for various reasons—perhaps you’ve changed your legal name, inherited a computer with someone else’s account, or simply want a more professional username for your work environment. While this process isn’t as straightforward as changing a password, with the right guidance, you can safely modify your Ubuntu username without compromising your system. This comprehensive guide walks through the entire process of changing a username in Ubuntu, covering both graphical and command-line methods, along with potential pitfalls and their solutions.
Understanding Username Structure in Ubuntu
Before diving into the process of changing usernames, it’s essential to understand how Ubuntu handles user accounts and the system architecture behind them.
User accounts and system architecture
In Ubuntu, like other Linux distributions, usernames are more than just labels. They’re integral parts of the operating system’s security model. Each username corresponds to a unique User ID (UID), which the system uses to track file ownership and access permissions. Your username is tied to your home directory (typically /home/username) and is referenced in numerous configuration files throughout the system.
Ubuntu distinguishes between your login username (what you type at the login screen) and your display name or “real name” (what appears in the user interface). While changing your display name is a simple task, modifying your login username requires more care.
Implications of changing a username
Changing your username affects more than just what you type at login. It impacts:
- File and directory ownership throughout the system
- Application configurations that reference your home directory path
- System services that may be configured to run under your user account
- Scheduled tasks (cron jobs) associated with your account
- Group memberships and permissions
This interconnected nature makes username changes more complex than they might initially seem, requiring careful planning and execution.
Prerequisites Before Changing Username
Proper preparation prevents poor performance. This adage holds especially true when modifying core system elements like usernames in Ubuntu.
Creating a system backup
Before attempting to change your username, create a comprehensive backup of your system. At a minimum, back up your entire home directory and essential system configuration files. For maximum safety, consider a full system backup using tools like Timeshift, Déjà Dup, or a simple rsync command to an external drive.
sudo apt install timeshift
sudo timeshift --create --comments "Before username change"
Alternatively, for a manual backup of your home directory:
sudo rsync -aAXv /home/yourusername/ /path/to/backup/location/
Required permissions and access
Changing a username requires administrative privileges. You’ll need sudo access or root permissions to execute the necessary commands. Importantly, you cannot change the username of an account while logged into that account. The user account being modified must be logged out, which creates a logistical challenge requiring planning.
Preparing for the change
Before proceeding, create a detailed checklist:
- Identify all services running under your user account
- Note any applications with hardcoded paths to your home directory
- Document your current group memberships with:
groups username
- List all files owned by your account outside your home directory:
sudo find / -user yourusername -not -path "/home/*" -not -path "/proc/*" 2>/dev/null
- Ensure you have another administrative user account available for the process
Creating a Temporary Administrator Account
Since you can’t change your own username while logged in, you’ll need an alternative administrative account to perform the changes.
For Ubuntu Desktop users
- Open Settings and navigate to “Users” or “User Accounts”
- Click the “Add User” or “+” button
- Fill in the required information for the new temporary user
- Make sure to set the account type to “Administrator”
- Set a secure password that you’ll remember
- After creating the account, log out of your current session
Creating this temporary account through the GUI is straightforward and provides a safety net in case anything goes wrong during the username change process.
For Ubuntu Server/SSH users
If you’re working with Ubuntu Server or accessing your system via SSH, create a temporary admin user with these commands:
sudo adduser tempAdmin
sudo usermod -aG sudo tempAdmin
After creating the temporary user, verify that it has sudo privileges:
su - tempAdmin
sudo -l
The command should list the sudo privileges available to the temporary user. If correctly configured, you can proceed with the username change process.
The Username Change Process
Now that preparations are complete, let’s execute the username change.
Logging out and switching users
First, ensure that the user account you want to modify is completely logged out of all sessions. Check for any active processes:
sudo ps -u oldusername
If needed, terminate persistent processes:
sudo pkill -u oldusername
Then log in with your temporary administrator account.
Changing the username with usermod
The primary command for changing a username in Ubuntu is usermod
. This powerful tool modifies user account properties, including the username itself. Here’s the basic syntax:
sudo usermod -l newusername oldusername
This command changes only the username, not the home directory or group name. For example, to change a username from “john” to “jonathan”:
sudo usermod -l jonathan john
Updating the group name
In Ubuntu, each user typically has a primary group with the same name as their username. After changing the username, you should update this group name to maintain consistency:
sudo groupmod -n newusername oldusername
This keeps your primary group aligned with your new username, helping prevent permission issues. For our example:
sudo groupmod -n jonathan john
Modifying the home directory
By default, changing your username doesn’t update your home directory path. You have two options:
- Create a new home directory and move the contents:
sudo mkdir /home/newusername sudo cp -r /home/oldusername/. /home/newusername/ sudo chown -R newusername:newusername /home/newusername
- Rename the existing home directory:
sudo usermod -d /home/newusername -m newusername
The second option uses the -m
flag to move the contents automatically. For our example:
sudo usermod -d /home/jonathan -m jonathan
Creating symbolic links for compatibility
Some applications might still reference the old home directory path. Creating a symbolic link can help during the transition:
sudo ln -s /home/newusername /home/oldusername
This link redirects access attempts to the old path toward the new location, reducing the likelihood of application errors.
Step-by-Step Username Change Guide
Let’s consolidate what we’ve learned into a sequential procedure for two common scenarios.
Method 1: For Ubuntu Desktop users
- Create a temporary administrator account through System Settings > Users
- Log out completely from your current account
- Log in with the temporary administrator account
- Open Terminal and execute these commands in sequence:
sudo usermod -l newusername oldusername sudo groupmod -n newusername oldusername sudo usermod -d /home/newusername -m newusername sudo ln -s /home/newusername /home/oldusername
- Update the display name (optional):
sudo chfn -f "New Display Name" newusername
- Verify the changes:
ls -la /home/ grep newusername /etc/passwd
- Log out from the temporary account
- Log in with your new username
- Test that applications function correctly
- Once satisfied, you can remove the symbolic link and temporary admin account
Method 2: For Ubuntu Server/SSH users
For headless servers or SSH connections, the process requires additional care:
- Connect via SSH and create a temporary admin user:
sudo adduser tempAdmin sudo usermod -aG sudo tempAdmin
- Disconnect and reconnect using the temporary account:
ssh tempAdmin@your-server-ip
- Check for and terminate processes for the old username:
sudo ps -u oldusername sudo pkill -u oldusername
- Perform the username change:
sudo usermod -l newusername oldusername sudo groupmod -n newusername oldusername sudo usermod -d /home/newusername -m newusername
- Check for and update any service configurations:
sudo grep -r "oldusername" /etc/
- Test logging in with the new account in a separate session
- After confirming everything works, remove the temporary admin account
Managing Application Settings After Username Change
After changing your username, some application settings may need adjustment.
Default application configurations
Many applications store their configurations in hidden directories within your home folder (files and directories that start with a dot, like .config
). While the file ownership changes when you move your home directory, some applications may have hardcoded paths or references to your old username.
Common locations to check include:
- ~/.config/
- ~/.local/share/
- ~/.cache/
- Application-specific directories like ~/.mozilla/ for Firefox
Most modern applications adapt automatically to home directory changes, but legacy applications might struggle.
Application-specific considerations
Some applications require special attention after username changes:
- Email clients: Check account settings and mail storage locations
- Database servers: Update user permissions and ownership
- Development environments: Update paths in project configurations
- Web servers: Verify virtual host configurations and file permissions
- SSH: Update ~/.ssh/config if it contains references to your old home path
For databases like MySQL or PostgreSQL, you might need to update user accounts:
sudo -u postgres psql -c "ALTER USER oldusername RENAME TO newusername;"
Fixing Permissions and Ownership
After changing your username, ensure file permissions and ownership are correctly updated.
Understanding file ownership issues
In Linux, every file and directory has an owner and a group assignment. When you change your username, files previously owned by your old username might need ownership updates, especially if they’re located outside your home directory.
To find files owned by your old username:
sudo find / -user oldusername -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null
Commands to correct permissions
Use the chown
command to update ownership of files and directories:
sudo chown -R newusername:newusername /path/to/directory
For system-wide files that should be owned by your user:
sudo find / -user oldusername -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null | xargs sudo chown newusername:newusername
Automating permission fixes
For extensive file collections, you might want to create a simple script to update permissions:
#!/bin/bash
# Change ownership of files from old user to new user
old_user="oldusername"
new_user="newusername"
sudo find / -user $old_user -not -path "/proc/*" -not -path "/sys/*" 2>/dev/null | while read file; do
echo "Changing ownership of $file"
sudo chown $new_user:$new_user "$file"
done
Save this as fix_permissions.sh
, make it executable with chmod +x fix_permissions.sh
, and run it with proper precautions.
Special Considerations for System Services
Some system services might be configured to run under your user account, requiring additional attention.
Identifying affected services
To identify services that might reference your old username:
sudo grep -r "oldusername" /etc/systemd/
sudo grep -r "oldusername" /etc/
Pay special attention to:
- Systemd service files (.service)
- Cron jobs (/etc/cron.d/ or user crontabs)
- Web server configurations (/etc/apache2/, /etc/nginx/)
- SSH configurations (/etc/ssh/)
Updating service configurations
For systemd services, edit the service files:
sudo systemctl edit servicename
After updating service configurations, reload systemd and restart the affected services:
sudo systemctl daemon-reload
sudo systemctl restart servicename
For cron jobs in the user’s crontab, you may need to recreate them:
sudo crontab -u newusername -e
Post-Change Verification
After completing the username change, thorough testing ensures everything works as expected.
Testing the new username
- Log in with your new username
- Verify access to your files and applications
- Check system integration:
id groups ls -la ~
- Test sudo access:
sudo whoami
- Verify application functionality, especially those with user-specific configurations
Troubleshooting common issues
If you encounter issues after logging in:
- Check file permissions in your home directory
- Verify environment variables with
env | grep HOME
- Check for error messages in system logs:
journalctl -b | grep -i error
- Review application-specific logs for errors related to file paths or permissions
Advanced Scenarios and Solutions
Some situations require additional considerations beyond the standard username change process.
Handling encrypted home directories
If your home directory is encrypted using Ubuntu’s built-in encryption (ecryptfs), changing your username becomes more complex. You’ll need to:
- Backup all data from the encrypted home
- Complete the username change as described earlier
- Set up encryption for the new user
- Restore the data to the newly encrypted home
The specific commands depend on your Ubuntu version and encryption setup. Consider whether decrypting temporarily might simplify the process.
Multi-user systems and shared resources
On systems with multiple users, changing a username requires additional considerations:
- Check for shared groups and update as needed:
grep newusername /etc/group
- Update permissions for shared directories:
sudo find /shared -group oldusername -exec chgrp newusername {} \;
- Notify other users about the change, especially if they reference your home directory in scripts or configurations
- Update ACLs if used:
sudo getfacl -R /path/to/shared | sed "s/oldusername/newusername/g" | sudo setfacl --restore=-
Best Practices and Tips
Experience from system administrators provides valuable insights for username changes.
When to change username vs. create new user
Consider creating a new user instead of changing an existing username when:
- The existing user has complex configurations that would be difficult to migrate
- You want to keep the old account for reference or compatibility
- You need to maintain different permission sets
- The system has many customizations tied to the username
Creating a new user is simpler but requires manual data migration:
sudo adduser newusername
sudo usermod -aG sudo newusername
sudo rsync -av /home/oldusername/ /home/newusername/
sudo chown -R newusername:newusername /home/newusername/
Documentation and record-keeping
Throughout the username change process, maintain detailed notes:
- Commands executed and their output
- Any errors encountered and their solutions
- Files and configurations modified
- Before and after states of critical system components
This documentation proves invaluable if you need to troubleshoot issues later or replicate the process on another system.
Troubleshooting Guide
Even with careful preparation, issues may arise during or after a username change.
Common errors during username change
- “User is currently used by process” error:
Solution: Find and terminate all processes running as the old user
sudo ps -u oldusername sudo pkill -9 -u oldusername
- “Cannot lock /etc/passwd” error:
Solution: Check for other processes trying to modify user information
sudo lsof /etc/passwd
- Home directory not moved correctly:
Solution: Manually move and update ownership
sudo mv /home/oldusername /home/newusername sudo chown -R newusername:newusername /home/newusername
- “Authentication failure” when trying to log in:
Solution: Verify password hasn’t been inadvertently changed
sudo passwd newusername
Recovery options if things go wrong
If you encounter serious issues:
- Log in with the temporary admin account
- Restore from your backup if available
- For unbootable systems, use Ubuntu recovery mode or a live USB
- As a last resort, reverse the username change:
sudo usermod -l oldusername newusername sudo groupmod -n oldusername newusername sudo usermod -d /home/oldusername -m oldusername