Home Linux Tips & Tricks How To Properly Change Username In Linux

How To Properly Change Username In Linux

By sk
Published: Updated: 24.5K views

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.

Introduction

A few days ago, I downloaded a Fedora 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 changing username in production system. 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 don't 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 root user.

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 command:

# 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 finally change 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 $HOME directory for the user.
  • -m : Move the content of the old user's $HOME directory to the new location.
  • -g ostechnix : Add the new user to a group called "ostechnix".
  • -l ostechnix vagrant : Change the name of the user from "vagrant" to "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 from the root user and log in as new user.

Verify if the hostname, username, and $HOME directories are changed:

Check username and hostname in Fedora
Check username and hostname in Fedora

Great! Everything is changed.

For more details about usermod command, refer its man page:

$ man usermod

Hope it helps.

You May Also Like

2 comments

Mike Landis June 3, 2021 - 8:01 pm

I have Scientific Linux 7.9 and I’d like to move to Centos Stream 8, but SL 7.9 can’t find centos-release-stream. Is (or will there be) a fix for that? I have a RAID-10 system that Fedora didn’t want to install on, but SL 7.9 didn’t complain (that’s how I got here). I now (finally) have Python 3.9.5, TensorFlow 2.5 & Keras installed and don’t want to lose all of that downloading & configuring. See below for specifics trying to pull/install centos-release-stream… Any suggestions?

root@localhost-3c58:/etc/systemd/system$ dnf install centos-release-stream
Repository ‘fermilab-debuginfo’ is missing name in configuration, using id.
Repository ‘fermilab-source’ is missing name in configuration, using id.
Repository ‘fermilab-testing’ is missing name in configuration, using id.
Repository ‘fermilab’ is missing name in configuration, using id.
Repository ‘repos’ is missing name in configuration, using id.
Repository ‘repos-source’ is missing name in configuration, using id.
Repository ‘sl-extras’ is missing name in configuration, using id.
Repository ‘sl-extras-debuginfo’ is missing name in configuration, using id.
Repository ‘sl-extras-source’ is missing name in configuration, using id.
Scientific Linux $slreleasever – x86_64 0.0 B/s | 0 B 00:00
Scientific Linux $slreleasever – x86_64 – bugfix updates 0.0 B/s | 0 B 00:00
Scientific Linux $slreleasever – x86_64 – security updat 0.0 B/s | 0 B 00:00
Failed to synchronize cache for repo ‘sl’, ignoring this repo.
Failed to synchronize cache for repo ‘sl-fastbugs’, ignoring this repo.
Failed to synchronize cache for repo ‘sl-security’, ignoring this repo.
Last metadata expiration check: 0:54:04 ago on Thu 03 Jun 2021 06:28:56 AM PDT.
No match for argument: centos-release-stream

Reply
sk June 3, 2021 - 8:44 pm

I guess you’re asking how to migrate to CentOS stream from Scientific Linux. The centos-release-stream package will only convert the CentOS systems to CentOS Stream. It won’t migrate Scientific Linux to CentOS Stream. You need to find a way (which I don’t know yet) to migrate your Scientific Linux to CentOS 8 and then convert the CentOS 8 to CentOS stream.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More