The usermod command is the standard way to change your username

To change your username in Linux, you use the usermod command with the -l flag, which stands for "login". The basic syntax is usermod -l newusername oldusername. You must run this command as root or with sudo privileges, because only the system administrator can modify user accounts.

Before you run the command, log out of the account you want to rename. You cannot change the username of an account you are currently logged into — the system will not allow it. Log in as root or as a different user with sudo access instead.

Here is the actual command you type:

sudo usermod -l james john

This changes the username from "john" to "james". The system updates the login name when ready, but the home directory name stays the same. If you want the home directory to match the new username, you need a second step.

Key Takeaways

  • Use sudo usermod -l newname oldname to change the username itself, and you must run it while logged out of that account.
  • The home directory does not rename automatically, so you need to move it separately with sudo mv /home/oldname /home/newname.
  • After renaming the home directory, update the home path in the user record with sudo usermod -d /home/newname username.
  • Always verify the changes worked by checking /etc/passwd and listing the contents of /home to confirm both the username and directory match.

Renaming the home directory to match the new username

When you change a username with usermod, the home directory keeps its old name. If your old username was "john" and you change it to "james", the directory is still called /home/john. This can cause confusion and permission problems later.

To rename the home directory, use the mv command:

sudo mv /home/john /home/james

This moves the entire directory and all its contents to the new name. After you move it, you must tell the system where the home directory is now. Use usermod again with the -d flag:

sudo usermod -d /home/james james

Now the username, home directory name, and home directory path all match. The user "james" will log in and land in /home/james as expected.

Checking that the change worked

After you make the changes, verify them by looking at the user record. The file /etc/passwd stores all user information. Open it with cat or grep to find your user:

grep james /etc/passwd

You should see a line that looks like this:

james:x:1000:1000::/home/james:/bin/bash

The username appears first, and the home directory path appears near the end. If both show the new name, the change is complete. You can also list the home directory to confirm the folder exists:

ls /home

You should see the new directory name in the list.

What to do if you only changed the username but not the home directory

If you ran usermod but forgot to rename the home directory, the user can still log in, but the home directory path will be wrong. The system will try to go to /home/james, but the folder is still named /home/john. The user may end up in the root directory or see permission errors.

To fix this, rename the directory now and update the path:

sudo mv /home/john /home/james

sudo usermod -d /home/james james

Then verify with grep again. The next time the user logs in, they will land in the correct home directory.

Changing the username back if you made a mistake

If you changed the username and realized you made an error, you can change it back using the same process. Log out of the account, then run usermod with the correct names:

sudo usermod -l john james

This changes "james" back to "john". If you also renamed the home directory, rename it back:

sudo mv /home/james /home/john

sudo usermod -d /home/john john

Verify the change with grep and ls as before.

Why you cannot change your own username while logged in

Linux prevents you from changing the username of an account you are currently using because the system needs to keep track of who owns running processes. If you changed your own username mid-session, the system would lose track of which processes belong to you, and file permissions could break.

The solution is straightforward: log out, log in as root or another admin account, and then run usermod. This is a safety feature, not a limitation of the command itself.

Frequently Asked Questions

What happens to files and folders owned by the old username?

Files keep their ownership by user ID number, not by name. When you rename the username, the ID stays the same, so all files remain owned by the same user. You can verify this by running ls -l in the home directory — the username shown will update to the new name automatically.

Do I need to change the username in any config files?

Most config files reference the user by ID, not by name, so they work automatically. However, if you have scripts or cron jobs that mention the old username by name, you should update those manually. Check your home directory for shell scripts and crontab entries.

Can I change the username of the root account?

Technically yes, but it is not recommended. The root account is special, and changing its username can break system tools and scripts that expect it to be called "root". If you need a different admin account, create a new user and give it sudo access instead.

What if the home directory is on a different partition or mounted elsewhere?

The mv command works the same way. As long as you have the full path correct, you can move the directory. If the directory is mounted as a separate filesystem, make sure you have enough space on the destination before you move it.