I've misspelled user name when installing Ubuntu and would like to rename if possible. How can I safely change my user name?
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))
root Drop to root shell promptto 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 home
OLDNAME="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
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
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
groupmod on their man-pages.