This brief guide explains how to properly change username in Linux operating system. Not just the username, this guide also describes how to change the hostname, user group, home directory and the ownership and group of that directory to the new user, along with all files.
A few days ago, I downloaded the Fedora 33 vagrant box and deployed it using
libvirt/kvm provider. As you might already know, all vagrant boxes comes with a default user called
"vagrant". And the default hostname for the vagrant box is
localhost. I changed the username and hostname in the Fedora VM for better accessibility in the network. If you're ever looking for a proper way to change the username in Linux without messing the existing configuration files, here is how.
A note of caution:
I would not recommend this in production environment. It is always best to just create a new user rather than renaming the existing one. Because renaming username will cause so many problems if the old user's home directory is referenced by some other configuration files. You may end up changing the config files used by so many services. Also do not do it if your $HOME directory is encrypted!. So I recommend you to create a new user and move the old user's data to new one. If it is a freshly installed system or just testing system, you can then go ahead and change the user name as described below.
The right way to change username in Linux
Changing hostname in Fedora and other Linux distributions is easy!
Login as a different
sudo user or
root user. Because you can't change the username of the currently logged in user. I logged in as
Change the current hostname using the
hostnamectl command like below:
# hostnamectl set-hostname fedora33
The above command changes the old hostname to "fedora33". Log out and log back in to apply the changes. You will now see that the hostname is updated in the shell prompt. You can also check it using
# hostname fedora33
Here comes the main part. Now I am going to change the user name from "vagrant" to "ostechnix". To do so, I created a new group called "ostechnix" using command:
# groupadd ostechnix
For the purpose of this guide, I used the same name for user and group. You can use different name if you want.
And then changed the username using the
usermod command like below:
# usermod -d /home/ostechnix -m -g ostechnix -l ostechnix vagrant
Let us break down the above command and see what each option does.
usermod: The command to modify a user account in Unix-like systems.
-d /home/ostechnix: Create a new
$HOMEdirectory for the user.
-m: Move the content of the old user's
$HOMEdirectory to the new location.
-g ostechnix: Add the new user to a group called
-l ostechnix vagrant: Change the name of the user from
"ostechnix". i.e. "vagrant" is the old user name and "ostechnix" is the new user.
The above command changes the user name, the user group, the $HOME directory and ownership and group of that directory to the new user, along with all the user's files and directories.
Finally, assign sudo privileges to the new user using command:
# usermod -aG wheel ostechnix
If you're on Debian-based systems like Ubuntu, you can grant sudo permissions to the user using this command:
$ sudo usermod -aG sudo ostechnix
Now exit it from the
root user and log in as new user.
Verify if the hostname, username, and $HOME directories are changed:
Great! Everything is changed as I wished.
For more details about
usermod command, refer its man page:
$ man usermod
Hope it helps.