Change username in Ubuntu
Basics

Change Username in Ubuntu

Sagar Sharma
Sagar Sharma

Table of Contents

Misspelled the name while creating a user? Don't worry; you can change the username in Ubuntu quite easily and effortlessly.

As the root user or with sudo, you can use the usermod command to modify a user account to change its name.

usermod -l new_username old_username

As you can see, it is pretty straightforward. Still, let me show it in detail with actual examples. I'll also discuss renaming the home directory to make the username change complete.

Ubuntu 18.04 Review of New Features
Ubuntu 18.04 Review of New Features

Changing the username in Ubuntu

You need to switch to the root account or use sudo with an admin user account to change the username.

🚧
Make sure that the user you are trying to rename is logged out. Otherwise, it won't let you change the username. Needless to say, you cannot change the name of the user you are logged in as even with sudo.

To change your username completely, I'm going to divide this process into three simple steps.

  • Change username
  • Change group name
  • Rename home directory

1. Change username in Ubuntu

To change the username, all you have to do is execute the following command:

usermod -l newUsername oldUsername

Here, I will be changing from sagar to abhiman:

usermod -l abhiman sagar
change username of user in ubuntu

2. Change the Group name in Ubuntu

In Ubuntu, every user has a default group with the same name as the user. To follow this norm, I suggest changing the group name as well.

To change the group name, follow the given command syntax:

groupmod -n newUsername oldUsername

For me, I'm changing the group name from sagar to abhiman:

groupmod -n abhiman sagar
change group name in ubuntu

3. Change the name of the home directory

Technically, not changing the name of the home directory won't cause any issues. But to make it a seamless experience, you should go for it.

You can use the same user modification command usermod like this:

usermod -d new_home_directory -m new_username

With the -m option, it copies the contents of the current home directory (/home/old_username) to the new home directory.

As I'm changing to abhiman, my command looks like this:

usermod -d /home/abhiman -m abhiman
change name of home directory in ubuntu

Now log out and log back in with the new username and it should work as expected:

change username in ubuntu

And if you followed the steps correctly, you would have the same effect.

What happens to files owned by the old username?

You don't need to change the ownership of the files anywhere. Linux works on an ID system. Users are assigned UID and groups have GID. When you change the username, the ID is not changed and hence, files owned by the user (UID) are also not changed.

In other words, the changed user name still owns the file created by the old username.

I hope you will find this guide helpful and if you have any queries or suggestions, let me know in the comments.



Sagar Sharma

Sagar Sharma

A software engineer who loves to tinker with hardware till it gets crashed. While reviving my crashed system, you can find me reading literature, manga, or watering my plants.