Table of Contents
Linux operating systems allow both to be used with more than one user and many users to log in at the same time.
If you don't have a centralized user management, you'll have to deal with users' privileges manually. In addition, you may have to produce user-based solutions to the problems you encounter. One of them is to change the username. Let's take a look at "How to change username in Linux".
You will need root level access or a user with sudo access to perform the steps as explained in this tutorial
Method-1: Change username on Linux with usermod command
Step-1: Change Username
usermod is used to change username. It comes installed in every Linux distribution as it is a basic need. The standard usage of the usermod command is as follows;
usermod -l [new_user] [old_user]
In this way, the username changes, but just changing the username may not be enough. It is important that information such as the group the user is in and the home directory change with username.
To change the username, of user pardus to faruk, run as follows;
$ sudo usermod -l faruk -d /home/faruk -m pardus
-l: After this parameter, new username information is given.
-d: The home directory name of the new user is determined in this parameter.
-m: Defines old username information to move home directory.
Let's look at the changes in the passwd and shadow files;
foc@pardus:~$ cat /etc/passwd | grep faruk faruk:x:1001:1001:faruk:/home/faruk:/bin/bash foc@pardus:~$ sudo cat /etc/shadow | grep faruk faruk:$6$9PuqX6wbcYFoEcUV$sPbZy2UttnMkN0o/D0VSY2ZhtTIjtl/JVKyRCKgj/Hqqzt6oA5bJOzpXMIcK.Rsl5Ulyr62ajytsleguTUSpX/:19193:0:99999:7:::
Pardus user's information has been updated as faruk. However, the group information remained as pardus. When you run the following command about the group, no results will be shown anymore;
foc@pardus:~$ sudo cat /etc/group | grep faruk
Step-2: Change Group Name
In Linux, a group with the same name is created with each newly created user. After changing the username, it is necessary to change the group name as well. For this, the
groupmod command is used.
The standard usage of the groupmod command is as follows;
groupmod -n [new_group] [old_group]
In our example;
foc@pardus:~$ sudo groupmod -n faruk pardus
Let's look at the
/etc/group file after this command;
foc@pardus:~$ sudo cat /etc/group | grep faruk faruk:x:1001:
Method-2: Change username on Linux without usermod command
Now we can also change username of any Linux user without using usermod or groupmod command by manually manipulating system files. As we already discussed in previous section. there are some important files which contains the user information. We just need to manually update those files. Let's have a look at those system files.
In this section, we are going to rename user deepak to amit on our Linux box.
~]# id deepak uid=1000(deepak) gid=1000(deepak) groups=1000(deepak),10(wheel)
Files Containing User Information
In Linux, everything is a file. User information is also kept in different files. For example;
User's hash password;
~]# grep deepak /etc/shadow deepak:$6$x1tVsOHcBiY8I0oV$Qr3tS3aiEJVkOgvEFFLlnR/y4kS0bGaQmRH3lO0D7YtUzILQ5EiFYQrpG93sbotAfewFD/b7.zYCnI9IEeKT..:18851:0:99999:7:::
Information about the user (home directory, default shell, user and group id etc.);
~]# grep deepak /etc/passwd deepak:x:1000:1000:deepak:/home/deepak:/bin/bash
User group information;
~]# grep deepak /etc/group wheel:x:10:deepak
The state of the user home directory before the changing the username;
~]# ls -ld /home/deepak/ drwx------. 15 deepak deepak 4096 Oct 27 2021 /home/deepak/
Step-1: Start modifying the system files to change the username
[root@mail ~]# grep amit /etc/passwd amit:x:1000:1000:amit:/home/amit:/bin/bash
[root@mail ~]# grep amit /etc/group wheel:x:10:amit amit:x:1000:
[root@mail ~]# grep amit /etc/shadow amit:$6$x1tVsOHcBiY8I0oV$Qr3tS3aiEJVkOgvEFFLlnR/y4kS0bGaQmRH3lO0D7YtUzILQ5EiFYQrpG93sbotAfewFD/b7.zYCnI9IEeKT..:18851:0:99999:7:::
Now let's verify if user amit exists on our Linux server:
~]# id amit uid=1000(amit) gid=1000(amit) groups=1000(amit),10(wheel)
As you can see, amit is having the same UID, GID and groups as user deepak had so we have successfully renamed our user.
Step-2: Update user’s home directory
If we are manually updating the username, then the user's home directory name also must be changed manually. Previously the user deepak's home directory was
[root@mail ~]# ls -l /home/ total 24 drwx------. 15 amit amit 4096 Oct 27 2021 deepak
As you can see, the user and group owner of
/home/deepak has changed to amit automatically but the homedir name is still deepak so we must manually change this to amit.
We will use
cp command along with
-ap to preserve all the permissions and copy content of
~]# cp -aprvf /home/deepak /home/amit
-a: archive and copy
-p: preserve mode, ownership, timestamps
-r: Recursively copy the content i.e. including sub-directories and files
-v: Verbose output
-f: Forcefully copy the content
Once the copy is complete, re-verify the permission:
~]# ls -l /home/ total 24 drwx------ 4 amit amit 4096 Jul 27 22:25 amit drwx------. 15 amit amit 4096 Oct 27 2021 deepak
Now you can try to login as user amit:
[root@mail ~]# su - amit [amit@mail ~]$
If everything looks good, then you can plan to delete
Things to be pay attention
Another thing to note before changing the Username is that you must not be logged in with the user you are changing. If you want to change the name of the logged in user, you will get an error;
foc@pardus:~$ sudo usermod -l faruk foc usermod: user foc is currently used by process 493
Processes starts for each logged in user. For example, the processes of the above user is below;
foc@pardus:~$ sudo ps -aux | grep pardus root 926 0.0 0.3 16980 8076 ? Ss Jul20 0:00 sshd: pardus [priv] pardus 929 0.0 0.4 21260 9132 ? Ss Jul20 0:00 /lib/systemd/systemd --user pardus 930 0.0 0.1 105396 2552 ? S Jul20 0:00 (sd-pam) pardus 944 0.0 0.2 16980 4960 ? S Jul20 0:00 sshd: pardus@pts/0 pardus 945 0.0 0.2 8356 5200 pts/0 Ss Jul20 0:00 -bash
You can kill each process id with the kill command (
kill -9 [proccess_id]) or
killall -u <user>, but the best method is to reboot the system and log in with a different user (use root user if there is no other local user).
You have more options regarding switching users. For this, you can examine the manual page with the following command in the terminal;
foc@pardus:~$ man usermod