FreeBSD

Configure Sudo on FreeBSD 15: wheel, sudoers.d and doas

A fresh FreeBSD box gives you root and nothing in between. There is no sudo in the base system, the wheel group only controls who may su, and the doas port ships a config option that quietly does nothing on FreeBSD. We rebuilt the whole setup on a small cloud VM called nas01 so every command and error below is a real capture rather than something remembered from Linux.

Original content from computingforgeeks.com - post 167218

This guide shows how to configure sudo on FreeBSD 15 the way an admin box should run. Wheel members get full sudo, one account gets a single passwordless command through a sudoers.d drop-in, and doas sits next to it for comparison. The second half covers what breaks, from a typo in visudo to a sudoers file that locks the whole wheel group out, and how to get back in with su or single-user mode. I ran through all of it on FreeBSD 15.1 in November 2026, with sudo 1.9.17p2 and doas 6.4 from the quarterly packages, and it works end to end.

sudo or doas on FreeBSD: which one should you run?

Run sudo on any shared or long-lived FreeBSD server, and keep doas for single-admin boxes where a four-line config is the point. The deciding difference showed up in the lab: a typo in doas.conf denies everyone, while a typo in a sudoers file only drops the broken line and the rest of the rules keep working. So the question is not which tool is safer on paper, but which one fails more gently when you get the config wrong. The table sums up what we measured on nas01.

Behaviour on FreeBSD 15.1sudodoas
Package install2 MiB download, 8 MiB on disk, pulls gettext-runtime on a clean box24 KiB download, no dependencies
Binary size228,312 bytes27,384 bytes
Config file/usr/local/etc/sudoers plus sudoers.d//usr/local/etc/doas.conf (not created, only a .sample)
Password cachingPer terminal, cleared with sudo -kNone: persist is ignored on FreeBSD
Syntax error in configBad line skipped, other rules still applyWhole file rejected, nobody can use doas
World-writable configFile ignoredFile refused
Config checkvisudo -cdoas -C file [command]
Session recordinglog_input, log_output, sudoreplayOne auth.log line per command

Nothing stops you from installing both, and we did exactly that here. Each tool reads only its own file, so a broken doas.conf never touches sudo and the other way round. That makes doas a handy second door while you are still learning sudoers syntax, because you can always fix one tool with the other instead of reaching for the console.

Prerequisites

You need a FreeBSD 15 machine you can reach as root, either on the console or over SSH. If you have not built one yet, our guide to installing FreeBSD 15 on Proxmox or KVM with ZFS gets you to the same starting point we used.

  • FreeBSD 15.x with a ZFS root and working pkg
  • The root password (you will need it for recovery later)
  • An admin user, kibet here
  • A second unprivileged user, deploy, to test denials

Steps 1 to 3, Steps 5 to 8 and the recovery commands in Step 10 run as root, because sudo does not exist yet when you start. Keep a root shell open in a second terminal for the whole session: it is the cheapest insurance against locking yourself out while you edit sudoers.

Step 1: Install sudo with pkg

sudo comes from the ports tree as a binary package, so one pkg install puts it under /usr/local. Confirm the release first, because the package set you pull from follows the release and the output below only matches a 15.x box. Run both commands as root.

freebsd-version -ku
pkg install -y sudo

On our box pkg pulled sudo alone, but only because the cloud image had shipped sudo and we removed it with pkg delete sudo before the test, which leaves its libraries behind. The port links against libintl, so on a clean install expect gettext-runtime and indexinfo in the same transaction (FreshPorts lists the dependency). The quarterly branch may also hand you a newer _N port revision than the one shown here.

15.1-RELEASE-p7
15.1-RELEASE-p7
...
New packages to be INSTALLED:
	sudo: 1.9.17p2_2 [FreeBSD-ports]

Number of packages to be installed: 1

The process will require 8 MiB more space.
2 MiB to be downloaded.
[1/1] Fetching sudo-1.9.17p2_2: 100%  1792 KiB   1.8 MB/s    00:01
Checking integrity... done (0 conflicting)
[1/1] Installing sudo-1.9.17p2_2...
[1/1] Extracting sudo-1.9.17p2_2: 100%

Now check where the binaries landed and what the package left in its config directory. Every path in this guide lives under /usr/local/etc, not /etc, which is the first thing Linux habits get wrong.

which sudo visudo
ls -l /usr/local/etc/sudoers /usr/local/etc/sudoers.d

