I've misspelled user name when installing Ubuntu and would like to rename if possible. How can I safely change my user name?
-
2This answers may still be valid.– TakkatOct 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 :-)– IvanOct 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.– TakkatOct 17, 2011 at 20:41
3 Answers
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.
I have recently faces such problem and got working solution.
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
We need to boot in Recovery mode (click Shift on boot, in GRUB select Advanced options for Ubuntu, select Ubuntu, with Linux ... (recovery mode))
- Select
root Drop to root shell prompt
to access root-shell 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):
Remount all filesystems with read and write
mount -o rw,remount / mount -o rw,remount /home
Rename user from old_user to new_user, rename comment on user-name (field 5 in
/etc/passwd
) and move its homeOLDNAME="old_user" NEWNAME="new_user" usermod -l $NEWNAME $OLDNAME -c $NEWNAME -d /home/$NEWNAME -m
Rename user's group from old_user to new_user
groupmod $OLDNAME -n $NEWNAME
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
Reboot the system and then login with new_user name and old password. Optionally change password with
passwd
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.