477

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?

Dan
  • 13,119
Takkat
  • 142,284
  • 1
    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? – Anand Jul 22 '19 at 10:46

8 Answers8

514

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.

To change home-folder, use

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:

  1. Login with your old credentials and add a new user, e.g. "temporary" in TTY1:

    sudo adduser temporary
    

set the password. 2. Allow the temporary user to run sudo by adding the user to sudo group:

    sudo adduser temporary sudo
  1. Log out with the command exit.

  2. 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)

  3. Go back to TTY7 (Ctrl+Alt+F7) to login on the GUI/normal desktop screen and see if this works.

  4. Delete temporary user and folder:

    sudo deluser temporary
    sudo rm -r /home/temporary
    
Egil
  • 14,162
  • 12
    This is unfortunately not true for different configuration files under $HOME, try running something like grep -IRFl /home/username ~ and you will see how many references to your home directory are stored there. – arrange Apr 08 '11 at 09:17
  • 1
    I'm running 12.04 LTS on a VirtualBox guest machine. This answer (using the "temporary" account) worked for me. However, the GUI still shows the username as the old username, even after system restart. The terminal prompt and directory structure show the new username. Any idea how to update the username displayed on the GUI? – Steve Koch Feb 21 '14 at 17:12
  • 2
    Was a simple fix -- Just had to use the User Accounts editor in unity, followed by restart. – Steve Koch Feb 24 '14 at 17:29
  • 6
    For those with an encrypted home folder, you'll have to edit /home/.ecryptfs/oldusername/.ecryptfs/Private.mnt to point to your new home folder, else you won't be able to login via Unity. – raphael Jan 22 '16 at 21:57
  • 2
    sudo usermod -d /home/edge -m edge yields: usermod: Directory /home/empedokles could not be renamed in /home/edge i.e. step 4 won't work. – empedokles Oct 13 '16 at 10:50
  • 1
    I would also recomment sudo nano /etc/sudoers and edit/add your new user if zou wany him to sudo – JTC May 03 '17 at 18:24
  • 3
    @JTC never edit sudoers with plain nano. Always use visudo. – styrofoam fly Jan 04 '18 at 11:09
  • 1
    I think you can get away without having to make a temporary user if: 1. You log out of the GUI, 2. Login to TTY and cd /tmp; exec sudo -i, so that your current shell is replaced by the sudo process and 3. kill all processes running under your user (pkill -u $SUDO_USER). That should eliminate everything that's using your user/home directory. – muru Mar 05 '18 at 07:15
  • This solution worked for me on Kubuntu 16.04, however some things broke. Including the fact that I'm not logged in automatically and xserver won't start automatically either. – AdyAdy Mar 23 '18 at 14:29
  • 2
    Doesn't work on Ubuntu 16.04. sudo su -, sudo usermod -l new old -> usermod: user old is currently used by process 123 where 123 = bash. If you kill it kill -9 123, it exits and goes to login screen. Ie. non-root cannot change their own username. – Juha Untinen Jun 01 '18 at 11:41
  • To avoid having to delete the home folder of "temporary", create the user using the --no-create-home option – ma3oun Sep 26 '18 at 17:59
  • 5
    You would need to change the group as well: sudo groupmod -n new-name current-name – Alessandro Oct 09 '18 at 09:43
  • 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? – Anand Jul 22 '19 at 10:45
  • This answer is longer, more confusing, and not linear like the answer from Valentin. While they both have the same basic info, the other is the better answer. – Cliff May 12 '20 at 18:43
  • 1
    after usermod for the home folder, the group was still old, so I had to do this: groupmod -n newGroup oldGroup – Aquarius Power Sep 10 '20 at 03:27
  • Why not just create a new user that will have your username and password as you wish: 'sudo adduser urname' and then add this user to sudo group: 'sudo adduser urname sudo' and then login to that user and delete the old one !? – YourHelper Jan 30 '22 at 18:05
  • Can you do this with ubuntu on wsl? – Vasiliki Aug 09 '22 at 11:25
  • is this still the way to go as of ubuntu 22.04? – Luca Clissa Dec 21 '22 at 15:11