The main file is read-only for root and the wheel group, and the drop-in directory starts empty.

/usr/local/bin/sudo
/usr/local/sbin/visudo
-r--r-----  1 root wheel 5565 Nov 19 01:34 /usr/local/etc/sudoers

/usr/local/etc/sudoers.d:
total 0

Package management on FreeBSD has more to it than pkg install, including quarterly versus latest branches and how services get enabled in rc.conf. Our FreeBSD and OpenBSD package and service management guide covers that side in depth.

Step 2: Add your user to the wheel group

Installing sudo grants nobody anything. What does a normal user see at this point? Before touching the group, we tried sudo whoami as kibet, and the denial only came back after the lecture and the password prompt, so you cannot tell from the prompt alone whether you have rights.

sudo whoami on FreeBSD 15 printing kibet is not in the sudoers file before the user joins wheel

The refusal is also logged to /var/log/auth.log as a user NOT in sudoers line, which is what “reported to the administrator” means here. As root, add kibet to wheel with pw and confirm the membership from both sides:

pw groupmod wheel -m kibet
pw groupshow wheel
id kibet

wheel is GID 0 on FreeBSD, and kibet now carries it as a supplementary group.

wheel:*:0:root,kibet
uid=1005(kibet) gid=1006(kibet) groups=1006(kibet),0(wheel)

Group membership has a second effect that Linux users do not expect. On FreeBSD, only wheel members may su to root, so our deploy user, who is not in wheel, gets refused even with the right root password. Keep that in mind, because it decides who can recover the box later.

su -

The reply is a single word, with no hint about why.

su: Sorry

Joining wheel is still not enough for sudo. Right after the pw groupmod, kibet ran sudo whoami again and got the same “kibet is not in the sudoers file.” message, because the wheel rule in the shipped sudoers is commented out. So if you stop here, you have a user who can su but not sudo, which is the most common half-finished setup we see. Step 3 fixes that.

Step 3: Enable the %wheel rule with visudo

The FreeBSD sudoers file ships with two wheel rules, both disabled. One asks for a password and the other carries NOPASSWD. Find them by line number before you change anything, so you edit the right one.

grep -n 'wheel' /usr/local/etc/sudoers
grep -n 'includedir' /usr/local/etc/sudoers

Line 133 is the one we want. Line 150 is the include that makes Step 5 work without editing the main file again.

132:## Uncomment to allow members of group wheel to execute any command
133:# %wheel ALL=(ALL:ALL) ALL
136:# %wheel ALL=(ALL:ALL) NOPASSWD: ALL
150:@includedir /usr/local/etc/sudoers.d

You can open the file with visudo and delete the leading # on line 133 by hand. We used a sed one-liner so the change is exact and repeatable, then ran visudo -c to check the result. Note the empty '' after -i: FreeBSD sed needs it, and the GNU habit of sed -i -e makes FreeBSD sed save a backup called sudoers-e next to the real file.

sed -i '' 's/^# %wheel ALL=(ALL:ALL) ALL/%wheel ALL=(ALL:ALL) ALL/' /usr/local/etc/sudoers
visudo -c
grep -n '^%wheel' /usr/local/etc/sudoers

Only the password rule became active, and the parser is happy with it.

pw groupmod adding kibet to wheel and visudo -c confirming the uncommented %wheel rule on FreeBSD 15

Leave line 136 alone. A NOPASSWD rule for the whole wheel group means anyone who steals one admin session owns the box without knowing a password, and Step 5 shows a narrower way to drop the prompt for the one command that needs it.

Step 4: Verify sudo access with sudo -l

sudo -l is the fastest way to see what sudo thinks a user may run, and it asks for the password once. Log in as kibet in a new session, because group changes only apply to new logins, then list the rules and run a test command.

sudo -l
sudo whoami
sudo -k
sudo whoami

The second command ran without a prompt because the first one cached the credentials. After sudo -k threw the cache away, sudo asked again. Use sudo -k whenever you test a rule change, otherwise a cached login hides whether the rule really works.

sudo -l output for a wheel member on FreeBSD 15 showing (ALL : ALL) ALL and sudo whoami returning root

Read the secure_path line closely. Commands run through sudo get a fixed PATH that includes /usr/local/sbin, so tools installed from packages work under sudo even when your own PATH is different. If a script works as root but fails under sudo, compare it against this list first.

