One missing letter in usermod costs people their sudo access. Type usermod -G docker alice instead of usermod -aG docker alice and Alice is in docker and nothing else. The sudo or wheel membership she had a second ago is gone, and the command printed nothing to warn you. Linux user and group management is full of commands like that, where the tool follows its man page to the letter and you still end up somewhere you didn’t mean to be.
This Linux user management cheat sheet puts the commands up front in tables: useradd, usermod, userdel, groupadd, gpasswd, passwd, chage, id, getent and sudoers drop-ins. After the tables, each trap gets proved with real output, including the ones the distros disagree on. Everything below was run on Ubuntu 26.04, Ubuntu 24.04, Rocky Linux 10 and Rocky Linux 9 in November 2026, so you’ll see where sudo-rs on Ubuntu 26.04 behaves differently from classic sudo.
Linux user management cheat sheet: the quick reference
Use the tables when you know the job and need the flag. The sections further down show the lab output and the edge case for each group, and where Ubuntu says sudo for the admin group, Rocky says wheel. Everything that writes to the account files needs root, so prefix those commands with sudo; id, groups and getent passwd work as any user.
Create users and groups
| Command | What it does |
|---|---|
useradd -m -s /bin/bash -c 'Dev One' dev1 | Create a user with a home directory, bash and a comment |
useradd -m -G sudo,devops dev1 | Create a user and add supplementary groups in one step |
useradd -u 2050 -g ops dev1 | Pick the UID and the primary group |
useradd -r -s /usr/sbin/nologin -d /var/lib/appsvc -M appsvc | System account for a service, no login, no home created |
useradd -D | Print the defaults useradd applies |
groupadd devops | Create a group |
groupadd -g 5000 ops | Create a group with a fixed GID |
echo 'dev1:S3cret' | chpasswd | Set a password non-interactively |
Change an existing account with usermod
| Command | What it does |
|---|---|
usermod -aG devops dev1 | Append a supplementary group (keeps the others) |
usermod -G ops dev1 | Replace ALL supplementary groups with ops |
usermod -s /bin/bash dev1 | Change the login shell |
usermod -c 'Dev One' dev1 | Change the comment (GECOS) field |
usermod -L dev1 / -U | Lock or unlock the password |
usermod -e 2027-06-30 dev1 | Expire the whole account on a date |
usermod -l dev2new -d /home/dev2new -m dev2 | Rename the login and move its home |
groupmod -n dev2new dev2 | Rename the matching primary group |
usermod -u 2050 dev1 | Change the UID and re-own the home directory |
Group membership with gpasswd
| Command | What it does |
|---|---|
gpasswd -a dev2 devops | Add one user to a group |
gpasswd -d dev2 devops | Remove one user from a group |
gpasswd -M dev1,dev2 ops | Set the full member list (replaces it) |
gpasswd -A dev1 ops | Make dev1 a group administrator |
groupmod -n newname oldname | Rename a group |
groupdel devops | Delete a group (fails if it is someone’s primary group) |
Passwords, locking and aging
| Command | What it does |
|---|---|
passwd dev1 | Set a password interactively |
passwd -S dev1 | Status: L locked, P usable, NP no password (LK/PS on Rocky 9) |
passwd -l dev1 / -u | Lock or unlock the password |
passwd -e dev1 | Force a password change at next login |
chage -l dev1 | Show aging and expiry dates |
chage -M 90 -m 1 -W 7 -I 14 dev1 | Max age, min age, warn days, inactive days |
chage -E 2027-03-31 dev1 | Account expiry date (-E -1 clears it) |
chage -d 0 dev1 | Force a change at next login |
Look up users and groups
| Command | What it does |
|---|---|
id dev1 | UID, primary GID and every group |
id -nG dev1 | Group names only, read from the database |
groups dev1 | Same list, older format |
getent passwd dev1 | Account entry from files, LDAP or SSSD |
getent group devops | Group entry with its members |
getent group 10 27 | Look groups up by GID |
sudo getent shadow dev1 | Aging fields (root only) |
pwck -r / grpck -r | Read-only consistency check of the account files |
sudo access with drop-in files
| Command | What it does |
|---|---|
usermod -aG sudo dev1 | Full sudo on Ubuntu |
usermod -aG wheel dev1 | Full sudo on Rocky |
visudo -f /etc/sudoers.d/devops | Edit a drop-in with a syntax check on save |
chmod 0440 /etc/sudoers.d/devops | Permissions sudo expects on a drop-in |
visudo -cf /etc/sudoers.d/devops | Check one drop-in without loading it |
visudo -c | Check the whole sudoers configuration |
sudo -l -U dev1 | List what dev1 may run |
Delete and clean up
| Command | What it does |
|---|---|
pkill -u dev1 | Stop the user’s processes first |
userdel -r dev1 | Delete the user, home directory and mail spool |
userdel dev1 | Delete the user, leave the home behind |
find / -xdev -nouser | Find files owned by a deleted UID |
chown -R newowner: /home/dev1 | Hand orphaned files to someone |
The two rows that cause the most damage are usermod -G without -a and userdel without -r. Both succeed silently and leave the system in a state you only notice later, which the sections below prove one capture at a time.
usermod -G replaces, -aG appends
usermod -G sets the complete list of supplementary groups rather than adding to it. The -a flag is what turns it into an append, and it only works together with -G. That is the whole story of the most common lockout on a fresh server: you add yourself to docker or libvirt, forget the a, and lose sudo or wheel in the same command.
What does that look like from id? On Ubuntu 26.04, dev1 starts in devops, gets -G ops, then -aG devops,sudo.

