Skip to content

Change Account Username on Linux

Note

This page contains {{ jinja2_style_variable_syntax }} in example commands or configs. Replace them with the appropriate values when using the code or values in question.

I have, on occasion, wanted to change the username of a pre-configured account on a Linux system. For example, when I first wrote the original iteration of this page, 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, and created this page to document how I did it.

Preparation

You must have the ability to run commands with root-level permissions without logging in as the user you want to rename. See the end of this document for a method if you want to rename your only admin user

Ensure that the user you want to rename has no currently-running processes. The following command lists running processes for the user, and should not have any output:

ps -u {{ old_username }} --no-headers

Renaming the User

Run the following commands to rename the user, the user's home directory, and the user's primary group, which is typically named after that user.

# Rename the user itself
usermod -l {{ new_username }} {{ old_username }}
# Rename the user's home directory
usermod -d /home/{{ new_username }} -m {{ new_username }}
# Rename the user's primary group
groupmod -n {{ new_username }} {{ old_username }}

References