Step 5: Add a NOPASSWD rule in /usr/local/etc/sudoers.d

A drop-in file is the right home for anything beyond the wheel rule, because package upgrades never touch sudoers.d. Our example lets kibet run one ZFS snapshot script as root with no password, which is what you want for a cron job or a backup hook. First, create the script itself as root. It stays this short for the demo, but its exit status comes from tail, so a failed snapshot still exits 0. Add set -e before you hang real backups on it.

printf '#!/bin/sh\nzfs snapshot -r zroot@backup-$(date +%%Y%%m%%d-%%H%%M)\nzfs list -H -t snapshot -o name | tail -1\n' | tee /usr/local/bin/zfs-backup.sh
chmod 750 /usr/local/bin/zfs-backup.sh

The tee echoes what landed in the file, so you can spot a quoting mistake right away. The doubled percent signs are for printf and come out single in the script.

#!/bin/sh
zfs snapshot -r zroot@backup-$(date +%Y%m%d-%H%M)
zfs list -H -t snapshot -o name | tail -1

Set the user name once so the rule and the file name match. Change kibet to your own admin account, then write the drop-in, lock its mode to 0440 and run the checker. Notice the file name has no dot in it; Step 6 explains why that matters.

export ADMIN_USER=kibet
printf '# %s runs the ZFS backup script as root, no password\n%s ALL=(root) NOPASSWD: /usr/local/bin/zfs-backup.sh\n' "${ADMIN_USER}" "${ADMIN_USER}" | tee /usr/local/etc/sudoers.d/10-${ADMIN_USER}-backup
chmod 440 /usr/local/etc/sudoers.d/10-${ADMIN_USER}-backup
visudo -c

visudo -c now lists every file it parsed, which is your proof that the include picked the drop-in up.

# kibet runs the ZFS backup script as root, no password
kibet ALL=(root) NOPASSWD: /usr/local/bin/zfs-backup.sh
/usr/local/etc/sudoers: parsed OK
/usr/local/etc/sudoers.d/10-kibet-backup: parsed OK

Back as kibet, clear the cached credentials and test both sides of the rule with -n, which makes sudo fail instead of prompting. The script should run and any other command should still demand a password. If both pass or both fail, the rule is wider or narrower than you meant.

sudo -k
sudo -n /usr/local/bin/zfs-backup.sh
sudo -n whoami
sudo -l

That is exactly what happened. The snapshot was created, whoami was refused, and sudo -l shows the new rule under the wheel one.

A NOPASSWD rule in /usr/local/etc/sudoers.d running the ZFS backup script while other commands still need a password

One snag from the lab: two runs inside the same minute produce the same snapshot name and the second zfs snapshot fails. We added seconds to the date format, which is why later snapshot names in this guide have six digits after the dash.

sed -i '' 's/%H%M)/%H%M%S)/' /usr/local/bin/zfs-backup.sh
cat /usr/local/bin/zfs-backup.sh

The script now reads as follows, and the sudoers rule needs no change because it names the path, not the contents.

#!/bin/sh
zfs snapshot -r zroot@backup-$(date +%Y%m%d-%H%M%S)
zfs list -H -t snapshot -o name | tail -1

Snapshots pile up fast once a rule like this runs from cron. Our walkthrough of ZFS snapshots, send/recv and disaster recovery on FreeBSD 15 covers retention and shipping them to a second pool.

Step 6: Name and permission your sudoers.d files correctly

Two things decide whether sudo reads a drop-in at all, the file name and its mode. The sudoers manual says files whose names end in ~ or contain a . are skipped, so a tidy name like 11-kibet.conf is silently ignored. We tested the name rule and then loosened the mode on the working drop-in to see what each check does.

cp /usr/local/etc/sudoers.d/10-kibet-backup /usr/local/etc/sudoers.d/11-kibet.conf
visudo -c
rm /usr/local/etc/sudoers.d/11-kibet.conf
chmod 644 /usr/local/etc/sudoers.d/10-kibet-backup
visudo -c
chmod 666 /usr/local/etc/sudoers.d/10-kibet-backup
visudo -c

The .conf copy never shows up in the list, with no warning at all. Both loose modes get the same complaint from the checker.

visudo -c skipping a sudoers.d file with a dot in its name and reporting bad permissions, should be mode 0440

