Filters
Zo zoek je
  • Gewone woorden vinden namen, teksten en voorbeelden.
  • "twee woorden" vindt precies die zin.
  • --naam of -x vindt een optie.
  • #woord zoekt alleen in commando's.
  • @grep vindt lessen en labs die grep gebruiken.
  • Filters: cat:network · in:labs · in:notes · level:rhcsa · ch:8 · root:yes. Zet ! voor een woord om het weg te laten. OR tussen woorden betekent één ervan.
  • ↑ ↓ bewegen · Enter openen · Esc sluiten · / zoeken

usermod

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

Niveau
RHCSA-examen
Groep
Gebruikers en groepen
Root nodig
Ja
Pakket
shadow-utils

Dit deel is nog niet vertaald. Je ziet de Engelse tekst.

Wat het doet

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.

Zo schrijf je het

usermod [OPTION]... LOGIN

Opties 25

Meest gebruikt

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.

Voorbeelden 12

$ als gewone gebruiker# als root (of met 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.

Let op

  • 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).

Bestanden

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

Exitcodes

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