Read the middle id line. After usermod -G ops dev1 the devops membership is gone and only ops is left, with no warning and exit status 0. The last command shows the safe form: -aG with a comma-separated list appends several groups at once. If you lock yourself out of sudo this way, you need another admin account or the console to put the group back, so do group changes from a second session that’s already root.
usermod: group ‘nosuchgroup’ does not exist
usermod checks every group in the list before it changes anything, so a typo fails the whole command rather than adding half the groups. Try it with a group that doesn’t exist.
sudo usermod -aG nosuchgroup dev1
All four distros print the same error and leave the account untouched.
usermod: group 'nosuchgroup' does not exist
Create the group with groupadd first. If it already exists, groupadd tells you with groupadd: group 'devops' already exists, and useradd does the same for users with useradd: user 'dev1' already exists. In a script, getent group devops || sudo groupadd devops skips that error cleanly.
useradd defaults differ by distro
You would expect useradd dev1 to do the same thing everywhere. It does not, because useradd reads its defaults from /etc/default/useradd and /etc/login.defs, and Ubuntu and Rocky ship different values in both. On Ubuntu a bare useradd creates no home directory and gives the user /bin/sh. On Rocky the same command creates a home directory and gives bash.
The capture below prints the Ubuntu 26.04 defaults first, then runs a bare useradd plain1 next to the explicit form.

plain1 got /home/plain1 written into its passwd entry, but the directory does not exist, so its first login lands in / with a sh prompt. The second user, created with -m -s /bin/bash, has a home with mode drwxr-x--- (0750, from HOME_MODE). Always pass -m and -s on Ubuntu, or use adduser, which asks the questions for you.
Rocky Linux 10 runs the same bare command and gets a different account.