Is sudo itself as strict as visudo? Not quite. With the file at 0644, the same sudo -n /usr/local/bin/zfs-backup.sh from kibet still ran and exited 0, so a wrong mode can sit unnoticed until someone runs the checker. At 0666 sudo drops the file and the passwordless rule is gone.

sudo -n /usr/local/bin/zfs-backup.sh; echo exit=$?

sudo names the file it refused and falls back to asking for a password.

sudo: /usr/local/etc/sudoers.d/10-kibet-backup is world writable
sudo: a password is required
exit=1

Put every drop-in back to 0440 before moving on. These are the rules we now follow for every file in sudoers.d, because each one came out of a test above:

  • Mode 0440, owner root, group wheel
  • No dot and no trailing ~ in the name
  • A numeric prefix such as 10- or 20-
  • visudo -c after every change

The numeric prefix matters because files are read in lexical order, so you get predictable results when two files set the same Defaults. It also keeps ls output in the order sudo applies the rules, which saves time when you debug a rule that seems to be ignored.

Step 7: Log every sudo command and replay sessions

The sudo package on FreeBSD is built to log through the authpriv syslog facility, and the default /etc/syslog.conf already sends that to /var/log/auth.log. Full keystroke and output recording is off by default, so a second drop-in turns it on for everyone. You end up with two trails that answer different questions:

  • /var/log/auth.log: who ran which command, and when
  • /var/log/sudo-io: what the session typed and printed

Write the I/O logging drop-in as root, give it the same 0440 mode, and check that syslog really routes authpriv where we expect.

printf 'Defaults log_input, log_output\nDefaults iolog_dir=/var/log/sudo-io\n' | tee /usr/local/etc/sudoers.d/20-iolog
chmod 440 /usr/local/etc/sudoers.d/20-iolog
visudo -c
grep -E '^auth' /etc/syslog.conf

The new file parses and the syslog line confirms where the summary lines go. Our run also listed a stray empty file called 40-test, which the visudo section further down explains.

Defaults log_input, log_output
Defaults iolog_dir=/var/log/sudo-io
/usr/local/etc/sudoers: parsed OK
/usr/local/etc/sudoers.d/10-kibet-backup: parsed OK
/usr/local/etc/sudoers.d/20-iolog: parsed OK
/usr/local/etc/sudoers.d/40-test: bad permissions, should be mode 0440
auth.info;authpriv.info				/var/log/auth.log

After kibet ran a few commands, both trails were there. auth.log has one line per command, and sudoreplay -l lists the recorded sessions with the TSID you pass to sudoreplay to watch one back.

grep 'sudo\[' /var/log/auth.log | tail -2
sudoreplay -l | tail -3

Each entry carries the user, working directory, target user and full command line.

Nov 30 10:40:17 nas01 sudo[2660]:    kibet : TTY=pts/1 ; PWD=/home/kibet ; USER=root ; TSID=000003 ; COMMAND=/usr/local/bin/zfs-backup.sh
Nov 30 10:40:20 nas01 sudo[2679]:    kibet : TTY=pts/1 ; PWD=/home/kibet ; USER=root ; TSID=000004 ; COMMAND=/usr/bin/whoami
Nov 30 10:32:59 2026 : kibet : HOST=nas01 ; TTY=pts/1 ; CWD=/home/kibet ; USER=root ; TSID=000002 ; COMMAND=/sbin/zfs list -o name,used zroot
Nov 30 10:40:17 2026 : kibet : HOST=nas01 ; TTY=pts/1 ; CWD=/home/kibet ; USER=root ; TSID=000003 ; COMMAND=/usr/local/bin/zfs-backup.sh
Nov 30 10:40:20 2026 : kibet : HOST=nas01 ; TTY=pts/1 ; CWD=/home/kibet ; USER=root ; TSID=000004 ; COMMAND=/usr/bin/whoami

I/O logs grow with every interactive session, because they store the full output and not just the command line. On a busy box put /var/log/sudo-io on its own ZFS dataset with a quota. The auth.log lines pair well with SSH login records too, and our SSH key authentication and hardening guide for FreeBSD 15 shows how to watch those.

Step 8: Install doas and write doas.conf

doas is the minimal sudo replacement from OpenBSD, ported to FreeBSD as a 24 KiB package. Install it the same way as root, then look at what the package left in /usr/local/etc.

pkg install -y doas
ls -l /usr/local/etc/doas.conf*

