How to Change a Username in Linux

How do I change or rename a username in Linux? This brief guide will show you how to do it properly using command-line tools.

As you know, Linux is a multi-user operating system. Therefore, a unique username is assigned to each account on the system. This is the name with which the user can log into the system.

However, sometimes you might have come across a situation where you want to rename a user name in a Linux system, for whatever reason.

Fortunately, Unix-like operating systems decouple the user name from the user identity, so you may safely change the name without affecting the ID. All permissions, files, etc., are tied to your identity (UID), not your username.

How to Change a Username in Linux

Changing the username consists of two phases, one in which we only change the name and the other in which we also change the UID or user identifier.

You need to use the usermod command to change a username under a Linux operating system. It is used to modify or change any attributes of an already created user account via the command line.

The system account files are modified by this command to reflect the changes supplied on the command line.

The usermod command can only be used by the root user or by a user with sudo privileges. The root user can modify every account that accesses the operating system.

However, before changing the username or user ID (UID), we must first understand the username or user ID.

Linux operating system identifies a user by a value called a user identifier (UID). It is a number assigned by Linux to each user on the system.

The UID, group identifier (GID), and other access control criteria determine which system resources a user can access.

The details of a user, such as UID, username, the complete name of the user, default shell, etc., are stored in the /etc/passwd file. Remember, do not edit /etc/passwd file by hand! Instead, always use a command that is designed for the purpose.

For this guide, let’s assume we want to change the username bobby to nadia. To get the UID or other details of a user, you can use the following command:

cat /etc/passwd | grep bobby
/etc/passwd file

Remember to log out from the account you are trying to rename. Either by simply logging out or by killing all the processes running for that user:

sudo pkill -9 -u bobby

Next, to change the username in Linux, weโ€™ll use the usermod command and the -l parameter to change a particular user’s username. The syntax is as follows:

sudo usermod -l new_username old_username

Therefore, to change the username bobby to nadia, the command should look like this:

sudo usermod -l nadia bobby
Change a Username in Linux

This command will change the username bobby to nadia but will not change the files and UID of the user.

Changing the Default Home Directory

Now weโ€™re going to change the userโ€™s home directory.

Changing the Default Home Directory

To move the content of the current /home/bobby directory, along with changing the home directory to /home/nadia, the command should look like this:

sudo usermod -m -d /home/nadia nadia

In the above command, we need to use the usermod command with the -d option to change the userโ€™s home directory and the -m option to move all contents from /home/bobby to the /home/nadia directory.

Changing the Default Home Directory

How to Change UID in Linux

To change the UID, we’ll use the usermod command and the -u parameter to change the user ID of a particular user. The syntax is as follows:

sudo usermod -u [UID] [username]Code language: CSS (css)

Remember that values between 0 and 99 are reserved for system accounts. The file user ID of any files owned by the user and located in the user’s home directory will be modified automatically. However, files outside of the userโ€™s home directory must be altered manually.

Now, let’s change the UID of the user nadia to 1169.

sudo usermod -u 1169 nadia
Change user ID in Linux

Renaming a Userโ€™s Group

Every user on a Linux system is created with a group of the same name. When we update a user’s name, we don’t change their group name. However, it’s a good idea also to change the user’s primary group name.

We use the groupmod command with the -n flag to change a user’s primary group name. We must provide both the old and new names.

Therefore, we would run the following command to rename the newly renamed user nadia‘s primary group to nadia from bobby.

sudo groupmod -n nadia bobby
Renaming a Userโ€™s Group

Conclusion

Using the above method, you can easily change the username or UID in your Linux system. I hope you find this post valuable and informative.

Please find out more about various options in usermod on its command line manual page.

Bobby Borisov

Bobby Borisov

Bobby, an editor-in-chief at Linuxiac, is a Linux professional with over 20 years of experience. With a strong focus on Linux and open-source software, he has worked as a Senior Linux System Administrator, Software Developer, and DevOps Engineer for small and large multinational companies.

Think You're an Ubuntu Expert? Let's Find Out!

Put your knowledge to the test in our lightning-fast Ubuntu quiz!
Ten questions to challenge yourself to see if you're a Linux legend or just a penguin in the making.

1 / 10

Ubuntu is an ancient African word that means:

2 / 10

Who is the Ubuntu's founder?

3 / 10

What year was the first official Ubuntu release?

4 / 10

What does the Ubuntu logo symbolize?

5 / 10

What package format does Ubuntu use for installing software?

6 / 10

When are Ubuntu's LTS versions released?

7 / 10

What is Unity?

8 / 10

What are Ubuntu versions named after?

9 / 10

What's Ubuntu Core?

10 / 10

Which Ubuntu version is Snap introduced?

The average score is 69%