Rocky creates the home with mode 0700 (drwx------., the trailing dot is the SELinux context) and sets bash because its login.defs has CREATE_HOME yes and its defaults file says SHELL=/bin/bash. The explicit form gives the same result on every distro, which is why your scripts should spell the flags out. Which values drive this on your own host? One grep shows them.
grep -E '^(UID_MIN|UID_MAX|SYS_UID_MIN|CREATE_HOME|HOME_MODE|ENCRYPT_METHOD)' /etc/login.defs
Rocky Linux 10 returns these lines, with YESCRYPT as the configured password hash.
HOME_MODE 0700
UID_MIN 1000
UID_MAX 60000
SYS_UID_MIN 201
ENCRYPT_METHOD YESCRYPT
CREATE_HOME yes
Ubuntu 26.04 also says YESCRYPT, while Ubuntu 24.04 and Rocky 9 still say SHA512. Don’t trust that line on Ubuntu, though. Its chpasswd and passwd hash through PAM, and Ubuntu’s PAM config asks for yescrypt, so a password set on Ubuntu 24.04 was stored with a $y$ prefix even though login.defs says SHA512. Only Rocky 9 stored $6$ (SHA512). Check what you really have with sudo getent shadow dev1 | cut -d: -f2 | cut -c1-3 before you copy hashes between hosts with usermod -p or chpasswd -e. All four use UID_MIN 1000, so regular users start at 1000 everywhere.
System accounts get UIDs below 1000
A service account wants no login shell, no password and a UID from the system range. -r picks the UID from the top of that range downwards.
sudo useradd -r -s /usr/sbin/nologin -d /var/lib/appsvc -M appsvc
getent passwd appsvc
On Ubuntu 26.04 that gave UID 999, while Rocky Linux 10 handed out 995 because a few system accounts already sat at the top of its range.
appsvc:x:995:995::/var/lib/appsvc:/usr/sbin/nologin
-M means the directory is not created, so the package or the systemd unit has to make it. If your unit runs with StateDirectory=appsvc, systemd creates /var/lib/appsvc with the right owner on start.
passwd -l locks the password, not the account
passwd -l puts a ! in front of the hash in /etc/shadow. That stops password logins, and that is all it stops. The passwd man page on all four systems says so directly: the user may still be able to log in with another authentication token, such as an SSH key. If you’re offboarding someone, lock the password and also expire the account. The same page on Ubuntu and Rocky 10 recommends usermod --expiredate 1, and Rocky 9’s passwd page points to chage -E 0; either one sets an expiry date in the past.
passwd -S is the quickest way for you to see which state an account is in. Rocky Linux 9 prints the verbose form.

A new account starts as LK because it has no password yet. After chpasswd it reads PS with the hash type. passwd -e does not touch the hash at all. It sets the last-change date to 1970-01-01, which is how the system knows to demand a new password at the next login. Ubuntu and Rocky Linux 10 print the same fields with short codes (L, P, NP) and no text in brackets, and their passwd -l just says passwd: password changed.
passwd: unrecognized option ‘–stdin’
passwd --stdin used to be a Red Hat extension, and plenty of scripts you inherit still use it.
echo 'Lab-Only-Pass-2026' | sudo passwd --stdin ops1
Rocky 9 still ships Red Hat’s own passwd, so it accepts the flag. Rocky 10 and Ubuntu 26.04 accept it because upstream shadow added -s, --stdin to its passwd in 4.15, and both ship 4.15 or newer. Ubuntu 24.04 is on shadow 4.13 and rejects it.
passwd: unrecognized option '--stdin'
Usage: passwd [options] [LOGIN]
Use chpasswd instead. It reads user:password lines on stdin and works on every distro in this test, so a script written with it does not care which passwd it finds.
chage counts from the last change
Password aging in chage counts days from an event rather than naming a calendar date, and only one of the three flags people mix up takes a real date:
-M 90means 90 days after the last password change-I 14means 14 days after that password expiry-E 2027-03-31is a fixed date for the whole account
Mixing those up is how an account ends up dead weeks before anyone expected. Here is Ubuntu 24.04 with a contractor account that needs all of it.

The password was set on Nov 13, 2026, so -M 90 expires it on Feb 11, 2027, and the 14 inactive days carry that to Feb 25. The account itself stops on Mar 31, 2027 whatever the password does. After chage -d 0, the first three lines all read password must be changed and passwd -S shows the last-change date as 1970-01-01, the same trick passwd -e uses. If you’re setting policy for many accounts, our guide to password aging and expiry policy covers the login.defs defaults that apply to new users.
Account expiry also has a usermod form, which is handy when you’re already editing the user.
sudo usermod -e 2027-06-30 dev2
sudo chage -l dev2
The password lines stay at their defaults while the account line picks up the date.
Last password change : Nov 13, 2026
Password expires : never
Password inactive : never
Account expires : Jun 30, 2027
Minimum number of days between password change : 0
Maximum number of days between password change : 99999
Number of days of warning before password expires : 7
chage -E -1 dev2 clears it again. Shadow fields are readable only by root, so getent shadow dev2 without sudo returns nothing at all rather than an error.
gpasswd edits the group, not the user
usermod works from the user’s side, where you name dev1 and set its groups. gpasswd works from the group’s side, which is what you want when you’re adding or removing one person without touching anything else they belong to. There is no -a to forget, because gpasswd -a and -d only ever change one membership. This run is from Rocky Linux 10.

