1. Introduction
There may be situations where you want to change your username in Ubuntu. Some possible reasons include:
- Organizing your system
- Privacy protection and security reasons
- Aligning with new naming conventions or project requirements
Changing a username may seem simple, but it requires careful steps. If done incorrectly, you may lose access to your system or experience permission issues.
This guide provides a step-by-step explanation on how to safely and correctly change your username in Ubuntu, targeting beginners and intermediate users. By following this guide, you can master the process without compromising your system.
2. Preparation
Checking Administrator Privileges
To change your username, you need administrator privileges (sudo access). You can check if your current user has administrative privileges with the following command:
id
If the output looks like the following, it means you have administrative privileges:
uid=1000(john) gid=1000(john) groups=1000(john),27(sudo)
Tip: Ensure that sudo
appears in the groups
list.
System Backup Recommendation
Changing your username can affect the entire system, so it is highly recommended to create a backup. Below is an example command to compress and back up your home directory:
sudo tar -cvpzf /path/to/backup/home-backup.tar.gz /home/your-username
Important: Store the backup file in a secure location. If any issues arise, you can restore your original state from this backup.
Impact of Username Changes
Changing your username may affect the following configurations and applications:
- SSH keys and authentication settings
- Scheduled
crontab
tasks in the system - Environment variables containing file paths or scripts
Check these settings in advance and back them up if necessary.
3. Steps to Change Your Username
Step 1: Create a New Administrator User
If you are changing your current username, 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 new user, log in with it and proceed to the next step.
Step 2: Log Out and Stop Processes for the Old User
To avoid errors, you must log out the old user and terminate their processes.
sudo pkill -u old-username
Verification: To confirm that the processes have stopped, run:
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-name old-group-name
Example:
If the old username is “john” and the new username is “doe”:
sudo usermod -l doe john
sudo groupmod -n doe john
Step 4: Change the Home Directory
After changing the username, you also need to rename the home directory.
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
To ensure the new user can access their home directory properly, adjust ownership settings:
sudo chown -R new-username:new-group-name /home/new-username
Example:
sudo chown -R doe:doe /home/doe
Step 6: Verify Changes
Confirm that the changes were applied correctly.
cat /etc/passwd | grep new-username
ls -l /home
Result: Ensure that the new username and home directory appear correctly.
4. Warnings and Troubleshooting
Warnings
1. Ending the Login Session
Before changing the username, ensure that the user is logged out. If the user remains logged in, the changes may not apply correctly.
How to check:
who | grep old-username
2. SSH Connection Issues
Changing the username also affects SSH configurations, such as the ~/.ssh/authorized_keys
file. If the file still references the old username, SSH connections may fail.
Solution:
- Move the
.ssh
folder to the new username’s home directory. - Check and update the file permissions.
sudo chown -R new-username:new-group-name /home/new-username/.ssh
chmod 700 /home/new-username/.ssh
chmod 600 /home/new-username/.ssh/authorized_keys
3. Scheduled Tasks (Crontab) Issues
After changing the username, scheduled tasks in crontab
may stop working.
How to check:
sudo crontab -u old-username -l
How to fix:
- Reconfigure tasks under the new username.
sudo crontab -u new-username -e
Troubleshooting
1. Error: Permission denied
Issue: “Permission denied” appears when running a command.
Cause: Insufficient privileges.
Solution:
- Make sure to use
sudo
:
sudo usermod -l new-username old-username
2. Error: user is currently used by process
Issue: This error appears during the username change.
Cause: The old user is still running processes.
Solution:
- Terminate running processes.
sudo pkill -u old-username
- Verify that processes have stopped.
ps -u old-username
3. Unable to Log In After Changing Username
Issue: You cannot log in after changing your username.
Cause: The new username or password may not be set correctly.
Solution:
- Log in with another administrator account and check settings.
- Manually edit the
/etc/passwd
file to verify and correct username settings.
sudo nano /etc/passwd
4. Home Directory Not Recognized
Issue: The home directory does not match the new username.
Cause: The usermod
command was not used correctly.
Solution:
- Reconfigure the home directory.
sudo usermod -d /home/new-username new-username
sudo chown -R new-username:new-group-name /home/new-username
5. FAQ
Q1. What should I do if my system doesn’t work properly after changing my username?
A:
Boot into recovery mode and manually edit the /etc/passwd
or /etc/group
file.
Example: Verify the username in /etc/passwd
.
Q2. Do I need to regenerate SSH keys?
A:
No, you can use your existing SSH keys. However, ensure they are correctly placed in the new user’s .ssh
directory with proper ownership and permissions.
Q3. Will this affect environment variables?
A:
Yes. If any scripts or settings in ~/.bashrc
or ~/.profile
reference the old username, you need to update them to reflect the new username.
Q4. What if there are multiple users on the system?
A:
To avoid affecting other users, make sure the changes only apply to the target user.

6. Conclusion
Changing a username in Ubuntu may seem complex, but with proper preparation and careful steps, it can be done safely and successfully. In this guide, we covered key points for beginners and intermediate users to maintain system stability while changing their username.
Key Takeaways from This Guide
- The Importance of Preparation
Confirming administrator privileges and backing up your system before making changes can prevent potential issues. Creating a backup of the home directory using thetar
command is an essential step. - Step-by-Step Process
We provided detailed instructions on creating a new administrator user, renaming the username, and changing the home directory. Each step includes command examples and expected outputs for clarity. - Warnings and Troubleshooting
We addressed common errors (e.g.,Permission denied
,user is currently used by process
) and provided practical solutions to help users resolve issues efficiently. - FAQ for Additional Support
We answered common concerns related to SSH configurations, environment variables, and system compatibility after a username change.
Next Steps
After changing your username, verify that your system functions correctly:
- Test Logging In
Try both SSH and local logins to ensure there are no errors. - Update Scripts and Tasks
Check scheduled tasks and scripts that reference the old username. - Keep Your Backup
Retain your backup until you are sure the system is functioning as expected.
We hope this guide helps you manage your Linux system smoothly and efficiently!