Changing your username requires stopping all active sessions and using the usermod command

To change a username on Ubuntu Server, you log in as root or with sudo access, stop any processes running under that user account, then run usermod -l newusername oldusername. You will also need to rename the user's home directory from /home/oldusername to /home/newusername, and update the group name if it matches the old username. The entire process takes about five minutes and does not require a reboot, but you cannot be logged in as the user you are changing.

This is different from changing your password, which you can do yourself at any time. Changing your username affects login credentials, file ownership, and any scripts or cron jobs that reference the old name. If you are the only user on the server and you lock yourself out, recovery is possible but requires either physical access or a backup authentication method.

Key Takeaways

  • You must have root or sudo access to change any username, and the user whose name you are changing cannot be logged in during the process.
  • The usermod command changes the login name, but you must separately rename the home directory and update the group name to match.
  • Any cron jobs, systemd services, or scripts that reference the old username by name will break and need to be updated manually.
  • If you are changing your own username, you will need to log back in with the new name after the change is complete.

Prerequisites and safety checks before you start

Before you change a username, verify that you have sudo access or can log in as root. Open a terminal and run sudo -l to see what commands you can run. If you see a long list of allowed commands, you have the access you need. If you see "not in the sudoers file", you cannot make this change without physical access to the server or help from someone who does have sudo access.

Check whether the user account you want to change is currently logged in. Run who or w to see active sessions. If the user is logged in, ask them to log out, or use pkill -u oldusername to end their sessions. If you are changing your own username, log out completely before proceeding — do not try to change your username while you are still logged in as that user.

Make a note of the user's current UID (user ID number) and GID (group ID number). Run id oldusername to see these. You will not need to change them, but knowing them helps you verify that file ownership is correct after the change.

The usermod command to change the login name

Once you have confirmed that the user is not logged in, run the usermod command to change the username. The syntax is:

sudo usermod -l newusername oldusername

Replace newusername with the new login name you want, and oldusername with the current name. For example, to change a user named "alex" to "alexandra", you would run sudo usermod -l alexandra alex. The command produces no output if it succeeds. If you see an error like "user alex is currently used by process", it means the user is still logged in or has a running process — go back and stop those sessions first.

The usermod command changes the name in /etc/passwd, which is the file that the system uses to look up usernames. It does not change the home directory path or the primary group name. Those are separate steps.

Rename the home directory to match the new username

After usermod completes, the user's home directory still has the old name. You must rename it so that the system can find it. Run:

sudo mv /home/oldusername /home/newusername

For the example above, this would be sudo mv /home/alex /home/alexandra. The mv command moves (renames) the directory. Verify that it worked by running ls -la /home/ and checking that the new directory name appears and the old one is gone.

If the home directory is in a non-standard location (for example, /opt/users/alex instead of /home/alex), adjust the path accordingly. You can find the correct path by running grep oldusername /etc/passwd before you make any changes — the sixth field in the output is the home directory path.

Update the primary group name if it matches the old username

By default, Ubuntu creates a primary group with the same name as the user. If you changed the username from "alex" to "alexandra", there is still a group called "alex" that owns the home directory. You should rename this group to match the new username for consistency.

First, check whether a group with the old username exists by running grep oldusername /etc/group. If you see a line with the old username, run:

sudo groupmod -n newusername oldusername

For the example, this would be sudo groupmod -n alexandra alex. Verify the change by running grep alexandra /etc/group — you should see a line with the new group name. If no group with the old username existed, you can skip this step.

Update file ownership to reflect the new username

After you rename the home directory and the group, the files inside still have the old username and group name in their ownership records. This does not break anything when ready, but it can cause confusion and problems later. Update all files in the home directory to use the new username and group:

sudo chown -R newusername:newusername /home/newusername

For the example, this would be sudo chown -R alexandra:alexandra /home/alexandra. The -R flag means "recursive", so it updates the directory and everything inside it. This command produces no output if it succeeds.

Verify the change by running ls -la /home/newusername and checking that the owner and group columns show the new username instead of a number or the old name.

Find and update scripts, cron jobs, and services that reference the old username

If the user had any cron jobs, systemd services, or scripts that reference the old username by name, those will break after the change. Search for references to the old username in common locations:

grep -r oldusername /etc/cron.d/ grep -r oldusername /etc/systemd/system/ grep -r oldusername /home/newusername/

For each file that contains the old username, open it with a text editor and replace the old username with the new one. For example, if a cron job runs a script as the old user, change * * * * * oldusername /path/to/script.sh to * * * * * newusername /path/to/script.sh. After you edit a cron file in /etc/cron.d/, the system picks up the change automatically. After you edit a systemd service file, run sudo systemctl daemon-reload to reload the configuration.

Verify the change is complete

Log in as the new user to confirm that everything works. If you changed your own username, log out of your current session and log back in with the new username and your existing password. Run whoami to confirm that you are logged in as the new username, and run pwd to confirm that your home directory path is correct.

Check that your SSH key (if you use one) is still in the correct location. SSH looks for keys in /home/newusername/.ssh/authorized_keys. If you moved the home directory correctly, the keys should be there already. If you use key-based authentication and cannot log in, check that the .ssh directory and authorized_keys file have the correct permissions by running ls -la /home/newusername/.ssh/ — the directory should be 700 and the file should be 600.

If you are changing a different user's username and you want to verify as root, run sudo -u newusername whoami to confirm that the user account works correctly.

Frequently Asked Questions

What happens if I change my own username and then get locked out?

If you cannot log in with the new username, you can still log in as root (if you have physical access or a recovery method) and undo the change by running usermod again with the old and new names reversed. If you do not have root access and cannot log in, you will need physical access to the server or help from someone who does have root access.

Can I change a username without changing the home directory path?

Technically yes, but it creates confusion and breaks the normal expectation that /home/username belongs to the user named username. It is better to rename the directory to match. If you have a specific reason to keep the old path, you can update /etc/passwd manually with a text editor, but this is not recommended.

Do I need to restart the server after changing a username?

No. The changes take effect when ready. You do not need to reboot. However, if the user has running processes or services, those may need to be restarted to pick up the new username in their logs or configuration.

What if the username I want already exists?

The usermod command will refuse to change to a username that is already in use. You must either delete or rename the existing user first, or choose a different new username. Run grep newusername /etc/passwd to check whether the name is already taken.

How do I change a username for a system user or service account?

The process is the same, but be extra careful. System users often have UID numbers below 1000 and may be tied to specific services. Before you change a system user's name, check the documentation for that service to see whether it expects a specific username. If a service is hardcoded to look for a user named "www-data" or "postgres", changing the username will break the service.