How Do I Change My Username on Linux


Linux being a widely used Operating System offers strong user management schemas. The administrators have to Change Username on Linux, files, access, and much more to keep the system smooth. For this, command line support of Linux is exercised the most as it works irrespective of the distribution you are using. In this Linux guide, we will shed some light on the methods to change the username.

Approach #1: Change Username Using the usermod Command

Basically, the usermod command is used to modify the user details. All the user details in Linux are stored in five files, i.e., “/etc/passwd”, “/etc/group”, “/etc/shadow”, “/etc/login.defs”, “/etc/gshadow”, “/etc/login.defs”. The usermod command manipulates the details in the above-stated files to perform the directed operation.

Change the Username Only

The syntax of the command to change the username using the usermod is listed below.

$ usermod -l [New-Name] [Old-Name]

Here, the “New-Name” represents the new username that you want to replace with the “Old-Name”.

Let’s say we want to change the username to “linux-genie” which does not exist at the time as shown below.

Change Username on Linux

A user named “linux” already exists on our system, here, we will change the username from “linux” to “linux-genie” using the following usermod command.

# usermod -l linux-genie linux

Let’s verify this by switching the user to “linux-genie”.

# su linux-genie

The output shows that the user has been switched, which assures that the username has been changed.

It is noticed that the usermod command only changes the username, but the home directory, user id, and group id of the user renames the same. If you are intended to change all these, then stick with this guide we are going to take you all there.

Change the UID of the User

The User ID is the unique identifier of the user. The command used to change the UserID of the username is provided below.

# usermod -u UID [User-Name]

Here, the UID represents the new username and the [User-Name] represents the name of the user whose ID is to be changed.

Let’s get the User ID of the username, i.e., linux-genie.

# id linux-genie

In this case, the User ID of the user is “1004”. If you see the gid and groups refer to the user “linux” which is not changed with the username.

Now, let’s change the UID of the username “linux-genie” from “1009”.

# usermod -u 1009 linux-genie

Let’s verify the UID using the id command.

# id linux-genie

The UID has been changed to “1009”.

Change the Group

First, add a new group using the command.

# groupadd linux-genie

Graphical user interface, text Description automatically generated

 

Now, add the user linux-genie to the group linux-genie. The “g” flag of the usermod command changes the primary group of the user.

# usermod -g linux-genie linux-genie

Graphical user interface, text Description automatically generated with medium confidence

Verify the change via the id command as follows.

# id linux-genie

Graphical user interface, website Description automatically generated

The output shows that the primary group of the username linux-genie has been changed to linux-genie.

Change the Home Directory of the User

The usermod does it all for you. The home directory of the user will be changed to “/home/linux-genie” and all the content of the current home directory will also be moved.

# usermod -d /home/linux-genie -m linux-genie

Text Description automatically generated

This was the complete method to change the username alongside all its essentials.

Approach #2: Change Username Using the /etc/passwd File

The “/etc/passwd” files contain all the details of the user including the username, user-id, group-id, and home directory. These files are extremely crucial to manipulate as a minor mistake may lead to a system halt. What’s the solution then?

Here it is.

First, create the backup of these files as we did here.

# cp /etc/passwd /etc/passwd.bak

Text Description automatically generated

Example:

Access the “/etc/passwd” file using the nano editor as follows.

# nano /etc/passwd

When the file is opened, scroll down and you will get the details of the user as shown above. The first column shows the username followed by the UID, GroupID, and the home directory.

After doing so, you need to change the password of the username as well. For this, you need to use the command written below.

# passwd linux-genie-user

The password can also be changed by manipulating the “/etc/shadow” file, which is highly not recommended.

Note: We changed the user-id and the group in the above method. Here, it can also be done by accessing the “/etc/passwd” file in the same way as we did to change the username.

Wrap-Up

The username can be changed using the usermod command and by manipulating the “/etc/passwd” file. The “/etc/passwd” file is risky to access but it works too. The most recommended and suitable method is the usermod command which allows you to change the username alongside the UID, group ID, and much more as described in this post.

Print Friendly, PDF & Email
Categories

1 comment

Add yours

Comments are closed.