Change Username in Ubuntu
Don't like an account name? You can change it. Here's all you need to know about changing username in Ubuntu command line.
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.
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.
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
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
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
Now log out and log back in with the new username and it should work as expected:
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.
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.