How to Change or Rename Username and UID in Linux

How to Change or Rename Username and UID in Linux
How to Change or Rename Username and UID in Linux

Change or Rename Username and UID in Linux

In some cases, we need to change the username of the system or the user id (UID) of the system or the both. In Linux there are lot configurations depends on user id like permissions. By using command usermod you can change the username of your system. After changing the Username of UID the changes will affect systemwide. In this tutorial, you are going to learn how to change or rename username and UID in Linux.

Prerequisites

Before you start to change or rename username and UID in Linux. You must have the non-root user account on your system with sudo privileges.

List all users in Linux

Before you going to change the username or user id of your Linux system you should know all the available users on your system.

To list all the users use following command in Linux system:

cat /etc/passwd

If You want to find a specific user then use grep command combining above command:

cat /etc/passwd | grep USERNAME_TO_FIND

The output should look like:

gnats:x:41:41:Gnats Bug-Reporting System (admin):/var/lib/gnats:/usr/sbin/nologin
admin:x:1000:1000:admin:/home/linux4one:/bin/bash

If you didn’t get any results means the specified user is not available.

NOTE: Do not change /etc/passwd file with any text editor ever.

Change User Name

You can change username of the system using usermod command in Linux system. To change username you should use username command with -l option.

The command to change the username of the system is given below:

sudo usermod -l NEW_USERNAME OLD_USERNAME

For example, if you want to change the username from john to sara then use below command:

sudo usermod -l sara john

You may get the following message:

usermod: user john is currently used by process 1532

If you get above output then it means that all the processes owned by user john should be killed. To kill the above 1532 process owned by user john run below commands.

 pkill -u john 1532

In above command 1532 taken from output of sudo usermod -l sara john so the process id may be changed on your system.

To kill all the processes owned by user john run below commands.

pkill -9 -u john

After using above command your systems username will be changed permanently. Now you may need to restart the system.

Change User ID (UID)

You can change UID of the system user using usermod command in Linux system. To change UID you should use username command with -u option.

Following is the basic syntax to change UID of the user.

usermod -u [NEW_USER_ID] [USERNAME]

To change UID of user john run following command:

usermod -u 10000 john

Conclusion

You have successfully learned how to change or rename username and UID in Linux. If you have any queries regarding this then please don’t forget to comment below.