The Two Ways to Change Your Linux Username
You can change your Linux username in two ways: rename your user account while keeping the same home directory and files, or create a new account and transfer everything over. The first method is faster and keeps all your permissions intact. The second method is safer if something goes wrong, because your old account still exists until you delete it.
Which one you choose depends on whether you are logged in as that user right now. If you are, you must use the second method — you cannot rename an account while you are using it. If you are logged in as root or a different user, you can use either method.
Key Takeaways
- You cannot rename your own username while you are logged in as that user; you must switch to root or another account first.
- The usermod command renames an account and keeps your home directory and files unchanged.
- The usermod -d option also moves your home directory to a new location if you want to change that at the same time.
- After renaming, check that your files still have the correct ownership using the ls -l command in your home directory.
- If you are logged in as the user you want to rename, create a new account, copy your files, and delete the old one instead.
Renaming Your Account While Logged In as Root
Open a terminal and type usermod -l newusername oldusername, replacing newusername with what you want and oldusername with your current name. For example, usermod -l james rodriguez changes the username from james to rodriguez. Press Enter and the change takes effect when ready.
Your home directory keeps its old name by default. If your old username was james, your home folder is still called /home/james even though your username is now rodriguez. Most people leave it this way, but if you want to rename the directory too, use usermod -d /home/newusername -m oldusername instead. The -m flag tells the system to move the directory and update the ownership.
After the change, log out and log back in to make sure everything works. Open a terminal and type whoami to confirm your new username appears. Then type ls -l in your home directory to check that your files still show the correct owner.
Renaming Your Account While Logged In as That User
If you are logged in as the user you want to rename, you cannot use usermod directly. Instead, create a new account with your desired username, copy your files to it, and then delete the old account. This takes longer but is the only safe way when you are logged in as yourself.
First, open a terminal and switch to root by typing su or sudo -i, depending on your system. Then create a new user with useradd -m -s /bin/bash newusername, replacing newusername with what you want. The -m flag creates a home directory and -s /bin/bash sets your shell. Set a password with passwd newusername.
Next, copy all your files from the old home directory to the new one. Type cp -r /home/oldusername/* /home/newusername/ to copy everything. Then type chown -R newusername:newusername /home/newusername/ to make sure the new user owns all the files. Log out, log in as your new username, and test that everything works.
Once you confirm the new account has all your files and settings, delete the old one. Type userdel -r oldusername as root to remove the old account and its home directory. The -r flag deletes the directory too; without it, the folder stays behind.
Fixing File Ownership After a Rename
If you renamed your account but some files still show the old username as the owner, you can fix that in one command. Type chown -R newusername:newusername /home/newusername as root, replacing newusername with your new username. The -R flag updates the owner for the directory and everything inside it.
Check the results by typing ls -l in your home directory. The third column should show your new username next to each file. If it still shows a number instead of a name, the system does not recognize the new username yet — log out and back in, then try the chown command again.
Changing Your Username on Systems Without Root Access
Some systems, like shared servers or containers, do not give you root access. In that case, you cannot change your username through the system. Contact your system administrator or hosting provider and ask them to rename your account. Provide them with your current username and the new one you want.
If you manage your own system but do not have a root password, you can still make the change by booting into single-user mode or recovery mode, which gives you temporary root access. The steps vary by Linux distribution, so search for "[your distribution] single-user mode" if you need to go this route.
What Happens to Your Sudo Permissions When You Rename
If your old username was in the sudoers file (the list of users allowed to run commands as root), you need to update that file after renaming. Type visudo as root to open the sudoers file safely. Find the line with your old username and change it to your new one, then save and exit.
If you do not update the sudoers file, you will not be able to use sudo commands with your new username. You can still log in and use your account normally, but you will get a "user is not in the sudoers file" error if you try to run sudo. Fixing it takes just one edit, so do it right after you rename.
Frequently Asked Questions
Can I change my username without losing my files?
Yes. The usermod command changes only your username, not your home directory or files. Everything stays in place and keeps working. If you want to rename the home directory too, use the -d and -m flags together.
What if I forget to log out after renaming my account?
Your current session keeps working with the old username until you log out. Once you log out and log back in, the new username takes effect. Some programs running in the background might behave oddly, so restarting is a good idea after a rename.
Do I need to change anything in my applications after renaming?
Most applications do not care about your username and will work fine. If you have scripts or configuration files that hardcode your old username, you will need to update those manually. Search your home directory for your old username using grep -r oldusername . to find files that mention it.
Can I rename my username to something that already exists?
No. Linux will not let you create a username that is already taken. If you want to swap two usernames, rename the first one to a temporary name, then rename the second one to the first name, then rename the temporary one to the second name.
What does the error "user is currently logged in" mean?
It means you are trying to rename an account while that user is logged in somewhere. Log out of all sessions using that username, including any open terminals or remote connections, then try the rename again.