Take the 2-minute tour ×
Ask Ubuntu is a question and answer site for Ubuntu users and developers. It's 100% free, no registration required.

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) without loosing 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?

Both, command-line and graphical solutions are appreciated.

share|improve this question

6 Answers 6

up vote 49 down vote accepted

Unix(-like) operative systems decouples the user name from its user identity, so you may safely change the first without changing the other. 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 (it is probably best to do this without being logged in):

usermod -l newUsername oldUsername

This however, doesn't rename the home folder.

To change home-folder, use

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). I found some information to get around this:

  1. Add a new user, e.g. "temporary". If you are still in TTY1:

    sudo adduser temporary
    

    set the password and just type enter exit. This should bring you to the original login prompt, if not type exit again.

  2. Change the account type of your new temporary user to administrator:
    In tty7 (Ctrl+Alt+F7, "normal" desktop) login in to your usual account. Change his privileges through the gui: System settings > users account. Click Unlock then change account type to "administrator".
  3. Log out again.
  4. 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)
  5. Go back to TTY7 (Ctrl+Alt+F7) to login on the GUI/normal desktop screen and see if this works.
  6. Delete temporary user and folder:

    sudo deluser temporary
    sudo rm -r /home/temporary
    
share|improve this answer
6  
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 8 '11 at 9:17
    
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 at 17:12
    
Was a simple fix -- Just had to use the User Accounts editor in unity, followed by restart. –  Steve Koch Feb 24 at 17:29

To put it all together:

  1. At the start screen press Ctrl+ALT+F1.
  2. Log in using your username and password.
  3. Set a password for the "root" account.
    sudo passwd root
  4. Log out.
    exit
  5. Log in using the "root" account and the password you have previously set.
  6. Change the username and the home folder to the new name that you want.
    usermod -l <newname> -d /home/<newname> -m <oldname>
  7. Change the group name to the new name that you want.
    groupmod -n <newgroup> <oldgroup>
  8. Look the "root" account.
    passwd -l root
  9. 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.
  10. Log out.
    exit
  11. press Ctrl+ALT+F7.

And now you can log in using your new username.

share|improve this answer

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>
share|improve this answer
    
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 –  Rana Muhammad Waqas Nov 24 '13 at 13:48

(Using Ubuntu 13.10)   Click on the "System Settings" icon.   Then click on "User Accounts".   Your administrator account should be displayed.   Click on the "Unlock" button.   Enter your user password as requested to allow changes to your account.   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.   When you have typed in the new name, click on the "Lock" button to make the change permanent.   Restart Ubuntu.

share|improve this answer

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>
share|improve this answer

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

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.