Linux how to rename user

Renaming user name [duplicate]

I made a mistake in the username at the time of installation. So I changed the name of the user after logging in by creating a guest account with SUDO rights. But when I checked the list of users using ls /home command I am not seeing the new name for the user instead Old name is appearing. But I use the new name to login. Is it like I need to change the name inside Home directory separately? Also , if there is any other changes needs to be done .. ?

4 Answers 4

Procedure to rename or change username in Ubuntu:

If running as the user to be modified, logout first, then open a console:

Otherwise, simply open a new Terminal:

exec sudo -i killall -u [oldname] id [oldname] usermod -l [newname] [oldname] groupmod -n [newname] [oldname] usermod -d /home/[newname] -m [newname] usermod -c "[full name (new)]" [newname] id [newname] 
sudo -i killall -u belaje id belaje uid=1000(belaje) gid=1000(belaje) groups=1000(belaje),4(adm),24(cdrom),27(sudo),30(dip),46(plugdev),109(lpadmin),127(sambashare) usermod -l balaja belaje groupmod -n balaja belaje usermod -d /home/balaja -m balaja usermod -c "Bala Jagannathan" balaja id balaja 

Whenever you want to get back from console version to graphical version:

Using this procedure I successfully modified user account however only from another account (which I had to create for that purpose and later deleted). I wonder if that is the case (which is not mentioned actually) or I was doing something wrong? The main point was that «killall -u oldname» logged me out so that I couldn’t continue remaining steps.

This probably would be better done through a TTY, not a terminal emulator in GUI. So I’d replace the keyboard shortcut to terminal emulator with shortcut to TTY1, which is Ctrl + Alt + F1

Please add that if the user has an encrypted home directory, it’s also necessary to edit /home/newname/.ecryptfs/Private.mnt to point to the new home directory.

Instead of killall and usermod (which I didn’t want to do through an SSH connection with the logged-in user) I had success with changing the user name in /etc/passwd and /etc/shadow .

@BKSpurgeon Nope. After renaming the account, the new login is using the /home/oldname folder. You specify the new folder name and the user it’s for, so newname both times. linux.die.net/man/8/usermod

on the server this worked for me:

sudo usermod -md /home/NewHome -l NewName OldName sudo groupmod -n NewGroupName OldGroupName 

This renames the user and its group and also the home folder.

Читайте также:  Linux remove directory force

This works, but a lot of the apps will break because they specifically call for the /home/oldusername ; many individual software authors don’t use variables to call their configs, they directly specify oldusername not $HOME. Chrome, GTK and firefox break like this. Don’t forget your keyrings will have the old credentials as well. I’m not sure how to back those up. maybe export them?

So, I end up doing the following to change username — man i wish i knew an easier way.

  • as olduser , sign in to chrome and firefox to sync out the settings
  • using users and groups in Settings, create user killmenow with pass of same, make him admin, reboot and log in as killmenow .
  • open terminal, riun sudo i , then mkdir /home/newusername and making it the same uid:gid as the oldusername
  • Move usercreatedfiles.xyz, Desktop, Downloads, and other dirs containing created files like docs, pics, music, movies, etc in /home/oldusername/ to /home/newusername .
  • Find out any specific apps with particular settings. Helpful to pay attention here 🙂 user will likely bean you with a phonebook if you lose their files 😉
  • vi /etc/passwd and /etc/group to reference newusername and /home/newusername
  • passwd newusername and enter a temporary password twice reboot and log in as newusername , password being what you entered in the previous point.
  • delete killmenow using «users and groups» in Settings.

wait until some time passes before deleting /home/oldusername — in case you missed important stuff. or move it to /home/newusername/Downloads and tell them

mv /home/oldusername /home/newusername/Downloads/oldusername-backup 

Источник

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.
Читайте также:  Alt linux vnc 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 find файлы больше

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