Two flags behave differently from the rest, and you should know both before you script them.
-Mreplaces the whole member list, the group-side twin ofusermod -G.-Awrites a group admin into/etc/gshadow(ops:!:dev1:dev1,dev2).- That admin can then run
gpasswd -aon the group without sudo (tested on all four).
Use -M only when you mean “exactly these people”, for example from a config management run. For that kind of repeatable membership, the Ansible user and group modules make the intent explicit with append: true.
Group changes need a new login
Adding a user to a group changes /etc/group. It does not change any process that is already running. The kernel copies the supplementary group list into a process at login, and every child inherits that copy. So id -nG in your current shell shows the old groups, while id -nG yourname reads the files and shows the new ones. That’s why “I added myself to docker and still get permission denied” is usually fixed by logging out, not by more usermod.
You can watch the two copies disagree. On Rocky Linux 10, ops1 started a sleep, then got added to auditors.

id -nG ops1 includes auditors. The running sleep’s SUPGRP column still says wheel,devops,ops1, because its group list was fixed when it started. A fresh login with sudo -iu ops1 picks up auditors straight away. Anything started before the change keeps the old list:
- your current SSH or desktop session
- tmux or screen servers started earlier
- daemons running as that user
A daemon needs a restart before a new group grants it anything, and a tmux server needs to be killed, not just detached. Our walkthrough on using virt-manager as a non-root user hits exactly this with the libvirt group.
Admin rights come from a group
There is no “admin” flag on a Linux account. Full sudo comes from a line in /etc/sudoers that grants it to a group, and the group differs by family. You can see the lines without opening the file.
sudo grep -nE 'includedir|^%' /etc/sudoers
Ubuntu 24.04 grants it to sudo (and the legacy admin group) and reads the drop-in directory.
50:%admin ALL=(ALL) ALL
53:%sudo ALL=(ALL:ALL) ALL
57:@includedir /etc/sudoers.d
Rocky has %wheel ALL=(ALL) ALL on line 107 and #includedir /etc/sudoers.d on line 120. That #includedir is not a comment. It is the old spelling of the same directive. So usermod -aG sudo on Ubuntu and usermod -aG wheel on Rocky are the whole job for full access. For anything narrower, write a drop-in rather than editing the main file. The FreeBSD sudo guide uses the same drop-in layout under /usr/local/etc.
Write and check a sudoers drop-in
This drop-in lets the devops group restart nginx and read the journal, nothing else.
echo '%devops ALL=(root) /usr/bin/systemctl restart nginx, /usr/bin/journalctl' | sudo tee /etc/sudoers.d/devops
sudo chmod 0440 /etc/sudoers.d/devops
sudo visudo -cf /etc/sudoers.d/devops
The check passes before sudo ever loads the file.
/etc/sudoers.d/devops: parsed OK
Then ask sudo what a member of devops may actually run. This is the check that proves the rule is live, because a file can parse cleanly and still be ignored.
sudo -l -U dev1
On Ubuntu 24.04 the restricted rule shows up under the full one dev1 already had from the sudo group.
Matching Defaults entries for dev1 on probe-u2404:
env_reset, mail_badpass, secure_path=/usr/local/sbin\:/usr/local/bin\:/usr/sbin\:/usr/bin\:/sbin\:/bin\:/snap/bin, use_pty
User dev1 may run the following commands on probe-u2404:
(ALL : ALL) ALL
(root) /usr/bin/systemctl restart nginx, /usr/bin/journalctl
On a production box, sudo visudo -f /etc/sudoers.d/devops is the safer way to write the same rule, because visudo checks the file before it saves it, while the tee above makes it live before visudo -cf has looked at it. sudo-rs on Ubuntu 26.04 lists the same rules but skips the Matching Defaults entries block. Normal users cannot even list the directory (ls /etc/sudoers.d gives Permission denied), so run these checks as root.
A dot in the filename disables the rule
Calling a drop-in dev2new.conf feels tidy and does nothing. The sudoers manual says @includedir skips any file whose name ends in ~ or contains a ., so editor backups and package-manager leftovers never load. Your .conf file falls in the same bucket, as this Rocky Linux 10 run shows.

