Amazon Cyber Monday deals See deals →ad

Mets Minor League Blog

Music theory, instruments and musicians

Mets Minor League Blog

Music theory, instruments and musicians

Linux Change User Name: Safe Commands and Home Folder Tips

To change a Linux login name, use usermod from a different administrator account: run sudo usermod --login newname oldname. This changes the account’s login name, but not its home-directory path; move or rename the home directory separately if you want it to match.

What a Linux username change actually changes

A Linux account has several distinct pieces of information that people often call a “username.” The login name is the account identifier you type at a login prompt or use with commands such as su. A user’s real name or display name is separate, as are the home-directory path, numeric user ID (UID), primary group, and supplementary groups. Renaming one of these does not automatically rename all the others.

The usermod --login option changes the login name recorded for an existing account. It normally leaves the UID and group membership intact. Keeping the same UID matters: Linux file ownership is stored numerically, so files owned by the account continue to belong to it after a login-name change. The name shown for that numeric ID is resolved from the account database.

For example, changing alex to alexander can leave the home directory at /home/alex. That is valid, but it can be confusing. If you want /home/alexander, move the home directory as a separate step and update the account’s configured home path.

Before you rename the account

Do the work from a different administrator account, not from a terminal or desktop session belonging to the account being renamed. A logged-in user may have running processes, open files, or services that still use the old name or home path. Some systems refuse a change while the account is in use; forcing the issue can leave processes or applications in an awkward state.

First make sure you have a working administrator login and a current backup of important files. If this is a remote server, keep an existing administrative session open while testing a new login, so a mistake does not lock you out. Check the account’s current details with:

  • id oldname to see its UID and group memberships.
  • getent passwd oldname to see the account record, including its home path and login shell.
  • ls -ld /home/oldname to inspect the home directory, if it is located there.

Choose a name that is not already in use. Common Linux account-name conventions favor lowercase letters, digits, underscores, and hyphens, though accepted characters and policy can differ by distribution and local configuration. Do not use a name already assigned to another account or service. You can check a proposed name with getent passwd newname; no output usually means no matching account record was found, though other local identity sources may also be configured.

Rename the login name with usermod

Log in as the separate administrator and replace the example names with the real ones. The first command changes the login name:

sudo usermod --login newname oldname

On many distributions, the shorter form sudo usermod -l newname oldname does the same thing. The final argument is the existing account name; the value after --login is the new one. Use the exact spelling, and do not include angle brackets from examples.

Verify the result with getent passwd newname and id newname. The account should now be found under the new login name. If the command reports that the user is in use, log out that account and stop its relevant processes or services before trying again. Avoid killing processes blindly: identify what they do first, especially on a server.

Move the home directory too, if appropriate

Renaming the login does not move the home directory. If you want the directory name to follow the new login, update the home path and ask usermod to move the existing contents:

sudo usermod --home /home/newname --move-home newname

The account must already have the new login name for this example. On distributions that use a different home-directory layout, substitute the correct path; for example, some systems or administrators store users outside /home. The --move-home option moves the current home directory to the new location while changing the account’s configured home path.

Check that the old directory existed and that the destination does not already contain unrelated files before running the move. If the account’s home is on a separate mount, network share, encrypted volume, or managed by a desktop or enterprise tool, follow that setup’s procedure instead of assuming a simple local rename is sufficient. Afterward, verify with getent passwd newname and ls -ld /home/newname.

Decide whether to rename the group

Many Linux installations create a private primary group with the same name as each user. After changing oldname to newname, the account might still have a primary group called oldname. That is not inherently an error: users and groups are separate records, and the account can continue using the same group ID.

If you want the group name to match, first inspect it with id newname and getent group oldname. If the old group is the user’s private group and the new group name is free, an administrator can rename it with:

sudo groupmod --new-name newname oldname

Do not rename a shared group just for appearance without checking who and what uses it. A group may grant access to shared files or devices. Renaming the group record does not change its numeric GID, and files owned by that GID remain associated with it, but scripts and configuration files that refer to the old group name may need attention.

