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
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
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.
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.
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
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
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.