A user named alice was in a group called devs. One command later, meant to add her to ops as well, she was in ops and no longer in devs. The command was usermod -G ops alice, and it did exactly what its manual says. The fix is a single letter. That letter, the home directory that useradd did not create, and the group membership that a logged-in session cannot see are the three surprises this post walks through. Every command was run on an Ubuntu 24.04 LTS system, and the output shown is what it printed.
Creating users
useradd, bare
The low-level tool is useradd. Run with nothing but a name:
# useradd alice
# grep '^alice:' /etc/passwd
alice:x:30035:30035::/home/alice:/bin/sh
# ls -d /home/alice
ls: cannot access '/home/alice': No such file or directory
The account exists, with a home directory path recorded in /etc/passwd, but the directory itself was never made, and the login shell is /bin/sh. The useradd manual explains the first part. The -m option creates the home directory and copies in the files from the skeleton directory; without it, and without CREATE_HOME enabled in the system’s configuration, no home directory is created.
The password field in /etc/shadow was !, meaning no usable password yet. The account cannot log in with a password until one is set.
useradd with the options people actually want
# useradd -m -s /bin/bash bob
# grep '^bob:' /etc/passwd
bob:x:30036:30036::/home/bob:/bin/bash
# ls -A /home/bob
.bash_logout .bashrc .profile
Now the directory exists, with the three skeleton files copied from /etc/skel, and the shell is bash. On Debian and Ubuntu, adduser is a friendlier front end that does this and prompts for a password; useradd is the tool present on essentially every distribution and the one scripts should use.

What the numbers mean
UIDs and the private group
Every user has a numeric UID and a primary group. Alice got 30035 for both. The system’s /etc/login.defs set UID_MIN to 1000 and UID_MAX to 60000, the range for ordinary users, and USERGROUPS_ENAB yes, which is why each new user got a private group with their own name and a matching number. On a typical fresh install the first human user gets 1000; this machine already had many accounts, so the next free number was higher.
System accounts
Services should not run as a person. useradd -r creates a system account, which the manual describes as one created with no aging information in /etc/shadow and with an ID from the SYS_UID_MIN to SYS_UID_MAX range instead. A service account with no login shell:
# useradd -r -s /usr/sbin/nologin svcapp
# grep '^svcapp:' /etc/passwd
svcapp:x:994:989::/home/svcapp:/usr/sbin/nologin
UID 994, below 1000, counted down from the top of the system range. nologin as the shell means nobody can open an interactive session as that account, even with its credentials.
Where accounts are stored
Three files
Accounts live in plain text files, and getent reads them the way the system does. For a test user named carol, added to devs:
$ getent passwd carol
carol:x:30035:30035::/home/carol:/bin/bash
$ getent group devs
devs:x:30036:carol
The passwd line holds name, a placeholder x for the password, UID, primary GID, a comment field (empty here), home directory and shell. The group line lists the group’s name, its GID, and the members for whom it is a supplementary group, which is where usermod -aG writes.
The shadow file
The real password hash lives in /etc/shadow. The shadow manual says the file must not be readable by regular users if password security is to be maintained, and the test system’s permissions matched: /etc/passwd and /etc/group were readable by everyone (-rw-r--r--), /etc/shadow only by root and the shadow group (-rw-r-----).
After setting carol’s password, her shadow line began carol:$y$j...:20788:0:99999:7. The $y$ prefix marks the hashing scheme. The third field, 20788, is the date of the last password change, which the manual defines as the number of days since 1970-01-01; 20,788 days after that is the day of the test. The manual adds that a value of 0 there forces a password change at next login, which is how chage -d 0 makes a new user pick their own password.
Admin rights are a group
On Ubuntu, administrator access is just another group. getent group sudo listed its members, and /etc/sudoers contained the line %sudo ALL=(ALL:ALL) ALL, which grants the whole group the right to run anything as anyone. After usermod -aG sudo carol, a fresh session as carol asked sudo -n -l what she could do and was told a password was required, meaning the rule now applied to her. Adding someone to sudo is handing them root; removing them with gpasswd -d is how that is taken back.