157

To put it all together:

  1. Log out of your session or restart your computer to get back to the start screen.

  2. At the start screen go to a console mode tty. Press Ctrl+Alt+F1 (on some Ubuntu revisions this may be Ctrl+Alt+F2 instead)

  3. Log in using your username and password.

  4. Set a password for the "root" account.

     sudo passwd root
    
  5. Log out.

     exit
    
  6. Log in using the "root" account and the password you have previously set.

  7. Change the username and the home folder to the new name that you want.

     usermod -l <newname> -d /home/<newname> -m <oldname>
     usermod -c "newfullname" <newname>
    

    "newfullname" is the fifth column in /etc/passwd, and might be "First Last", for instance.

  8. Change the group name to the new name that you want.

     groupmod -n <newgroup> <oldgroup>
    
  9. Lock the "root" account so it no longer has a valid password that can be used to log in as root.

     passwd -l root
    
  10. If you were using ecryptfs (encrypted home directory). Mount your encrypted directory using ecryptfs-recover-private and edit <mountpoint>/.ecryptfs/Private.mnt to reflect your new home directory.

  11. Log out.

    exit
    
  12. Press Ctrl+Alt+F7 to go back to the Ubuntu graphics mode login screen (on some Ubuntu revisions this may be Ctrl+Alt+F1).

And now you can log in using your new username.

Valentin Uveges
  • 1,699
  • 1
  • 11
  • 5
  • 11
    If after usermod -l you get a user <oldname> is currently used by process <processno>, you can find that process (probably a daemon, or tmux/screen) using ps aux | grep <processno>. You might want to gracefully stop that process, otherwise you can kill <processno>. – RedPixel Sep 30 '15 at 08:46
  • 1
    Thank you for putting this comment together. It was very useful. Two things that might be mentioned though. After step 4 I rebooted so I wouldn't get error messages at step 6. Also, after this process is complete, the /etc/passwd file will have and ID comment matching the old username. This will cause the login screen to display the old login name. I couldn't find a decent way to fix it from the command line, but I did fix it by logging into the user account, going to the account settings and changing the "Login Name" to the username. This updated the /etc/passwd file and fixed the problem. – Jibbers Mar 28 '16 at 17:17
  • 1
    This is the most easy and fool-proof way to do it. It's better reboot after step 4 as @Jibbers suggested. – Yerke Apr 08 '16 at 05:45
  • I'd mention that the "bookmarks in the File browser windows" are now pointing to the old folders, so I have to delete those bookmarks. – ollydbg23 Jun 04 '16 at 08:47
  • Won't work. If you press Ctrl+Alt+F1 at the beginning it's trying to log in into the newly created user which it won't allow. – empedokles Oct 13 '16 at 08:54
  • I'm unsure about what locking the root user does (passwd -l root), why was that necessary? – hello_there_andy Feb 13 '17 at 13:44
  • I tried this on Lubuntu 16.04 but it broke my installation: I get cannot loginto lightdm any more, I get a Dialogue "your Display is runnintǵ on low resolution" with a folowup dialog to try the default settings, which are not working, but the other options I cannot reach because there is no mouse to select another option and no possibility to reach the other options with TAB and Arrows. – rubo77 Jun 24 '17 at 09:34
  • I tried this on Lubuntu 16.04 but it broke my installation: I cannot log into lightdm any more, I get a Dialogue "The system is running in low-graphics mode" with a followup dialog "try running with the default graphical mode" (which are not working) or "reconfigure graphics" (the other options I cannot choose because there is no mouse pointer to select another option and no possibility to reach the other options with TAB and Arrows!) So: I had to reinstall lubuntu to get graphics back! – rubo77 Jun 28 '17 at 08:43
  • You will need to replace any reference in hidden files, and even then some things will still be broken – chefarov Jan 05 '19 at 11:08
  • 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? – Anand Jul 22 '19 at 10:45
  • In My ubuntu 16.04 LTS, need to kill all jobs by previous user before step 6 killall -u <old_user>, and rename the user directory after step 7 sudo mv /home/<old_user> /home/<new_user> – Eric Sun Feb 27 '20 at 07:30
  • 2
    If doing this over SSH, you need to enable root login in SSHD config for a moment. Go sudo nano /etc/ssh/sshd_config uncomment PermitRootLigin and change the value to yes. Restart SSH sudo systemctl restart sshd. After you've modified the user using root, put everything back to how it was ie lock root account and disable remote root login. – TimD Mar 09 '20 at 06:47
  • I'm getting Permission denied in step 6 when renaming administrator user account. Any idea how to bypass that? – June Wang Aug 01 '20 at 06:56
  • for parallels on mac running Ubuntu I had to use Ctrl-Alt-F3 to get to terminal – joshp Jan 10 '22 at 20:15
  • During installation of Ubuntu, setting a username with dots/periods in it was prohibited, so I set the username to firstlast. I then followed these instructions afterwards, after completing the full Ubuntu installation, to add a period in the username, changing it from firstlast to first.last, and it worked perfectly! – Gabriel Staples Apr 17 '23 at 06:17