With the .conf name, sudo -l -U dev2new reports that the user isn’t allowed to run sudo at all. After mv to a plain name, the NOPASSWD rule appears. All four distros behaved the same, sudo-rs included, and none of them warned about the skipped file. Going by the manual’s rule, these are the names sudo will and won’t read:
devopsor10-devopsloaddevops_nginxloads (underscore is fine)devops.confanddevops~are skipped
The sudo -l -U check after every new drop-in is what catches a skipped file, since no tool reports it on its own.
visudo checks syntax, sudo enforces permissions
People treat visudo -c as the final word on sudoers. It is a syntax checker, and the two sudo implementations disagree on what else it reports. Ubuntu 26.04 ships sudo-rs as sudo, so the difference shows up on your default install there.

Read that capture closely before you trust visudo again, because it shows three behaviours that classic sudo users don’t expect.
- sudo-rs
visudo -cprints only/etc/sudoers: parsed OK, not one line per drop-in. - It accepts a 0664 drop-in silently; classic sudo warns
bad permissions, should be mode 0440. - A broken drop-in doesn’t stop sudo: it prints the error and still runs
sudo true.
The last point surprises people who expect a parse error to lock everyone out. On all four VMs, sudo kept working with a bad drop-in in place, printing the parse error on every call. Don’t count on it as a safety net. The sudoers manual describes this recovery as discarding the part of the line that holds the error, and if that line is your %sudo or %wheel grant, you’ve lost admin access anyway. That’s why you edit through visudo and keep a root shell open.
syntax error: expecting ‘:’ but found ‘A’
That is sudo-rs telling you a tag like NOPASSWD is missing its colon. Classic sudo reports the same mistake at the same column with a plain syntax error.
/etc/sudoers.d/broken:1:25: syntax error
dev1 ALL=(ALL) NOPASSWD ALL
^~~
The fix is NOPASSWD: ALL. The column number (25) points at the character after the tag, so count from there when the line is long.
sudo: /etc/sudoers.d/perm1 is world writable
Permissions are where sudo draws a hard line. A drop-in at mode 0666 is ignored by both implementations, and the user loses whatever it granted. Classic sudo on Rocky Linux 10 says it on every sudo call.
sudo: /etc/sudoers.d/perm1 is world writable
User perm1 is not allowed to run sudo on probe-r10.
sudo-rs words it as /etc/sudoers.d/perm1 cannot be world-writable and its visudo fails with visudo: invalid sudoers file. Classic visudo -c on Rocky only warned about bad permissions and still printed parsed OK, so it is the sudo -l -U check, not visudo, that tells you the rule is dead. chmod 0440 brings it back on both.
userdel leaves files for the next UID
userdel without -r removes the account and leaves its home directory owned by a bare number. That looks harmless until the next useradd, which takes the next UID after the highest one in use. When the deleted user held that highest UID, the number comes straight back. The new user then owns the old user’s files. Want to see it happen? Ubuntu 24.04 did it on the first try.

