Managing users and groups on NetBSD with useradd, userdel, and groupadd, understanding the /etc/passwd and /etc/group files, and controlling root access with su, doas, and sudo.

In episode 4 we filled the system with software via pkgsrc and pkg_add. But a good system isn't run as root all the time — it needs structured inhabitants. In this episode we'll set up users, groups, and privileges on NetBSD: creating accounts, dividing users into groups, and limiting administrative access with su, doas, and sudo.
On NetBSD, every process runs on behalf of a user (identity) and one or more groups (collectives). Users and groups are defined in two text files:
| File | Contents |
|---|---|
/etc/passwd | List of users: username, UID, GID, home, shell |
/etc/group | List of groups: name, GID, members |
You can read both directly:
cat /etc/passwd
cat /etc/grouproot:*:0:0:Charlie &:/root:/bin/sh
bin:*:1:1:Bin:/bin:/sbin/nologin
arman:*:1000:100:Arman Dwi:/home/arman:/bin/kshNotice the colon-separated fields: username, password (stored in /etc/master.passwd — /etc/passwd only contains *), UID, GID, GECOS (full name), home directory, and shell. This is the classic UNIX structure NetBSD inherited.
Warning
Never edit /etc/passwd manually to change passwords. Passwords are stored in the protected /etc/master.passwd file and must be changed via passwd or chpass. Editing the wrong file can make the system unable to log in.
The useradd command creates a user in one line and automatically updates /etc/passwd, /etc/master.passwd, and /etc/group:
useradd -m -s /bin/ksh -G wheel,oper -c "Arman Dwi" armanarmanExplanation of the commonly used options:
| Option | Function |
|---|---|
-m | Creates the home directory (/home/arman) |
-s | Sets the login shell (e.g. /bin/ksh) |
-G | Adds to additional groups (comma-separated) |
-c | Comment/GECOS: the user's full name |
After the user is created, set a password with passwd:
passwd armanThe interactive alternative is adduser, which guides you through creating a user step by step — convenient for beginners:
adduserTo remove a user, use userdel. The -r option also removes the home directory and mail spool:
userdel -r armanBefore deleting, make sure no processes are running under that user. Use ps -u arman to check.
Groups are created with groupadd, modified with groupmod, and removed with groupdel. Here's an example of creating a group for a developer team and adding members:
groupadd devteam
groupmod -A arman,budi devteamdevteam:*:100:arman,budiAnother way to add a user to groups is with usermod -G:
usermod -G wheel,devteam,oper armanOnce users and groups are in place, the question is: how do you grant administrative access without sharing the root password? Three common answers on NetBSD: su, doas, and sudo.
NetBSD follows the BSD tradition: only members of the wheel group may su to root. This is the first security layer — not everyone can become root just by knowing the password.
su switches to another user. From a wheel member, su to root by entering the root password:
su - rootPassword:
# whoami
rootdoas is a modern lightweight tool — your pick if you want minimalism. It's available as a pkgsrc package:
cd /usr/pkgsrc/security/doas
make install cleanThe doas configuration is written in /etc/doas.conf. Grant full access to wheel members:
permit keepenv :wheelAfter that, wheel members just type:
doas sysctl kern.ostypesudo gives finer control — it can restrict commands per user, per host, and log every execution. Install from pkgsrc:
cd /usr/pkgsrc/security/sudo
make install cleanConfigure it in /usr/pkg/etc/sudoers with visudo (never edit it directly):
visudo%wheel ALL=(ALL:ALL) ALLWith that line, all members of wheel can run any command as root with sudo. To restrict specific commands (e.g. only restarting services):
arman ALL=(ALL) /usr/sbin/service| Tool | Basis | Strengths | Weaknesses |
|---|---|---|---|
su | Built into the base system | Always available | Needs the root password |
doas | pkgsrc package | Simple, lightweight config | Limited features |
sudo | pkgsrc package | Command control, audit log | More complex config |
| Rule | Reason |
|---|---|
| Don't log in as root directly | A single mistake is enough to break the system |
Limit wheel members | Only those who genuinely need admin access |
Use doas/sudo and log activity | Audit and accountability |
| Use separate users for services | Principle of least privilege |
In this episode 5, you've managed NetBSD users and groups with useradd, userdel, groupadd, and usermod, understood the structure of /etc/passwd and /etc/group, and limited root access with su, doas, and sudo — with the wheel group as the gateway.
Key takeaways:
/etc/passwd, groups in /etc/group; change passwords via passwd, not by hand-editing files.useradd -m -s /bin/ksh -G wheel arman creates a complete account with a home directory.wheel group can su to root.doas for a lightweight solution (permit keepenv :wheel), sudo for granular command control.In the next episode, episode 6, we'll bring the service machinery to life: system services and the rc system — understanding /etc/rc.conf, the scripts in /etc/rc.d, the service command, and how rcorder determines the boot order. See you in episode 6!