Managing Users and Groups
Reading /etc/passwd and /etc/group tells you what exists. Actually creating an account, adding someone to a team’s group, or removing a user who’s left means going through dedicated commands instead — editing those files directly by hand is both unnecessary and risky, since a malformed line in /etc/passwd can break login for the entire system.
Every command in this chapter needs elevated privileges, since you’re changing system-wide account data — expect sudo in front of nearly everything.
Creating A User: useradd
sudo useradd aliceThis creates a bare-minimum account: an entry in /etc/passwd, an entry in /etc/shadow with no password set yet (meaning alice can’t actually log in until one’s set), and by default, depending on your system’s configuration, possibly no home directory at all. That last part catches people off guard — plain useradd on many Debian-based systems doesn’t create a home directory automatically, unlike some other distributions.
A more complete, realistic version:
sudo useradd -m -s /bin/bash alice-mcreates a home directory (/home/alice) if it doesn’t already exist, and populates it with default configuration files.-s /bin/bashsets the login shell explicitly, rather than leaving it unset or defaulting to something you didn’t intend.
You can also assign a primary group and additional supplementary groups at creation time:
sudo useradd -m -s /bin/bash -g developers -G sudo alice-g developerssetsdevelopersas the primary group (lowercaseg, singular).-G sudoaddsaliceto the supplementary groupsudo(uppercaseG, and it can take a comma-separated list for multiple groups at once).
Important: check the behaviour of -g
Important
-g and -G update different files, and mixing them up when checking membership later leads to genuinely confusing results.
-g <group>sets the user’s primary group. This is recorded in/etc/passwd(the GID field), not in/etc/group’s member list — so grepping/etc/groupfor a user set this way via-gwill show no members at all, even though the user genuinely belongs to that group.-G <group>sets supplementary groups. This is recorded in/etc/group’s member list, but does not show up in/etc/passwd.
Checking /etc/group alone will therefore miss primary-group membership entirely. The reliable way to see a user’s complete group membership — primary and supplementary together — is:
id -Gn aliceTrust this over manually grepping either file individually.
Tip
There’s a related command, adduser, available on Debian-based systems, that wraps useradd in a friendlier, interactive script — prompting you for a password and details step by step. useradd is the one covered here because it’s the portable, scriptable version present on essentially every Linux system, but recognize adduser if you see it in someone else’s instructions.
Creating System User
Every account created so far has been a regular human user, landing at UID 1000 or above, as covered in the User/Group Model chapter. Service accounts — the www-data, syslog-style entries you saw browsing /etc/passwd — are created differently, with -r (or the equivalent long form --system):
sudo useradd -r -s /usr/sbin/nologin -M appservice-rcreates a system account instead of a regular one — the account gets a UID from the lower, system-reserved range instead of counting up from 1000, marking it clearly as non-human at a glance in any/etc/passwdlisting.-s /usr/sbin/nologinsets a shell that refuses interactive login entirely — appropriate here, since nobody should ever be logging in as a service account directly.-Mexplicitly skips creating a home directory. Most system accounts don’t need one, and the-ralone doesn’t guarantee a home directory is skipped on every distribution, so it’s worth setting explicitly rather than assuming.
This is exactly the pattern you’d use when setting up a dedicated, isolated identity for a service to run under — the same principle covered back in the System Accounts vs. Human Accounts section: if that service is ever compromised, whatever it can access is limited to what appservice has permission to touch, nothing more.
Note
A system account created this way still shows up in /etc/passwd and can still own files and have permissions checked against it exactly like any other account — the only real differences are the UID range, the deliberately unusable shell, and the convention of skipping a home directory. “System user” isn’t a separate mechanism under the hood, just a deliberate, conventional configuration of the same account system covered in this chapter.
Clean up the same way as any other account:
sudo userdel appserviceSetting A Password
A newly created account has no usable password until you set one, using the same passwd command referenced back in the Special Bits chapter as the classic setuid example:
sudo passwd aliceThis prompts you to type — and confirm — a new password for alice, updating the hash in /etc/shadow directly. Run without a username, passwd changes the password for whoever is currently logged in, no sudo required, since you’re always allowed to change your own password.
Modifying An Existing User: usermod
usermod changes an account’s settings after it already exists — everything useradd sets at creation time, usermod can adjust afterward.
Adding a user to an additional group, without disturbing their existing group memberships:
sudo usermod -aG developers aliceThat -a (append) matters enormously here. -G alone replaces a user’s entire supplementary group list with whatever you specify — leaving off -a on a user who already belongs to several groups will silently remove them from all the others. -aG together means “add this group to the existing list,” which is what you want essentially every time you’re managing group membership after the fact.
Warning
Forgetting -a is one of the most common usermod mistakes, and it’s dangerous precisely because it fails silently — the command succeeds, alice is now in developers, and you won’t notice she’s been quietly removed from sudo and every other group until something she used to have access to stops working.
Changing a user’s shell:
sudo usermod -s /usr/sbin/nologin aliceUseful for converting a former human account into something closer to a service account — nobody can log in interactively as alice anymore, without deleting the account or its files.
Locking and unlocking an account, without deleting it:
sudo usermod -L alice
sudo usermod -U alice-L locks the account by disabling the password (the account still exists, but can’t authenticate), -U unlocks it again. This is generally the safer move for a user leaving temporarily, compared to deleting the account outright and potentially losing ownership context on their files.
Tip
passwd isn’t the only way to set a password — both usermod and useradd accept a -p flag that sets a password directly from an already-hashed value, useful for scripting account creation without an interactive prompt.
-p expects an already-hashed value, not a plaintext password — generate the hash with openssl passwd directly inline using command substitution:
sudo usermod -p "$(openssl passwd -6 'YourPasswordHere')" alice
sudo useradd -p "$(openssl passwd -6 'YourPasswordHere')" -m -s /bin/bash bobYou can also skip ‘YourPasswordHere’ so it doesn’t show in shell history. You will be prompted for password then.
-6 selects SHA-512 hashing — the modern default, matching what /etc/shadow itself uses per the User/Group Model file. Passing a plaintext password straight to -p instead of a hash sets that literal string as the hash, locking the account out with a broken, unusable password — always route it through openssl passwd first.
Removing A User: userdel
sudo userdel aliceThis removes the account entry from /etc/passwd and /etc/shadow, but by default leaves the home directory and its contents untouched. Files alice owned on the system don’t disappear — they just end up owned by a UID that no longer resolves to any username, showing up as a raw number in ls -l output instead of a name.
To remove the home directory and mail spool along with the account:
sudo userdel -r aliceWarning
-r deletes alice’s entire home directory, recursively, with the same lack of confirmation as rm -r. Before running userdel -r on any account, be certain nothing in that home directory still needs to exist — there’s no built-in way to recover it afterward.
Creating And Removing Groups: groupadd / groupdel
Groups follow the same pattern, with fewer moving parts since there’s no password or home directory involved.
sudo groupadd developersCreates a new, empty group. Removing one:
sudo groupdel developersThis fails if the group is still set as any user’s primary group — you’d need to reassign those users to a different primary group first (with usermod -g) before the group can be removed.
A Full Example, Start To Finish
Putting the whole thing together into one realistic sequence — onboarding a new team member:
sudo groupadd developers
sudo useradd -m -s /bin/bash -g developers -G sudo alice
sudo passwd alice
sudo usermod -aG developers,sudo aliceThat last line is redundant here since group membership was already set at creation — included only to show the safer, append-based pattern you’d actually reach for later, when adding an existing user to a new group without disturbing what they already have.
Clean it up once you’re done experimenting:
sudo userdel -r alice
sudo groupdel developersQuick Reference
| Command | Purpose |
|---|---|
useradd | Create a new user |
passwd | Set or change a password |
usermod | Modify an existing user (-aG to add a group, -s to change shell, -L/-U to lock/unlock) |
userdel | Remove a user (-r to also remove their home directory) |
groupadd | Create a new group |
groupdel | Remove a group |
What’s Next
You can now create, adjust, and remove both users and groups directly. There’s still one gap: every command in this chapter required sudo, and you’ve been typing it for a while now without a real explanation of what it’s actually doing, how it differs from su, or where its rules are configured. That’s the chapters in this section.