32

Restart in recovery mode and go to the root prompt shell ("Drop to root shell prompt")

First remount the root

mount -o remount,rw /

To change the username and home folder name,

usermod -l <newname> -d /home/<newname> -m <oldname>

For group name,

groupmod -n <newgroup> <oldgroup>
Eric Carvalho
  • 54,385
karthick87
  • 81,947
  • 1
    when I use usermod -l <newname> -d /home/<newname> -m <oldname> this gives me error usermod: user oldusername is currently used by process 3170 and if I kill the process still it gives me the same error. using ubuntu 13.10 – Waqas Nov 24 '13 at 13:48
  • 4
    DO NOT DO THIS IF YOUR HOME DIRECTORY IS ENCRYPTED!!!! I followed this procedure and it sent me for a wild 2 hour ride... The only way to recover is to perform this procedure again and put the original (disliked) name back in. The problem you will have is "Cannot chdir into mount point" – Mike Oct 28 '16 at 12:21
  • 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? – Anand Jul 22 '19 at 10:46
9

On Ubuntu 13.10, 14.04, 16.04:

  1. Click on the "System Settings" icon.
  2. Click on "User Accounts".
  3. Your administrator account should be displayed.
  4. Click on the "Unlock" button.
  5. Enter your user password as requested to allow changes to your account.
  6. Once unlocked, you can click on your old user name that you wish to change and type in a new user name to replace it.
  7. When you have typed in the new name, click on the "Lock" button to make the change permanent.
  8. Restart Ubuntu.
wjandrea
  • 14,236
  • 4
  • 48
  • 98
Chicodoodoo
  • 123
  • 1
  • 4
  • 13
    This did not change the username for me, just the name. The old username still appears in the terminal, and as the home directory and group (even after rebooting). – DougC Mar 07 '18 at 19:30
3

When receiving usermod: can't change /etc/password just run the following commands:

In the root recovery console run:

mount -o remount,rw /

Then rerun:

usermod -l <newname> -d /home/<newname> -m <oldname>
chaos
  • 27,506
  • 12
  • 74
  • 77
  • 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? – Anand Jul 22 '19 at 10:46
1

The answer listed here by @karthick87 works perfectly fine but here is what I would do just to be safe and avoid all glitches:

  1. Create a separate account/user and make sure this account/user root capabilities or login as any root-able user.
  2. Logout with the current account and then login with the account mentioned in step 1.
  3. Kill all the processes of the previous user.
  4. Now follow the steps mentioned by @karthick87 and change username and also the home directory owner.
  5. Then follow @Sriraj Hebbar's answer to change the group name.
  6. logout with the spare user and login with your user. If you created an extra user delete it.
1

when you do usermod -l <newname> -d /home/<newname> -m <oldname> you will get useradd: can't change /etc/passwd error message to avoid this just add sudo -- to above command like

sudo -- usermod -l <newname> -d /home/<newname> -m <oldname>

and