The package message is worth reading because it says the config does not exist yet, and the listing confirms only the sample is there. Until you create doas.conf, doas lets nobody in.

New packages to be INSTALLED:
	doas: 6.4_1 [FreeBSD-ports]
...
Message from doas-6.4_1:

--
To use doas,

/usr/local/etc/doas.conf

must be created. Refer to doas.conf(5) for further details and/or follow
/usr/local/etc/doas.conf.sample as an example.
...
-rw-r--r--  1 root wheel 750 Nov 19 12:07 /usr/local/etc/doas.conf.sample

We mirrored the sudo setup: wheel may run anything with a password, and kibet runs the backup script with none. The persist keyword is in there on purpose, because Step 9 shows what it does on FreeBSD. Write the file, make it readable by root only, and check the syntax with doas -C.

export ADMIN_USER=kibet
printf '# wheel members may run anything, password cached by persist\npermit persist :wheel\n\n# %s runs the ZFS backup script as root with no password\npermit nopass %s as root cmd /usr/local/bin/zfs-backup.sh\n' "${ADMIN_USER}" "${ADMIN_USER}" | tee /usr/local/etc/doas.conf
chmod 600 /usr/local/etc/doas.conf
doas -C /usr/local/etc/doas.conf; echo exit=$?

A clean check prints nothing except the exit code we asked for.

# wheel members may run anything, password cached by persist
permit persist :wheel

# kibet runs the ZFS backup script as root with no password
permit nopass kibet as root cmd /usr/local/bin/zfs-backup.sh
exit=0

Order matters here. The doas.conf manual says the last matching rule determines the action, so put the general wheel rule first and the narrow nopass rule after it. Reversed, the wheel rule would also match the backup script for kibet and bring the password prompt back, which breaks any cron job that relies on it.

Step 9: Test doas persist and nopass

Now the surprise. Most doas guides promise that persist caches your password for a few minutes, so we ran doas whoami twice in a row as kibet, then the nopass script, then a command with no rule.

doas whoami
doas whoami
doas -n /usr/local/bin/zfs-backup.sh
doas -n id; echo exit=$?

doas asked for the password both times. The nopass rule worked, and -n on an unlisted command failed cleanly instead of hanging on a prompt.

doas on FreeBSD 15 asking for a password every time despite persist, with the nopass rule running the backup script

So is persist broken? No, it is unsupported, and the manual that ships with the FreeBSD port says so. We read it straight from the box rather than trusting an OpenBSD or Linux man page.

MANPAGER=cat man doas.conf | col -b | grep -B1 -A6 'persist'

The relevant paragraph is short and leaves no room for doubt.

		  persist  After the user successfully authenticates, do not
			   ask for a password again for some time. Works on
			   OpenBSD only, persist is not available on Linux or
			   FreeBSD.

doas accepts the keyword without complaint and simply ignores it. If you want password caching on FreeBSD, that alone is a reason to stay with sudo. doas -C with a command name is the other test worth knowing, because it tells you which rule would match without running anything.

doas -C /usr/local/etc/doas.conf /usr/local/bin/zfs-backup.sh
doas -C /usr/local/etc/doas.conf /bin/sh

The script matches the nopass rule and a shell matches the wheel rule.

permit nopass
permit

Run those checks as root. With doas.conf at 0600 a normal user cannot read the file, and that is the mode we recommend. doas logs to auth.log as well, one line per command, including failures.

grep doas /var/log/auth.log | tail -4

The last line is the deploy user trying the same trick.

Nov 30 10:39:50 nas01 doas[2541]: kibet ran command whoami as root from /home/kibet
Nov 30 10:39:50 nas01 doas[2542]: kibet ran command whoami as root from /home/kibet
Nov 30 10:39:50 nas01 doas[2543]: kibet ran command /usr/local/bin/zfs-backup.sh as root from /home/kibet
Nov 30 10:39:53 nas01 doas[2558]: failed command for deploy: whoami

Those lines are thinner than the sudo entries, with no TTY, no session ID and no recording. On a box that runs jails for other people, like the setup in our Bastille jails guide for FreeBSD 15, the richer sudo trail is worth the larger package because you will want to know who did what.

Step 10: Recover from a broken sudoers file

