Linux: Change Account Username - eliminmax/cncs-journal GitHub Wiki

Change Account Username

I have, on occasion, wanted to change the username of a pre-configured account on a Linux system. Most recently, I had to set up a Red Hat Enterprise Linux instance on EC2 for a class lab. The default username was ec2-user, but I prefer the username eliminmax, so I changed it. Here's how to do that:

Note: for the purposes of this tutorial, I'm writing commands to rename the user "old" to "new". Replace those words with the actual values when running the commands. It also assumes that you have root/superuser access on the system.

  1. Create a new user called temp with sudo access. This user should be deleted when you finish this process, but you can't rename a logged-in user.

    • Only necessary if you can't log in as root directly.

    • Depending on the configuration, you might need to add them to the group wheel, sudo, admin, et cetera - see Sudoers

  2. Log out, and log in as root if able, otherwise, log in as the new user, then elevate to root with: $ sudo -i

  3. Check that the account you want to rename is not logged in: # who

  4. Change the username: # usermod -l new old

  5. Change the user's home directory to match the new username: # usermod -d /home/new -m new

  6. Rename the user's primary group: # groupmod -n new old

  7. Log out, and log in as the original user. If you created a temporary user, delete it: $ sudo userdel temp -r


References: