How to rename user in Linux (also rename group & home directory)
We might have come across a situation where we might want to rename user in Linux system, for whatever reasons. We can easily rename user in Linux & also we can rename the home directory or its UID as well.
In this short tutorial, we will be discussing these things only. Let’s start by renaming user in Linux first,
(Recommended Read: How to use FIND command to locate anything in Linux)
Rename user in Linux
For renaming user in Linux systems, we will use ‘usermod’ command. Syntax for the command is,
$ usermod -l new_username old_username
For example, if we have a user named ‘dan’ & want to rename it to ‘susan’, execute the following command from terminal;
$ sudo usermod -l susan dan
This will only change the username & everything else, like group, home directory, UID will remain same.
Note:- You should need to logged out from the account you are trying to rename. You can also kill all the processes running for that user, to do so execute the following command,
$ sudo pkill -u dan
$ sudo pkill -9 -u dan
Renaming Home directory
For renaming home directory to correspond to the renamed user, we use ‘-d’ option with ‘usermod’ command.,
$ sudo usermod -d /home/susan -m susan
Changing UID for the user
To change the UID of the user , execute the following command,
$ sudo usermod -u 2000 susan
where ‘2000’ is the new UID for user.
Renaming the group
To rename the group from ‘dan’ to ‘susan‘, we will use ‘groupmod’ command. Use the following command to rename the group,
$ groupmod -n susan dan
To use a name that’s already in use but you want to use it anyway, command would be,
$ groupmod -o -n susan dan
Once we have made the required changes, we can than check the changes made using the ‘id’ command,
$ id susan
With this we end this tutorial on how to rename user in Linux. Please let us know if you have any question or any issue or if you do have any suggestion, please do let us know that as well.