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 and also we can rename the home directory or its UID as well.
In this short tutorial, we will be discussing these things only.
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 ‘testuser’ & want to rename it to ‘produser’, execute the following command from terminal;
$ sudo usermod -l produser testuser
This will only change the username but 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 testuser
$ sudo pkill -9 -u testuser
Renaming Home directory
For renaming home directory to correspond to the renamed user, we use ‘-d’ option with ‘usermod’ command.,
$ sudo usermod -d /home/produser -m produser
Changing UID for the user
To change the UID of the user , execute the following command,
$ sudo usermod -u 2000 produser
where ‘2000’ is the new UID for user.
Renaming the group
To rename the group from ‘testuser’ to ‘produser’, we will use ‘groupmod’ command. Use the following command to rename the group,
$ groupmod -n produser testuser
Once we have made the required changes, we can than check the changes made using the ‘id’ command,
$ id produser