Change username on Ubuntu

ubuntu_logo_81x81Changing username of an existing user is riskier than it sounds. There is a command to change the username but simply changing it is not enough. To get the renamed user account working (if the account is not a fresh one) follow the steps below. The procedure migrates one username to another. This is NOT for the faint of heart and should be done only if it is the last resort.

  • Logout of the desktop environment, press <Ctrl-Alt-F1> and login as root in the first virtual terminal.
  • Run the following to change the username
    # usermod -l newname oldname
  • Change the name of the home directory to new username
    # usermod -d /home/newname -m newname
  • Change any references to oldname in /etc/sudoers by editing it
    # sudoedit /etc/sudoers
  • At this point you can create a symbolic link to the new home directory with the old name and skip the rest of the steps. If you do not want to have an extra link under /home proceed further but the next steps are not guaranteed to ensure that everything will work fine.
    # ln -s /home/newname /home/oldname
  • Switch to new user
    # su newname
  • Following are the other common locations where there might be important references to the old username:
    Run the following command to find and replace occurrences of oldname with newname:

    $ find ./ -type f -exec sed -i "s/oldname/newname/g" {} \;
  • Change the 5th field (User ID info) in the entry for new username in /etc/passwd file.
  • Press <Ctrl-Alt-F7> to login with the new username.

Leave a Reply

Your email address will not be published. Required fields are marked *