Changing your Unix username requires using the `usermod` command as the root user or with sudo privileges
On a Unix system, your username is the name you use to log in and is stored in the system's user database. Changing it is different from changing your display name in an process — you are modifying the account itself at the operating system level. The process involves a single command, but you need administrative access to run it, and you should plan ahead because you cannot be logged in as that user while you change it.
The command you will use is usermod, which modifies user account properties. The basic syntax is usermod -l newusername oldusername, where -l stands for "login name". You must run this command as root or with sudo, and the user account you are changing cannot be actively logged in or running processes.
Key Takeaways
- You must have root or sudo access to change a Unix username using the usermod command.
- The user account you are changing cannot be logged in or have active processes running at the time of the change.
- Changing the username does not automatically change the home directory path, so you may need to rename that separately with the mv command.
- After changing the username, file and directory ownership may still point to the old username, and you can update those with the chown command if needed.
Preparing to change your username
Before you run the usermod command, make sure the user account is not logged in anywhere. If you are changing your own username, you must do this from a different account with sudo access, or from the root account directly. Open a terminal and log in as root or as a user with sudo privileges.
Check whether the user you want to change has any running processes by typing ps aux | grep oldusername. If processes appear in the output, the user is still active. Ask them to log out, or if you are changing your own account, make sure you are logged in as a different user first. You should also verify that the new username you want does not already exist on the system by typing id newusername — if the command returns "no such user", the name is available.
Running the usermod command to change the login name
Once you have confirmed the account is not logged in and the new username is not taken, type the usermod command. If you are using sudo, the full command is:
sudo usermod -l newusername oldusername
If you are logged in as root, omit the sudo part and type:
usermod -l newusername oldusername
Replace oldusername with the current username and newusername with what you want it to be. Press Enter. If the command completes without printing an error, the username has been changed. You can verify this by typing id newusername — it should now return the user's ID and groups.
Renaming the home directory to match
Changing the username does not automatically rename the home directory. If the user's home folder was at /home/oldusername, it will still be there after the username change. Most systems will still work, but it is cleaner to rename the directory to match the new username.
Type the following command to rename the home directory:
sudo mv /home/oldusername /home/newusername
After this, you should also update the user's home directory setting in the system database. Type:
sudo usermod -d /home/newusername newusername
This tells the system that the user's home directory is now at the new path. The next time the user logs in, the system will use the correct home directory.
Updating file ownership after the username change
Files and directories that the old user owned will still show the old username as the owner until you update them. This is usually not a problem for the system to function, but it can be confusing when you list files. To change ownership of all files in the user's home directory to the new username, type:
sudo chown -R newusername:newusername /home/newusername
The -R flag means "recursive", so it will change ownership of the directory and everything inside it. The format newusername:newusername sets both the user owner and the group owner. If the user belongs to a specific group other than their username, replace the second part with that group name.
If there are files owned by the old username in other locations on the system, you can find them by typing find / -user oldusername 2>/dev/null. This will search the entire system and hide error messages. For each directory or file that matters, run chown to update the owner to the new username.
Testing the username change
The safest way to confirm everything worked is to have the user log in with their new username. They should be able to type the new username at the login prompt and enter their password as usual. Once logged in, they can type whoami to confirm their username is now the new one, and pwd to confirm they are in the correct home directory.
If the user cannot log in, check that the home directory path is correct by typing getent passwd newusername as root. This will show the user's entry in the system database, including the home directory path. If the path is wrong, run the usermod command again with the -d flag to correct it.
Frequently Asked Questions
Can I change a username while the user is logged in?
No. The usermod command will fail or cause problems if the user has an active session. The user must log out completely, and you must verify there are no running processes under that username. If you are changing your own username, log in as a different user or as root first.
What if I change the username but forget to rename the home directory?
The system will still work, but the home directory path will not match the username. The user's home directory setting in the system database will point to the old path. You can rename it later by moving the directory and updating the home directory setting with usermod -d, as described in the "Renaming the home directory" section.
Do I need to change file ownership for everything to work?
No. File ownership is mostly cosmetic — the system uses user IDs internally, not usernames. However, updating ownership makes the output of commands like ls cleaner and prevents confusion about who owns what. It is a good practice but not required for the system to function.
Can I change a username back to what it was before?
Yes. Run the usermod command again with the old username as the new name. You can also rename the home directory back and update ownership the same way. There is no permanent record of the change beyond what you can see in the system database and file listings.
What if the new username I want is already taken?
The usermod command will fail with an error message. Choose a different username and try again. You can see all existing usernames on the system by typing cut -d: -f1 /etc/passwd, which will list every user account.