A sudoers mistake that slips past visudo can take sudo away from every admin at once. To see how bad it gets, we broke the wheel rule on purpose with a trailing comma, the kind of slip that happens when you edit with sed or a config management tool instead of visudo. Keep a copy of the good file first. There are three ways back in, from cheapest to slowest:

  1. A root shell you left open
  2. su - from a wheel member
  3. Single-user mode on the console

We tested the last two. This is the breakage, run as root with a backup copy saved first.

cp -p /usr/local/etc/sudoers /root/sudoers.good
sed -i '' 's/^%wheel ALL=(ALL:ALL) ALL$/%wheel ALL=(ALL:ALL) ALL,/' /usr/local/etc/sudoers
grep -n '^%wheel' /usr/local/etc/sudoers

The line now ends in a comma that sudo cannot parse.

133:%wheel ALL=(ALL:ALL) ALL,

The next sudo whoami from kibet printed the parse error, took the password, and then refused. sudo dropped the broken line and carried on, so the wheel rule simply no longer existed.

sudoers syntax error on line 133 locking a wheel member out of sudo on FreeBSD 15

Notice the order of events in that capture. sudo complains about line 133 before it even asks for the password, so the error is visible to anyone who tries, but nothing in it tells a wheel member how to get root back.

Fix it with su when you know the root password

This is why Step 2 matters. kibet is still in wheel, so su - with the root password works even though sudo does not. From the root shell, check, fix and check again.

su -
visudo -c; echo exit=$?
sed -i '' 's/^%wheel ALL=(ALL:ALL) ALL,$/%wheel ALL=(ALL:ALL) ALL/' /usr/local/etc/sudoers
visudo -c; echo exit=$?

The first check points at line 133, column 26, just past the trailing comma. After the fix the main file parses again. The non-zero exit on our second run came from the leftover 40-test file explained in the next section, not from sudoers.

/usr/local/etc/sudoers:133:26: syntax error
%wheel ALL=(ALL:ALL) ALL,
                         ^
exit=1
/usr/local/etc/sudoers: parsed OK
/usr/local/etc/sudoers.d/10-kibet-backup: parsed OK
/usr/local/etc/sudoers.d/40-test: bad permissions, should be mode 0440
exit=1

After exit back to kibet, sudo whoami asked for the password and returned root. Restoring /root/sudoers.good with cp -p works just as well when you are not sure what changed.

Fix it in single-user mode when su is not an option

Single-user mode is the way back in when nobody remembers the root password or nobody in wheel can log in. The boot process chapter of the FreeBSD Handbook describes two ways to get there. You can type boot -s at the loader prompt on the console, or set boot_single in the loader. If you are truly locked out, the console is the only road: pick single user from the loader menu, or type boot -s, through a monitor, IPMI or your cloud provider’s interactive serial console.

Our lab VM lived in the cloud and we still had a root SSH session, so we took the other route to reproduce the lockout: we added boot_single="YES" to /boot/loader.conf and rebooted with the wheel rule still broken. That needs root before things go wrong, so treat it as a rehearsal method rather than a rescue.

Boot stops at Enter full pathname of shell or RETURN for /bin/sh:. Press Enter and you get a root shell with no password, as long as the console is not marked insecure in /etc/ttys. On a ZFS root the boot environment comes up read-only, so the first edit attempt fails until you lift that and mount the rest of the pool.

zfs get -H -o name,property,value readonly zroot/ROOT/default
zfs set readonly=off zroot/ROOT/default
zfs mount -a
/usr/local/sbin/visudo -c
sed -i '' 's/^%wheel ALL=(ALL:ALL) ALL,$/%wheel ALL=(ALL:ALL) ALL/' /usr/local/etc/sudoers
/usr/local/sbin/visudo -c
sed -i '' '/^boot_single/d' /boot/loader.conf

We called visudo by its full path, /usr/local/sbin/visudo, so the fix does not depend on how the single-user shell sets up PATH. The screenshot shows the session from our serial console, including the failed write before the readonly flag came off.

FreeBSD 15 single-user mode on ZFS: setting readonly off, fixing the sudoers line and visudo -c parsing OK

Typing exit continued into a normal multi-user boot. One cleanup step remains after you log back in, because zfs set left a local readonly=off on the dataset. Run zfs inherit readonly zroot/ROOT/default to put it back to the default source, so it does not confuse the next person who reads zfs get.

What broke: sudo and doas errors from the lab

