Linux update user name

How To Change Username On Ubuntu, Debian, Linux Mint Or Fedora

This is a step by step guide on how to change your username on Debian, Ubuntu, Linux Mint (and other Linux distributions based on Debian/Ubuntu), and Fedora.

The instructions explain how to change (rename) the username along with the user’s home folder name, as well as a simple work-around for potential issues with configuration files that still point to the old home folder name. There’s also a step for changing the full name (display name), which is shown on the login screen and other places throughout the desktop.

Throughout all the instructions / commands below, remember to replace newusername with the new username, and oldusername with the . old username.

1. Create a temporary user and give it sudo privileges.

We’ll login with the temporary user to perform the username change commands. This way there won’t be any issues running them from the same user we’re trying to rename (e.g. processes running for that user, etc.).

Add a new temporary user ( tempuser ) and give it sudo privileges on Debian, Ubuntu or Linux Mint (after entering a password you can keep pressing Enter to skip entering the rest of information):

sudo adduser tempuser sudo usermod -aG sudo tempuser

Add a new tempuser and give it sudo privileges on Fedora:

sudo adduser tempuser sudo passwd tempuser sudo usermod -aG wheel tempuser

2. Login with tempuser and change (rename) the username, home folder and group.

Logout, and on the login screen select tempuser and login with that user. Next, open a terminal and run these command to change your username, home folder, and group from oldusername to newusername :

sudo usermod -l newusername -d /home/newusername -m oldusername sudo groupmod -n newusername oldusername

If you get an error about a process being in use for the old username, kill that process ( kill PID ), but that shouldn’t happen in most cases since we’re logged in using a temporary, intermediate username to make these changes. If it does happen, another workaround is to reboot and then login straight with tempuser , that way there aren’t any processes used by the old username.

3. Create a symbolic link from /home/newusername to /home/oldusername .

Some applications will show errors when changing the username, because in some cases there are configuration files that point to the old username’s home folder. A solution for this is to create a symbolic link from the new home folder to the old home folder:

sudo ln -s /home/newusername /home/oldusername

4. Change the display name / full name (firstname lastname).

Читайте также:  Web media player linux

In my case, trying to rename a username on Ubuntu 19.04 and Fedora 29, the display name (full name) remained unchanged on the GDM3 login screen and on the user menu. This can be changed though, using:

sudo chfn -f "firstname lastname" newusername

Replace firstname and lastname with your first and last name, or whatever you want to show up on the login screen and various other places throughout the system. firstname and lastname can also be one item, e.g. your new username.

The chfn command is available on Fedora in the util-linux-user package, which is not installed by default. You can install it on Fedora using:

sudo dnf install util-linux-user

After installing this package, the chfn command should work on Fedora.

5. Login using the new (renamed) username, and delete the temporary user created on step 1.

Logout and select the new, renamed username from the login screen, and proceed to login. You can now delete the temporary user created in step 1 ( tempuser ) and its home folder:

Источник

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.
Читайте также:  Linux ubuntu as terminal server

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.

Читайте также:  Linux root file system fsck

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.

Buy GoLinuxCloud a Coffee

For any other feedbacks or questions you can either use the comments section or contact me form.

Thank You for your support!!

Источник

Оцените статью
Adblock
detector