tmp1 had UID 2051. After userdel tmp1, /home/tmp1 showed 2051 2051 as owner. The next account, newhire, got 2051 too, and ls now shows newhire owning tmp1’s home. The find -nouser sweep at the end returns nothing, because those files have an owner again. All four distros reused the UID the same way. Run find / -xdev -nouser right after a userdel without -r, before anyone else gets created, and decide where those files go.
userdel: user dev1 is currently used by process
userdel refuses to remove an account that still has processes, which is the right call.
sudo userdel -r dev1
The error names the PID it found.
userdel: user dev1 is currently used by process 2656
The safe order for removing someone is the same on every distro:
- Lock and expire the account with
usermod -L -e 1 - Stop their processes with
pkill -u dev1 - Delete with
userdel -r dev1 - Sweep for leftovers with
find / -xdev -nouser
Locking first matters because a user who’s still logged in can start new processes between your pkill and your userdel. On Ubuntu you’ll also see userdel: dev1 mail spool (/var/mail/dev1) not found, which is only a note. Ubuntu does not create mail spools by default (CREATE_MAIL_SPOOL=no), and the delete still succeeds.
groupdel: cannot remove the primary group of user
A group that is still someone’s primary group cannot be deleted on its own, and you’ll hit that most often after a rename, where usermod -l changed the login but left the group with the old name. Delete the user with userdel -r and its private group goes with it, which is why a follow-up groupdel dev2new reports group 'dev2new' does not exist.
Ubuntu and Rocky disagree on the details
The commands are the same shadow tools on all four systems, but the defaults and a few helpers are not. This table holds the differences the lab turned up, so you can check your target before you script against it.
| Behaviour | Ubuntu 26.04 | Ubuntu 24.04 | Rocky 10 | Rocky 9 |
|---|---|---|---|---|
Bare useradd home | Not created | Not created | Created, 0700 | Created, 0700 |
| Default shell | /bin/sh | /bin/sh | /bin/bash | /bin/bash |
ENCRYPT_METHOD in login.defs | YESCRYPT | SHA512 | YESCRYPT | SHA512 |
Hash stored by chpasswd | $y$ yescrypt | $y$ yescrypt | $y$ yescrypt | $6$ SHA512 |
| Admin group | sudo | sudo | wheel | wheel |
| sudo | sudo-rs 0.2.13 | sudo 1.9.15p5 | sudo 1.9.17p2 | sudo 1.9.17p2 |
passwd --stdin | Works | Unrecognized | Works | Works |
chsh installed | Yes | Yes | Yes | No (util-linux-user) |
adduser | Interactive Perl tool | Interactive Perl tool | Link to useradd | Link to useradd |
getent passwd nobody home | /nonexistent | /nonexistent | / | / |
The adduser row trips people moving between families. On Rocky, adduser --disabled-password tester fails with adduser: unrecognized option '--disabled-password', because it is useradd under another name. The chsh row is similar. Rocky 9 answers sudo: chsh: command not found until you install util-linux-user, Ubuntu 24.04 changes the shell silently, and chsh warns on the other two: Ubuntu 26.04 calls nologin an invalid shell, and Rocky 10 says it is not listed in /etc/shells. If you want one command for every distro here, use these:
usermod -s /usr/sbin/nologin dev1to disable a shelluseradd -m -s /bin/bash dev1instead of adduserchpasswdinstead ofpasswd --stdin
usermod changed the shell silently on all four VMs. You get the same account on Ubuntu and Rocky, and a script built from those three lines doesn’t need a distro check.
Prove the account from outside
Most of the mistakes above exit with status 0, so the command’s silence proves nothing. After any change to a user, read the result back from the system instead.
id -nG dev1for the group list the next login will get.sudo -l -U dev1for the sudo rules that actually loaded.sudo chage -l dev1andsudo passwd -S dev1for lock and expiry state.sudo find / -xdev -nouserafter every delete.
Those four checks would have caught every trap in this sheet before a user did. If you are preparing for a Red Hat exam, the same commands come up again and again in our RHCSA practice questions. The grep cheat sheet and the SSH commands cheat sheet cover what you’ll reach for next on a new server. For packages, you have the APT cheat sheet and the DNF5 cheat sheet, and every sheet with its PDF lives on the cheat sheets hub.
Pin the PDF where you’ll see it, and when an account change looks done, check it with id and sudo -l -U before you close the root shell. If either shows something you didn’t expect, you still have the session you need to fix it.