What to check after the change

Test a fresh login using the new name before deleting or altering anything else. Then check the home directory, shell, and group membership. For example, getent passwd newname shows the account record, while id newname shows the UID and groups. A successful login is the most useful confirmation that authentication and the account record work together.

Most files owned by the account do not need an ownership change when only the login name changes, because the UID stays the same. If you also changed the UID, that is a different and more consequential operation: files outside the home directory may retain the old numeric ownership. Do not run broad ownership-changing commands merely because the displayed login name changed.

Look for software that stores the old name or old home path as text. Potential examples include scripts, scheduled jobs, application settings, backup rules, service definitions, SSH-related configuration, and paths in shared mounts. These references are not automatically rewritten by usermod. A graphical login manager may also display cached account information until it refreshes or the machine is restarted.

Common errors and recovery considerations

If usermod says the new name already exists, choose another unused name rather than trying to overwrite an account. If it cannot find the old account, confirm the exact spelling and check the system’s account sources. In organizations using LDAP, Active Directory, or another centralized identity service, local account tools may not be the right way to rename a user; make the change through the identity provider or follow the administrator’s process.

If the home-directory move fails because the destination exists, inspect both directories before proceeding. Do not delete the destination just to make the command succeed: it may contain data. If a command stops partway through, inspect the account record and both paths before retrying. Restoring from a backup or carefully moving the files back may be safer than repeating commands without knowing what already changed.

To undo a successful login-name change, use usermod --login oldname newname from another administrator account, provided the old name is free. If you also moved the home directory, restore its location and update the home path as a separate operation. Group renaming is likewise separate. This is why recording the original name, home path, UID, and group details before starting is useful.

Changing a display name instead

If the goal is only to change the person’s full name shown in account menus or some login screens, do not rename the login account. On many Linux distributions, the chfn command edits the account’s descriptive “finger” information; an administrator can also use the distribution’s user-management settings. The exact graphical options vary. This display information is not the same as the name used to authenticate.

Similarly, changing a desktop profile name, email address, or computer hostname does not change the Linux login name. Before making a system-level change, identify where the old name appears: at the login prompt, in the home-directory path, in a desktop display, or in a command prompt. The appropriate setting depends on which of those you mean.

When creating a new account is a better choice

Renaming is reasonable when the same person and account should retain its UID, files, permissions, and access. Creating a new account may be cleaner when transferring ownership to another person, separating work from personal files, or changing account policies. A new account has its own UID and permissions, so copying files requires deliberate decisions about ownership, hidden application settings, SSH keys, and access to shared resources.

For a personal machine, a practical alternative is to create a second administrator, verify that it works, and then rename the original account from that session. For a production system or centrally managed workstation, coordinate the change with backups, authentication, automation, and service owners. A name that appears simple at the shell can be referenced in many places across a managed environment.

Related

Frequently Asked Questions

Can I change my Linux username while logged in as that user?

It is not recommended. The account may have active processes, and some systems reject the change while it is in use. Log in as a different administrator, or use an appropriate recovery environment, and make sure the account being renamed is logged out.

Does changing the username rename the home folder?

No. usermod --login changes the login name only. To move the home directory as well, use usermod --home /new/path --move-home newname after checking that the destination is appropriate and available.

Will I lose my files or need to change file ownership?

Not when you change only the login name and keep the same UID. Linux file ownership is based on numeric IDs, not the text name displayed for the owner. A separate UID change can require additional ownership checks and should not be confused with a simple rename.

How do I change only the name shown on the login screen?

Change the account’s descriptive full-name information through your distribution’s user settings or a suitable account-information tool such as chfn. Do not use usermod --login unless you intend to change the actual login name.

Can I rename the root account?

Do not rename the root account as a routine customization. System tools and administrative procedures generally expect the standard root account and its UID. If the aim is to avoid direct root login, use a regular administrator account with sudo where supported and configure access according to the distribution’s guidance.

Linux Change User Name: Safe Commands and Home Folder Tips
Scroll to top