sudo --  groupmod -n <newgroup> <oldgroup>
heemayl
  • 91,753
-1

Since not all the linuces (however Ubuntu must have it) have the usermod app, there is the way you can do it manually. As of root open /etc/passwd to edit with vim or any other editor presening in the system:

sudo vim /etc/passwd

and change the user's name at the beginning of a line:

user:x:500:501:username:home/user:/bin/bash

to:

newuser:x:500:501:username:home/user:/bin/bash

then if you worked of root just login, and if you have been logged in as a user, logoff, and relogin.

Of course you have to fix /etc/shadow, and /etc/group also to the system works properly. Thanx to @JohanBoulé

NOTE: You should use this approach carefully, to not break the system.

NOTE: This approach is common, not only for Ubuntu, but and for ubuntu it will work, however google search will show it event for non ubuntu search, for example for embedded linux

  • I tried this way (before I discovered the usermod command) and the user's password is no longer accepted. – Ben Voigt May 11 '16 at 17:51
  • @BenVoigt not all the pcs have the usermod – Малъ Скрылевъ May 11 '16 at 18:25
  • 4
    It's Ubuntu! If it doesn't have usermod your install is broken as it is part of the passwd package, which is required. – Auspex Mar 06 '17 at 14:10
  • @Auspex I know that here is the ubuntu QA, but this approach is common, not only for ubuntu, but and for ubuntu it will work, however google search will show it event for non ubuntu search, for example for embedded linux – Малъ Скрылевъ Jan 03 '21 at 20:43
  • 1
    I also do it this way regardless of whether usermod or whatever pseudo magical obscure opaque command exists because I find it simpler to remember, and it doesn't bug me with running processes. You need to edit /etc/passwd and /etc/shadow at the minimum, and also /etc/group if you want to have a group with the same name as the user. – Johan Boulé Jan 03 '21 at 20:44
  • I had intended to leave this but I can't. First this is the Ubuntu Q&A as you admit. Suggesting a method that is not required on an Ubuntu system and is potentially dangerous is a bad idea. @JohanBoulé 's argument is very bad. This "pseudo magical obscure opaque command" exists to make this sort of change easy without making it possible to break logins, as Ben managed to do. usermod is not magic, it's not obscure, and it is the correct way to make the change on Ubuntu. – Auspex Jan 06 '21 at 12:49
  • My very bad argument, as you said, is that the fileformats of passwd, shadow, and groups, are dead-simple and fairly well documented and standard ... and easy to remember or lookup in the manpages. Also notice that the groupmod command is not going to do for you all the edits that are needed in the group file. So, for the group file, go on and open it with your favorite text editor to replace all the occurrences of "debian". Finally, notice that the provided workarounds to running processes cannot be used on headless servers that disallows root ssh login, which the default. – Johan Boulé Jan 07 '21 at 10:57
  • @Auspex " it is the correct way to make the change on Ubuntu. " that isn't unique method, people have to know ALL the methods, I've already assertained in it here on SO/SE – Малъ Скрылевъ Jan 09 '21 at 15:48
  • This is not SO/SE. This is AskUbuntu – Auspex Jan 11 '21 at 15:29
  • @Auspex hah, didn't you really know that AskUbunty is undeed a member of SO/SE community?) – Малъ Скрылевъ Jan 12 '21 at 15:57
  • Of course I know that. And there's nothing wrong with adding an alternate answer in AskUbuntu, but the pure fact is that this IS AskUbuntu, and if you'd prefaced it with "For non-Ubuntu systems you might want to use...", I'd be fine with it. However, Ubuntu systems must have usermod, and usermod is the correct way to do this on an Ubuntu system. Ubuntu users do not have to be told methods that can break their systems. And suggesting vim is equally wrong... – Auspex Jan 13 '21 at 13:50
  • @Auspex for me, (im using ALT) i prefer always way to fix username manually with editor, even usermod is also present on my system, so that is the only additional correct way to change username along with usermod one. And this method itself can't bear the system, because system can be broken by a human... Some changes I did – Малъ Скрылевъ Jan 13 '21 at 16:48