Code360 powered by Coding Ninjas X Naukri.com. Code360 powered by Coding Ninjas X Naukri.com
Table of contents
1.
Introduction
2.
How to Change Username in Linux
2.1.
Preparing for the Username Change:
2.1.1.
1 Backup Your Data:
2.1.2.
2 Administrative Access:
2.2.
Changing the Username
2.2.1.
1. Log in as Root or Use sudo:
2.2.2.
2. Change the Username:
2.2.3.
3. Update the Home Directory:
2.3.
Post-Username Change Steps
2.3.1.
1. File Ownership and Permissions:
3.
Changing Username while Logged in as the User
4.
Important Points before Changing Username
5.
Changes needed after Updating the Username
6.
Frequently Asked Questions
6.1.
What function does a username in Linux serve?
6.2.
What characters may I use as a username in Linux?
6.3.
Where the Linux system stores the username?
7.
Conclusion
Last Updated: Mar 27, 2024
Easy

How to Change Username in Linux?

Author Rinki Deka
0 upvote
Roadmap to SDE career at Amazon
Speaker
Anubhav Sinha
SDE-2 @
25 Jun, 2024 @ 01:30 PM

Introduction

In Linux, the username is an essential aspect of user identification and plays a crucial role in various system operations. Changing your username may be necessary for security reasons, personal preference, or system management purposes. In this article, we will walk you through the steps to change your username in Linux, ensuring a smooth transition while preserving your user data and permissions.

How to Change Username in Linux?

How to Change Username in Linux

Step-by-Step Guide to Changing Username in Linux:

Preparing for the Username Change:

Before proceeding with the actual username change, it's important to take a few preparatory steps to ensure a successful transition.

1 Backup Your Data:

Create a backup of all your important files and data associated with your current username. This step ensures that your data remains safe in case of any unexpected issues during the username change process. 

2 Administrative Access:

Ensure that you have administrative access to the system or have the necessary permissions to modify user accounts. Without appropriate privileges, you won't be able to change usernames.

Changing the Username

Once you have completed the preparation, follow these steps to change your username in Linux:

1. Log in as Root or Use sudo:

To change a username, you need to have administrative access. Log in as the root user or use the sudo command to run the necessary commands with elevated privileges.

2. Change the Username:

Open a terminal and run the following command:

sudo usermod -l new_username old_username

 

Replace new_username with your desired new username and old_username with your current username. This command modifies the username in the system.

Change the Username

3. Update the Home Directory:

Next, run the following command to update the home directory associated with the new username:

sudo usermod -d /home/new_username -m new_username

 

This command ensures that your home directory is correctly updated to match the new username.

Post-Username Change Steps

After changing your username, it is essential to update certain system configurations and settings to reflect the new username. Here are a few areas that require attention:

1. File Ownership and Permissions:

Update file ownership and permissions to match the new username. Run the following command to recursively change ownership:

sudo chown -R new_username: /home/new_username

 

This command ensures that all files and directories in the home directory are owned by the new username. 

Get the tech career you deserve, faster!
Connect with our expert counsellors to understand how to hack your way to success
User rating 4.7/5
1:1 doubt support
95% placement record
Akash Pal
Senior Software Engineer
326% Hike After Job Bootcamp
Himanshu Gusain
Programmer Analyst
32 LPA After Job Bootcamp
After Job
Bootcamp

Changing Username while Logged in as the User

You can change your username while logged in as the user; however, some additional steps are required to ensure a seamless transition. Follow these steps to change your username while logged in:

Step 1: Open a terminal.

Step 2: Run the command 

sudo usermod -l new_username old_username

 

Replace new_username with your desired new username and old_username with your current username.

Step 3: Run the command 

sudo usermod -d /home/new_username -m new_username

 

This command ensures that your home directory is correctly updated to match the new username. 

Please note that after changing your username, you need to log out and log back in for the changes to take effect.

Important Points before Changing Username

Before changing your username, it is important to consider the following precautions:

1. Create a backup: Make sure to back up all important files and data associated with your user account before making any changes. This ensures that your data is safe in case of any unexpected issues during the username change process.

2. Administrative access: Ensure that you have administrative access to the system or have the necessary permissions to modify user accounts. Without appropriate privileges, you won't be able to change usernames.

3. Check system services: Some system services or applications may be tied to your username. Before changing your username, verify if any such dependencies exist and make the necessary updates to ensure their continued functionality.

Changes needed after Updating the Username

After changing your username, it is essential to update certain system configurations and settings to reflect the new username. Here are a few areas that require attention:

1. Home directory: As mentioned earlier, the home directory needs to be updated to match the new username. This ensures that your user data and configuration files are correctly associated with the updated username.

2. File ownership and permissions: Update file ownership and permissions to match the new username. Run the command sudo chown -R new_username: /home/new_username to recursively change ownership.

3. System configurations: Some system configurations may store your old username. Check and update any relevant configuration files in /etc or other system directories to reflect the new usernames.

Frequently Asked Questions

What function does a username in Linux serve?

The username serves as an identifier for you to the computer. A password, on the other hand, serves as an authenticator, allowing you to show the operating system that you are who you say you are.

What characters may I use as a username in Linux?

On UNIX and Linux operating systems, user IDs can have up to 8 characters. On Windows, user names can have up to 30 characters.

Where the Linux system stores the username?

Linux usernames are saved in a file called as "/etc/passwd."  In this file, each line contains the data about a user including their username, user ID number (UID), home directory, and the login shell.  

Check this out: Touch command in linux

Conclusion

Changing your username in Linux is a straightforward process but requires careful consideration and attention to detail. By following the steps outlined in this guide, you can safely change your username while preserving your data and ensuring a smooth transition. Remember to take appropriate precautions, back up your data, and update relevant system configurations to reflect the new username. With these steps, you can personalize your Linux system to suit your preferences or maintain the security of your user account.

Recommended Reading: 

gzip command in linux

Previous article
YUM in Linux
Next article
Difference Between Hard Link and Soft Link in Linux
Live masterclass