8

I've misspelled user name when installing Ubuntu and would like to rename if possible. How can I safely change my user name?

3
  • 2
    This answers may still be valid.
    – Takkat
    Oct 17, 2011 at 20:11
  • @takkat thanks. There you've got a good formal answer on how to rename a user. But I believe Jo-Erlend's suggestion I've got here is safer :-)
    – Ivan
    Oct 17, 2011 at 20:25
  • If you haven't done much with your user, I'd definitely go for Jo-Erlends approach. I had other people in mind that may come here for a more sophisticated solution on this topic. Renaming users is not a trivial task.
    – Takkat
    Oct 17, 2011 at 20:41

3 Answers 3

6

Safely? Don't rename at all. Instead, (make sure the user is logged out first) create a new user with the correct username, rename the old home directory to the new username and chown -R the folder to the new user.

This assumes that you're not using encrypted homes. That would require some different steps, but since I haven't done that myself, I'll leave that to someone else.

6

Use usermod like so:

usermod -l newname currentname
0

I have recently faces such problem and got working solution.

  1. Make backup of the users- and groups-related files

    sudo cp /etc/passwd ~/passwd.bak
    sudo cp /etc/group ~/group.bak
    sudo cp /etc/subuid ~/subuid.bak
    sudo cp /etc/subgid ~/subgid.bak
    sudo cp /etc/gshadow ~/gshadow.bak
    sudo cp /etc/shadow ~/shadow.bak
    
  2. We need to boot in Recovery mode (click Shift on boot, in GRUB select Advanced options for Ubuntu, select Ubuntu, with Linux ... (recovery mode))

  3. Select root Drop to root shell prompt to access root-shell
  4. Enter your main user (usually GID = 1000, member of sudo group) password here when asked

    Press Enter for maintenance
    (or press Control-D to continue):
    
  5. Remount all filesystems with read and write

    mount -o rw,remount /
    mount -o rw,remount /home
    
  6. Rename user from old_user to new_user, rename comment on user-name (field 5 in /etc/passwd) and move its home

    OLDNAME="old_user"
    NEWNAME="new_user"
    usermod -l $NEWNAME $OLDNAME -c $NEWNAME -d /home/$NEWNAME  -m
    
  7. Rename user's group from old_user to new_user

    groupmod $OLDNAME -n $NEWNAME
    
  8. Replace old_user to new_user in /etc/subgid and /etc/subuid

    sed -i "s/$OLDNAME/$NEWNAME/g" /etc/subuid
    sed -i "s/$OLDNAME/$NEWNAME/g" /etc/subgid
    
  9. Reboot the system and then login with new_user name and old password. Optionally change password with passwd

  10. If goes right - remove backed-up files:

    sudo rm ~/passwd.bak ~/group.bak ~/subuid.bak ~/subgid.bak \
    ~/gshadow.bak ~/shadow.bak
    

Note: you can read about used options of usermod and groupmod on their man-pages.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .