Rename user on Linux and Windows
5/15/2021, 12:29:40 PM

Recently I needed to rename my username on some Linux and Windows machines.

Here’re the right way to rename a user on those operating systems. (And correctly rename user home dir)

For Linux

  • Setup another admin account, or unlock root account
  • Stop all systemd services running using that user
  • Disable or remove all systemd services that run as that user
  • Logout, then login with another admin account (or root)
    • You must ensure that the user has NO running process
  • Open terminal (replace $NEW_USER and $OLD_USER to your own need)
    • Run: usermod --login $NEW_USER --move-home --home /home/$NEW_USER $OLD_USER
    • Run: groupmod --new-name $NEW_USER $OLD_USER
  • Login with that user. Done!
  • You may need to:
    • Modify .zshrc to update oh-my-zsh dir
    • Modify ssh public key username

For Windows

  • Open cmd.exe
  • Run: control userpasswords2, modify username in the pop-up window
  • Run: regedit
    • Go to HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\Windows NT\CurrentVersion\ProfileList
    • Found the subdir whose ProfileImagePath record contains your current home dir
    • Edit this record’s value to your wanted home dir
  • Logout and login. Don’t worry if your user settings (eg. background image) and desktop file is missing!
  • If your new home dir exists, delete it
  • Rename your old home dir to your wanted home dir
  • Logout and login. Done!
  • You may need to
    • Re-setup onedrive
    • Re-install some apps installed in user’s AppData dir