64

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 .. ?

  • Duplicate of https://askubuntu.com/questions/34074/how-do-i-change-my-username/317008 and https://unix.stackexchange.com/questions/98461/proper-way-of-changing-username-in-ubuntu-or-any-linux – sondra.kinsey Aug 30 '17 at 16:53

4 Answers4

107

Procedure to rename or change username in Ubuntu:

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

Press Ctrl+Alt+F1

Otherwise, simply open a new Terminal:

Press Ctrl+Alt+T

And run:

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]

Example:

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:

Press Ctrl+Alt+F7

Stachu
  • 103
  • 4
kyodake
  • 15,401
  • 2
    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. – baltasvejas Oct 03 '15 at 07:20
  • 3
    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 – Sergiy Kolodyazhnyy Oct 06 '15 at 17:45
  • 2
    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. – Shaun Dychko Nov 06 '16 at 20:09
  • Does not really work for me. After doing this, the ui does not work properly anymore. After login it just shows the desktop wallpaper. – Johannes Dec 12 '17 at 09:32
  • 2
    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 . – krlmlr May 09 '18 at 09:03
  • As baltascejas tried to point out: pressing Ctrl-Alt-T while logged in as the user you are trying to edit will just kill your session when you killall -u oldname – virtualxtc Jun 26 '18 at 03:13
  • should this line: usermod -d /home/newname -m newname be this instead: usermod -d /home/oldname -m newname (note the change from old name to new name)?? – BenKoshy Jul 23 '18 at 07:30
  • 1
    @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. https://linux.die.net/man/8/usermod – bitsoflogic Jul 14 '20 at 13:57
  • why exec? is it really needed? – Johannes Lemonde Apr 11 '21 at 07:59
  • In WSL killall didn't work, as a new process started shortly after killall. So i ran: killall -u [oldname] && usermod -l [newname] [oldname] – fishbone May 26 '21 at 12:00
22

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.

wbloos
  • 331
  • 2
  • 5
3

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
muru
  • 197,895
  • 55
  • 485
  • 740
-3

Please try the below to change the user name in home directory,

usermod -d /home/newHomeDir -m newUsername

Hope this helps !

Pilot6
  • 90,100
  • 91
  • 213
  • 324
BDRSuite
  • 3,156
  • 1
  • 12
  • 11