In this tutorial, you will learn how to a rename a user in CentOS, Debian, Ubuntu and most other Linux distributions. More than that, you will also learn how to rename thier home directory, primary group, and change their UID.
Renaming Users in Linux
Linux provides a tool named
usermod specifically for making modifications to user accounts. In this case we are using it to rename a user account, which is done using the
sudo usermod -l <new-name> <old-name>
For example, to rename a user named student1 to johndoe, you would run the usermod command as follows.
sudo usermod -l johndoe student1
-l flag will only change the user’s name. All other things attached to the user will remain unaffected, such as home directory. and UID.
Changing a User’s Home Directory in Linux
After renaming a user it may make sense to change their home directory, too. Otherwise, it would cause a lot of confusion trying to explain why
johndoe‘s home directory is
To change the user’s home directory we once again use the
usermod command. However, this time we need to perform to actions: change the home directory to a new path, and move the contents from the old path into the new path.
We set the new home directory path using the
--home flag with the path to the new directory. We must also use the
-m flag to copy the contents of the old home directory into the new one.
sudo usermod -d /home/joedoe -m johndoe
Renaming a User’s Group
Every user on a Linux system is created with a group of the same name. When we change the name of a user their group name is left untouched. It’s a good idea to also change the user’s primary group name as well.
To change a user’s primary group name we use the
groupmod command with the
-n flag. We must supply the old name and a new name.
For example, to rename the newly renamed user
johndoe‘s primary group to
student1, we would run the following command.
sudo groupmod -n johndoe student1
Changing a User’s UID
A little more rare than renaming a user or changing their home directory is changing their UID. A User’s UID is their unique ID on a Linux system. When we assign permissions to file and directories, we use their UID. Processes started by a user are also executed using a user’s UID.
To change a user’s ID we use the
usermod command with the
-u flag, followed by a new, unique integer.
For example, to set
johndoes UID to 5001, we would run the following
sudo usermod -u 5001 johndoe