Every error below came out of nas01 while writing this guide. The exact message is the heading, so you can match what your terminal shows and jump straight to the fix. Most of them trace back to a file that skipped visudo -c or doas -C, so if you only take one habit away, make it running the checker after each edit.

“kibet is not in the sudoers file.”

You see this in two situations: the user is not in wheel, or the user is in wheel but the %wheel line in /usr/local/etc/sudoers is still commented. We hit both. Check id for 0(wheel), then grep -n '^%wheel' /usr/local/etc/sudoers for an active rule, and log in again after any group change.

“syntax error” and the “What now?” prompt in visudo

We typed kibet ALL=(root) NOPASSWORD: /usr/bin/uptime into visudo -f /usr/local/etc/sudoers.d/30-uptime. The real tag is NOPASSWD, and visudo refused to save. Our terminal printed the error block twice; one copy is shown.

/usr/local/etc/sudoers.d/30-uptime:1:45: syntax error
kibet ALL=(root) NOPASSWORD: /usr/bin/uptime
                                            ^
What now? ??
Options are:
  (e)dit sudoers file again
  e(x)it without saving changes to sudoers file
  (Q)uit and save changes to sudoers file (DANGER!)

Press e and fix the line. Never press Q, which writes the broken file anyway. The x option has a quirk too, at least on sudo 1.9.17p2. When visudo created a new file and you exit without saving, it leaves an empty file at mode 0640 behind. Every later visudo -c then fails on it with “bad permissions, should be mode 0440”. An aborted earlier test session is the likely source of the empty 40-test file in Steps 7 and 10. Delete such files with rm.

A broken drop-in, but sudo still works

When we wrote the same bad NOPASSWORD line straight into sudoers.d with printf, sudo printed the syntax error before every command and then carried on: the wheel rule and the backup rule both still worked. The sudoers manual explains this as the error_recovery option, on by default since 1.9.3, which discards the broken part of the line. Do not count on it. A typo in the wrong place, like the trailing comma in Step 10, removes the rule you rely on.

“Sorry, user kibet is not allowed to execute ‘/usr/bin/whoami’ as root on nas01.”

sudo knows the user and accepted the password, but no rule matches the command. After a sudoers edit, that usually means error recovery threw your rule away, so look at the syntax error printed above it. Use su - or single-user mode as shown in Step 10.

“doas: Operation not permitted”

This is what deploy got for doas whoami, with exit code 1. doas does not prompt for a password when no rule matches, so the message comes back instantly. Add a permit line for the user or group, or check with doas -C /usr/local/etc/doas.conf <command> as root to see which rule, if any, would match.

“doas: syntax error at line 2”

We misspelled nopass as nopas. Unlike sudo, doas has no error recovery. The perfectly valid wheel rule on line 1 stopped working too, and every doas call returned the same syntax error with exit code 1. Always run doas -C /usr/local/etc/doas.conf after an edit, and keep sudo or su available while doas is your only escalation path.

“doas: /usr/local/etc/doas.conf is writable by group or other”

doas refuses a config that anyone besides root could edit, which we saw at mode 0666. A mode of 0644 is accepted, but 0600 keeps your rules private. Fix it with chmod 600 /usr/local/etc/doas.conf, and check the file is still owned by root.

“sudo: /usr/local/etc/sudoers.d/10-kibet-backup is world writable”

sudo skips that file and every rule in it, so passwordless commands suddenly ask for a password. Set the file back to 0440 with chmod 440 and confirm with visudo -c. If your sudo rules also open ports or services, our PF firewall guide for FreeBSD 15 is the next place to lock things down.

Keep reading

Run Linux VMs on FreeBSD 15 with bhyve FreeBSD Run Linux VMs on FreeBSD 15 with bhyve Install Python 3 and pip on FreeBSD 15 FreeBSD Install Python 3 and pip on FreeBSD 15 How To Install and Configure OPNSense Firewall FreeBSD How To Install and Configure OPNSense Firewall Install Postfix + Dovecot Mail Server on FreeBSD 15 FreeBSD Install Postfix + Dovecot Mail Server on FreeBSD 15 ZFS Snapshots, Send/Recv, and Disaster Recovery on FreeBSD 15 FreeBSD ZFS Snapshots, Send/Recv, and Disaster Recovery on FreeBSD 15 FreeBSD and OpenBSD Package and Service Management Guide FreeBSD FreeBSD and OpenBSD Package and Service Management Guide

Leave a Comment

Press ESC to close