The -G trap
What happened to alice
The test created a group devs, then added alice to it:
# usermod -G devs alice
# id alice
uid=30035(alice) gid=30035(alice) groups=30035(alice),30037(devs)
So far, so good. Then a second group, ops, using the same command shape:
# usermod -G ops alice
# id alice
uid=30035(alice) gid=30035(alice) groups=30035(alice),30038(ops)
devs is gone. The usermod manual says so plainly: -G sets the list of supplementary groups, and if the user is currently a member of a group which is not listed, the user will be removed from the group. -G does not add. It replaces.
-aG
The -a option changes that. The manual describes it as adding the user to the supplementary groups, and says it is to be used only with -G:
# usermod -aG devs alice
# id alice
uid=30035(alice) gid=30035(alice) groups=30035(alice),30037(devs),30038(ops)
Both groups. Every tutorial that says “add a user to the docker group” or “add a user to sudo” should be using -aG, and the ones that do not are a way to quietly remove someone from everything else.
Removing from one group
To take a user out of one group and leave the rest, gpasswd -d is the precise tool:
# gpasswd -d alice ops
Removing user alice from group ops
# id alice
uid=30035(alice) gid=30035(alice) groups=30035(alice),30037(devs)
Why the new group does not work yet
Sessions keep the groups they started with
The most common follow-up question after usermod -aG is why it did not take effect. The test showed it directly. A shell running as alice printed its groups, slept two seconds, and printed them again. During those two seconds, ops was added from another terminal:
alice devs (before)
alice devs (after the change, same session)
alice devs ops (a brand-new session)
id alice from root showed the new group immediately, because it reads the group database. The running session did not, because a process’s group list is set when it logs in and does not change underneath it. Logging out and back in picks it up. So does starting a new login session, such as a fresh SSH connection.
newgrp
For a quick fix within the same terminal, newgrp exists. Its manual describes it as changing the current group ID during a login session. It starts a new shell with the named group active. It is a workaround for one terminal, not a substitute for logging in again, and it leaves the original shell underneath.

Locking, expiring and removing
Locking a password
passwd -l alice locked the account’s password, and passwd -S alice reported its status with an L. The usermod manual’s note on its own -L option is worth reading: locking puts a ! in front of the encrypted password, effectively disabling the password, but to lock the account entirely, not only password access, the expiry date should also be set. A user with an SSH key can still log in to an account whose password is locked.
Expiry dates
chage -E sets the date an account stops working:
# chage -E 2026-06-01 bob
# chage -l bob | grep -i "account expires"
Account expires : Jun 01, 2026
For contractors and temporary staff, setting the end date when the account is created means the account closes itself on time even if nobody remembers.
userdel -r
userdel -r bob removed the account and its home directory; ls -d /home/bob afterward reported no such directory. It also reported that bob had no mail spool, which is informational. Without -r, the home directory stays behind, owned by a UID that no longer has a name, and the next user to be assigned that number inherits the files.
A short checklist
For people
useradd -m -s /bin/bash name, thenpasswd name, oradduser nameon Debian and Ubuntu.- Add groups with
usermod -aG group name. Never-Galone unless the full list is intended. - Tell the user to log out and back in after any group change.
- Set
chage -Efor any account with a known end date.
For services
useradd -r -s /usr/sbin/nologin name, with a home directory only if the service needs one.- Give it only the groups it needs, and check with
id name.
Sources
- useradd(8), Linux manual page (man7.org): https://man7.org/linux/man-pages/man8/useradd.8.html
- usermod(8), Linux manual page (man7.org): https://man7.org/linux/man-pages/man8/usermod.8.html
- shadow(5), Linux manual page (man7.org): https://man7.org/linux/man-pages/man5/shadow.5.html
- newgrp(1), Linux manual page (man7.org): https://man7.org/linux/man-pages/man1/newgrp.1.html
