A2oz

How Do I Change My Linux Name?

Published in Linux System Administration 2 mins read

Changing your Linux name refers to modifying either your username or your hostname. Let's explore both scenarios:

Changing Your Username

  1. Switch to root:
    • Use the sudo su command to gain root privileges.
  2. Edit the /etc/passwd file:
    • Open the /etc/passwd file using a text editor like nano or vi.
    • Locate the line containing your current username.
    • Replace the old username with your new desired username.
  3. Update the user's home directory:
    • Rename the directory corresponding to your old username to your new username. For example, if your old username was olduser and your new username is newuser, you'd rename the /home/olduser directory to /home/newuser.
  4. Logout and log back in:
    • Log out of your current session and log back in with your new username.

Changing Your Hostname

  1. Edit the /etc/hostname file:
    • Open the /etc/hostname file using a text editor like nano or vi.
    • Replace the current hostname with your desired hostname.
  2. Edit the /etc/hosts file (optional):
    • You can also edit the /etc/hosts file to reflect the new hostname. This is not strictly necessary, but it can help with local network resolution.
  3. Restart the networking services:
    • Run the command sudo systemctl restart network to restart the networking services and apply the new hostname.

Example

Let's say you want to change your username from olduser to newuser:

  1. You would use the command sudo su to become root.
  2. Then you would open the /etc/passwd file and change olduser to newuser on the relevant line.
  3. Next, you would rename the /home/olduser directory to /home/newuser.
  4. Finally, you would log out and log back in using your new username newuser.

Note: These steps might vary slightly depending on your Linux distribution. Always back up your files before making any significant system changes.

Related Articles