Filters
How to search
  • Plain words find names, texts and examples.
  • "two words" finds the exact phrase.
  • --name or -x finds an option.
  • #word looks only inside commands.
  • @grep finds lessons and labs that use grep.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Put ! before a word to leave it out. OR between words means one of them.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search

usermod

Change an existing user: groups, shell, home folder, name, UID, expire date, lock.

Level
RHCSA exam
Group
Users and groups
Needs root
Yes
Package
shadow-utils

What it does

usermod changes the account data in the user and group files. The most used job is adding a user to an extra group: usermod -aG GROUP USER.

Take care with -G: without -a it replaces all extra groups. A user who is logged in gets new groups only after a new login.

How to write it

usermod [OPTION]... LOGIN

Options 25

Most used

Groups

-a, --append
Add the groups of -G to the current ones (do not replace).
-G, --groups GROUP1[,GROUP2...]
The extra groups (alone it replaces the list).
-r, --remove
Remove the user from the groups of -G (newer versions).
-g, --gid GROUP
Change the primary group.

Account data

-c, --comment COMMENT
Change the comment (full name).
-s, --shell SHELL
Change the login shell.
-l, --login NEW_LOGIN
Rename the login.
-u, --uid UID
Change the UID (files in the home folder get the new owner).
-o, --non-unique
Allow a UID that is used already (with -u).
-e, --expiredate DATE
The account expires on this date (YYYY-MM-DD; empty = never).
-f, --inactive DAYS
Lock the account this many days after the password expired.
-p, --password HASH
Set an already hashed password.
-b, --badname
Allow names that do not follow the rules.

Lock

-L, --lock
Lock the password (puts ! before the hash).
-U, --unlock
Unlock the password.

Home folder

-d, --home DIR
Set a new home folder.
-m, --move-home
Move the files to the new home folder (with -d).

Other

-v, --add-subuids FIRST-LAST
Add a range of sub UIDs (for rootless containers).
-V, --del-subuids FIRST-LAST
Remove a range of sub UIDs.
-w, --add-subgids FIRST-LAST
Add a range of sub GIDs.
-W, --del-subgids FIRST-LAST
Remove a range of sub GIDs.
-Z, --selinux-user SEUSER
Set the SELinux user of the login.
-R, --root DIR
Work in this chroot folder.
-P, --prefix DIR
Use the files under this prefix folder.
-h, --help
Show help.

Examples 12

$ run as a normal user# run as root (or with sudo)

  1. 1Add a user to a group

    usermod -aG webadmins lisa

    lisa keeps all other groups.

  2. 2Give a user full sudo rights

    usermod -aG wheel lisa

    On RHEL wheel = all sudo rights.

  3. 3Block logins of a service account

    usermod -s /sbin/nologin backup1

    The account can still own files and run jobs.

  4. 4Lock and unlock a password

    usermod -L lisa
    usermod -U lisa

    A lock does not stop SSH key logins. Use an expire date too.

  5. 5Disable an account fully when someone leaves

    usermod -L -e 1 olduser

    -e 1 = expired on 2 Jan 1970: no login at all, also not with keys.

  6. 6Move the home folder

    usermod -d /srv/home/lisa -m lisa

    -m moves the files; without it only the entry changes.

  7. 7Rename a user

    usermod -l lisa.hassan lisa

    The home folder and group keep the old name; change them with -d -m and groupmod -n.

  8. 8Change the UID to match other servers

    usermod -u 1501 sam
    find / -xdev -uid 1500 -exec chown -h sam {} +

    Files outside the home folder keep the old number; fix them with find.

  9. 9Remove one extra group

    usermod -rG webadmins lisa

    Or use gpasswd -d lisa webadmins (works on all versions).

  10. 10Set an expire date

    usermod -e 2026-12-31 contractor1

    Same as chage -E 2026-12-31 contractor1.

  11. 11Give a user ranges for rootless podman

    usermod --add-subuids 100000-165535 --add-subgids 100000-165535 dev1

    Needed when the account was created without sub IDs.

  12. 12Check the result

    id lisa

    Shows all groups now.

Watch out

  • usermod -G group user without -a removes all other extra groups.
  • You cannot change the UID or name of a user who has running processes.
  • New groups work only after a new login (or newgrp).

Files

/etc/passwd, /etc/shadow, /etc/group, /etc/gshadow
The account files.
/etc/subuid, /etc/subgid
Sub ID ranges.

Exit codes

0Done.
1Could not update the password file.
2Wrong syntax.
3Wrong value for an option.
4UID already used.
6The user or group does not exist.
8The user is logged in (for changes that need logout).
9The new name is already used.
12Could not move the home folder.