- How do I change my username?
- 8 Answers 8
- How to PROPERLY change username on Linux [2 Methods]
- Pre-requisite
- Method-1: Change username on Linux with usermod command
- Step-1: Change Username
- Step-2: Change Group Name
- Method-2: Change username on Linux without usermod command
- Files Containing User Information
- Step-1: Start modifying the system files to change the username
- Step-2: Update user’s home directory
- Things to be pay attention
- Summary
- Change Username in Ubuntu
- Changing the username in Ubuntu
- 1. Change username in Ubuntu
- 2. Change the Group name in Ubuntu
- 3. Change the name of the home directory
- What happens to files owned by the old username?
How do I change my username?
Some time ago, when I installed Ubuntu, I chose a rather stupid username for my account that I do not want to use anymore. How do I change this (including the name of my home directory, and the name in the terminal) without losing settings for applications?
How do I keep permissions and my keys for various authentification (e.g. email, SSH, GPG and more)?
What settings could possibly get lost if I changed my username?
Why can’t we navigate to /usr/share/applications/users.desktop >> Click on the user for which the name has to be changed. >> Click on Change User name >> Change the name >> Click on Ok. I think, this would be the easiest way to change the username, wouldn’t it?
8 Answers 8
Unix-like operating systems decouple the user name from the user identity, so you may safely change the name without affecting the ID. All permissions, files, etc are tied to your identity (uid), not your username.
To manage every aspect of the user database, you use the usermod tool.
To change username and user’s groupname (it is probably best to do this without being logged in):
sudo usermod -l newUsername oldUsername sudo groupmod -n newUsername oldUsername
This however, doesn’t rename the home folder.
sudo usermod -d /home/newHomeDir -m newUsername
after you changed the username.
For instance, you could logout, drop to a console ( Ctrl + Alt + F1 ), and sudo su — to become true root (as opposed to sudo -s , where $HOME is still /home/yourname.) Maybe you also have to kill some still running processes from this user first. To do so, enter ps -u username , look for the matching PID and kill them by kill PID-number .
Update: as arrange mentioned, some files may reference your old home directory. You can either keep a symlink for backward compatibility, e g ln -s /home/newname /home/oldname or you can change the file contents with sed -i.bak ‘s/*oldname*/*newname*/g’ *list of files* It creates a backup for each file with a .bak extension.
Some additional information for not so experienced users like me:
As I only have ONE user account (administrator), it would not let me change the username («you are already logged in» was the response in TTY1 ( Ctrl + Alt + F1 ). To get around this:
- Login with your old credentials and add a new user, e.g. «temporary» in TTY1:
set the password. 2. Allow the temporary user to run sudo by adding the user to sudo group:
sudo adduser temporary sudo
- Log out with the command exit .
- Return to tty1: Login with the ‘temporary’ user account and password. Change your username and folder as mentioned above. exit (until you get the login prompt)
- Go back to TTY7 ( Ctrl + Alt + F7 ) to login on the GUI/normal desktop screen and see if this works.
- Delete temporary user and folder:
sudo deluser temporary sudo rm -r /home/temporary
How to PROPERLY change username on Linux [2 Methods]
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».
Pre-requisite
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]
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 /home/deepak :
[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 /home/deepak to /home/amit .
It is recommended to use cp instead of mv command, as it is better to first copy the files and verify that everything is working before removing the old content.
~]# 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 /home/deepak directory.
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 , 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).
Summary
You have more options regarding switching users. For this, you can examine the manual page with the following command in the terminal;
Didn’t find what you were looking for? Perform a quick search across GoLinuxCloud
If my articles on GoLinuxCloud has helped you, kindly consider buying me a coffee as a token of appreciation.
For any other feedbacks or questions you can either use the comments section or contact me form.
Thank You for your support!!
Change Username in Ubuntu
Misspelled the name while creating a user? Don’t worry; you can change the username in Ubuntu quite easily and effortlessly.
As the root user or with sudo, you can use the usermod command to modify a user account to change its name.
usermod -l new_username old_username
As you can see, it is pretty straightforward. Still, let me show it in detail with actual examples. I’ll also discuss renaming the home directory to make the username change complete.
Changing the username in Ubuntu
You need to switch to the root account or use sudo with an admin user account to change the username.
Make sure that the user you are trying to rename is logged out. Otherwise, it won’t let you change the username. Needless to say, you cannot change the name of the user you are logged in as even with sudo.
To change your username completely, I’m going to divide this process into three simple steps.
1. Change username in Ubuntu
To change the username, all you have to do is execute the following command:
usermod -l newUsername oldUsername
Here, I will be changing from sagar to abhiman :
2. Change the Group name in Ubuntu
In Ubuntu, every user has a default group with the same name as the user. To follow this norm, I suggest changing the group name as well.
To change the group name, follow the given command syntax:
groupmod -n newUsername oldUsername
For me, I’m changing the group name from sagar to abhiman :
3. Change the name of the home directory
Technically, not changing the name of the home directory won’t cause any issues. But to make it a seamless experience, you should go for it.
You can use the same user modification command usermod like this:
usermod -d new_home_directory -m new_username
With the -m option, it copies the contents of the current home directory (/home/old_username) to the new home directory.
As I’m changing to abhiman , my command looks like this:
usermod -d /home/abhiman -m abhiman
Now log out and log back in with the new username and it should work as expected:
And if you followed the steps correctly, you would have the same effect.
What happens to files owned by the old username?
You don’t need to change the ownership of the files anywhere. Linux works on an ID system. Users are assigned UID and groups have GID. When you change the username, the ID is not changed and hence, files owned by the user (UID) are also not changed.
In other words, the changed user name still owns the file created by the old username.
I hope you will find this guide helpful and if you have any queries or suggestions, let me know in the comments.