How do I change my username?

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 (it is probably best to do this without being logged in):

sudo usermod -l 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
    
  3. Log out with the command exit.
  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
    

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>
    
  8. Change the group name to the new name that you want.

     groupmod -n <newgroup> <oldgroup>
    
  9. Lock the "root" account.

     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.


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>

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.

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>