How to Change Username in Linux?

Learn via video courses
Topics Covered

Introduction

In the Linux operating system, usernames are essential for identifying and managing user accounts. A username is a unique identity for a user that allows them to log in, access files, and perform certain tasks on the system. However, there may be times when you need to change your username owing to personal preferences or security concerns.

Fortunately, Linux provides various options for changing usernames, giving flexibility and simplicity to both system administrators and users.

In this article, we'll look at two common approaches:

  • Using the usermod command
  • Using the chfn commands.

These solutions allow you to change usernames quickly while yet maintaining a well-organized user management system.

Change a Username on Linux Using the usermod Command

The usermod command is a powerful utility that allows system administrators to change the username and other attributes of a user account. You can modify a username with the usermod command by following a few simple steps.

  1. Open a Terminal: To begin, open a terminal on your Linux system. You can typically find the terminal application in the system's application launcher or by using the shortcut Ctrl+Alt+T.
  2. Check Existing Username: Before making any changes, it is essential to verify the current username of the account you wish to modify. This step ensures that you have accurate information and helps avoid any confusion during the renaming process. To check the current username, use the following command in the terminal:

Change a Username on Linux Using the usermod Command

  1. Change the Username: Once you have identified the account's current username, you can use the usermod command to change it. The usermod command allows you to modify various attributes of a user account, including the username. The syntax for the usermod command to change the username is as follows:

Replace "new_username" with the desired new username and "old_username" with the current username of the account. The 'sudo' command is used to execute the usermod command with administrative privileges.

Let's take an example to change the username from taran to tarandeep, for that the command will be:

Change Username Command

  1. Update the User's Home Directory and Group:
    After changing the username, it is crucial to update the user's home directory and group to reflect the new username. This step ensures consistency and prevents any conflicts or inconsistencies in the system. To update the user's home directory and group, use the following command:

In this command, replace "new_username" with the newly assigned username. The '-d' option specifies the new home directory path and '-m' ensures the user's files are moved to the new directory.

Change Username Command

5. Verify the Changes:

To confirm that the username has been successfully changed, you can use the following command:

The terminal should display the new username associated with the current session, indicating that the changes have taken effect.
Change Username Command

Change a Username on Linux Using the chfn Command

The chfn command, short for "change finger information," allows users to modify their account information, including the full name and login name (username). We can use this command to change the display name of the user based on the user's needs.

Here's how you can change a username using the chfn command:

  1. Open a Terminal: To begin, open a terminal on your Linux system. You can typically find the terminal application in the system's application launcher or by using the shortcut Ctrl+Alt+T.
  2. Update the Username: To update the display name of the logged-in user, use the chfn command with the -f flag and pass the full_name of the user:

Replace "FULL NAME" with the desired display name and "username" with the actual username of the user. The "-f" option is used to specify the new full name or display name.

Running this command with sudo privileges will update the display name associated with the user account. Make sure to replace "FULL NAME" and "username" with the appropriate values. Change Username Command

  1. Verify the Changes: Verify the changes by checking the logged-in user that the new Full name has been updated or not. Verify the Changes

Managing User Information on Linux

Managing user information on Linux involves several tasks, including creating new users, modifying user details, and deleting user accounts.

Here are some pointers to help you manage user information effectively:

  • Creating a User:
    • Use the useradd command to create a new user. For example: sudo useradd -m newusername.
    • The -m flag ensures that a home directory is created for the user.
  • Modifying User Details:
    • Use the usermod command to modify user details. For example: sudo usermod -l newusername oldusername to change the username.
    • The -l flag specifies the new username.
  • Changing User Password:
    • Use the passwd command to change a user's password. For example: sudo passwd username.
    • Follow the prompts to enter and confirm the new password.
  • Deleting a User:
    • Use the userdel command to delete a user account. For example: sudo userdel username.
    • By default, userdel does not delete the user's home directory or their files. To remove the home directory and files, use the -r flag: sudo userdel -r username.
  • Granting Superuser Privileges:
    • To grant a user superuser privileges, add them to the sudo group using the usermod command: sudo usermod -aG sudo username.
    • This allows the user to execute commands with administrative privileges using the sudo command.
  • Listing User Information:
    • Use the cat /etc/passwd command to view a list of all user accounts on the system.
    • The output includes details like the username, user ID (UID), group ID (GID), home directory, and shell.

Remember to exercise caution when managing user accounts and ensure that you have the necessary permissions to perform these actions. It's recommended to refer to the official documentation or man pages of specific commands for more detailed information.

See Also

Conclusion

  • A username is a unique identity for a user that allows them to log in, access files, and perform certain tasks on the system and learning to change the username is an important task while we talk about user and system management in Linux.
  • Linux provides various options for changing usernames
  • In this article, we learnt how to change the username in Linux using various methods:
    • Change a Username on Linux Using the usermod Command
    • Change a Username on Linux Using the chfn Command
  • We also learnt about user management techniques in Linux.