[root tip] [How To] Change username

Difficulty: ★★☆☆☆

The easy way


  1. Backup your important files.
  2. Create a new user
    a. Standard user
    useradd -mUG <new_user_name>
    
    b. Admin user
    useradd -mUG lp,network,power,wheel <new_user_name>
    
  3. Remove the previous user and the user’s home
    userdel -rf <old_user_name>
    
  4. Restore the files to the new user.

The hard way


Username should be chosen with care for while it is possible to change it - it does have implications beyond recognition.

Implications to consider

  • Configuration file may no longer work as expected.
  • Scripts and service units relying on the old username.
  • Symlinks may point to invalid destinations.

Be sure to read the comments to below too …

If you still think you are up for the change - please read on.

The GNU way

By reading the man pages you find the GNU system has thought of it.

man usermod
man groupmod

Login as root!


You cannot login as the user you want to change - you must use root.

root access caveat

If you have chosen omit the root password upon initial installation - you need to set a root password before trying to change the username. This must be done using a sudo enabled user.

sudo passwd root

root access option 1

  • Reboot and don’t login.
  • Open a TTY pressing CtrlAltF4

root access option 2

  • Reboot and edit the grub entry.
  • Press e on the selected grub entry and add the number 3 to the kernel command line and press F10 to continue boot.

Manjaro usually creates a user group matching the username and assigns the user to the specific group. This makes it necessary on Manjaro to rename both the login and group.

When you are logged in as root first rename the user and rename the user’s home

usermod --login $NEWLOGIN  --move-home --home /home/$NEWLOGIN $OLDLOGIN

Then rename the user’s group

groupmod --new-name $NEWGROUP $OLDGROUP

NOTE: This can also be done from a live ISO using chroot.

Remedy implications


Contributed by @freggel.doe

Some symlinks in that new home-directory might be broken after, because their target contain the full path with the old username. Wine and it’s default $HOME/.wine -prefix comes to mind.

To find dead symlinks:

find /home/$NEWLOGIN -xtype l

Some config-files contain full paths as well (desktop background image for example). To find files with “old” username:

grep -rn "/home/$OLDLOGIN/"

Crossposted from nix.dk

10 Likes

Some symlinks in that new home-directory might be broken after, because their target contain the full path with the old username. Wine and it’s default $HOME/.wine -prefix comes to mind.
To find dead symlinks:

find /home/$NEWLOGIN -xtype l

Some config-files contain full paths as well (desktop background image for example).
To find files with “old” username:

grep -rn "/home/$OLDLOGIN/"
4 Likes

In order to avoid a (manual) reconfiguration of existing/saved network connections, they must be renamed, too, and can be found in

/etc/NetworkManager/system-connections/

Switch into that folder via cd (logged in as root), search for the old user name by

grep -rn "OLDLOGIN" .

and replace the entries of the desired connection(s) by the new user name. This can be done automatically, for example by

find . -type f -exec sed -i 's/OLDLOGIN/NEWLOGIN/g' {} \;

(I’m not an expert in sed and regular expressions, maybe someone could explain this or has a better method.)

As Manjaro uses sudo its root password is auto-generated on installation. You need to create a new password for the ‘root’ before logging out!

  1. sudo passwd root
  2. close your windows manager (for gnome: gnome-session-quit)
  3. (If you use a MacBook, [CTRL]+[ALT]+[F4] does not work to get to tty → use [FN]+[CTRL]+[ALT]+[F4])
  4. login as root with your above created password
  5. follow the tutorial…

Handy tips:

  1. find /home/$NEWLOGIN -xtype l will show ALL broken symlinks not nessesarily the ones with a wrong username. Keep that in mind.
  2. When fixing a broken symlinks/links/configs as root run the commands with your $NEWLOGIN e.g.: su -c 'COMMAND' $NEWLOGIN

Handy COMMANDS:

export NEWLOGIN=myshinynewusername
export OLDLOGIN=MyUglyOldUserName

Wine:

mv /home/$NEWLOGIN/.wine/drive_c/users/$OLDLOGIN /home/$NEWLOGIN/.wine/drive_c/users/$NEWLOGIN

config/log/text etc files (non binary = option I):

grep -rlI '/home/$OLDLOGIN/' | xargs sed -i.bak 's/\/home\/$OLDLOGIN/\/home\/$NEWLOGIN/g'

Network wifi settings (reboot for it to work):

sudo sed -i 's/user:$OLDLOGIN:/user:$NEWLOGIN:/g' /etc/NetworkManager/system-connections/*

I enhanced your commands below, thanks for pointing me into the right direction!

1 Like

I am not sure if it is connected but: I also needed to do …

sudo chmod g-w /home/$NEWLOGIN

… to get my sshd server working with the $NEWLOGIN

How to cleanly do that? Can I just delete that home folder?

# userdel -r $user_to_delete

https://man.archlinux.org/man/userdel.8

1 Like