/etc/rc.conf is a shell script that the boot process sources. Which means a stray quote or a half-written line from a bad sed -i doesn’t give you a warning. It gives you a box that comes up without networking, and you find out at the console. I have driven to a colo for exactly this. sysrc(8) exists so you never have to repeat my mistake.
The basics
sysrc sshd_enable=YES
sysrc ntpd_enable
sysrc -x sendmail_enable
First form sets the variable. Second prints it (ntpd_enable: YES). Third deletes the line from the file. Add -n to print just the value, which is what you want in a script:
if [ "$(sysrc -n zfs_enable)" = "YES" ]; then ...
It edits in place and keeps the rest of the file alone. It also puts the variable in the right file: if the name already lives in /etc/rc.conf.local or a file under /etc/rc.conf.d/, it updates it where it found it rather than leaving you with two conflicting definitions.
Append and remove from list variables
Plenty of rc variables are space-separated lists: kld_list, cloned_interfaces, jail_list on older setups. Rewriting the whole string to add one module is how you drop the other three by accident. The += and -= forms handle it:
sysrc kld_list+="coretemp"
sysrc kld_list-="coretemp"
Check sysrc(8) on your release to confirm these operators are present before you rely on them in a provisioning script. They are on current releases, but I would rather you read it than trust my memory.
Show what you changed
sysrc -a
Dumps every variable set in the rc.conf files, not the defaults. That is the “what did someone do to this machine” listing, and it beats reading three files by hand. Add -A to include the defaults from /etc/defaults/rc.conf too. -d prints the description of a variable, and -e prints it as name=value, ready to paste.
Note the difference between a variable being set and a service actually running. sysrc only edits configuration. Starting is service sshd start, and service -e lists what is enabled. The conf file and the live state are separate facts, and confusing them is how you end up with a service that “is enabled” and not running.
Editing a jail without entering it
sysrc -j webjail nginx_enable=YES
sysrc -R /jails/webjail nginx_enable=YES
-j targets a running jail by name or JID. -R targets a root directory, which works for a jail that is stopped, or a freshly extracted base tree you are preparing before the first boot. This is the part that makes provisioning clean: build the tree, set its variables, start it. No chroot gymnastics, and no heredocs appended to a file. If you share a tree into a jail with nullfs, the rc.conf inside is still the jail’s own, so set it with -j or -R and not by editing the host’s file.
Other files
-f points it at a different file than the default. It will happily manage something like /boot/loader.conf style name="value" files, though for loader tunables and sysctls the loader.conf and kenv walkthrough covers the difference in when each one applies. -c checks whether a variable is set without changing anything, returning the exit status. And -i suppresses the error for a name that has no definition anywhere, which matters under set -e.
Why not sed
Because sed -i rewrites the file and a typo in your pattern rewrites something you did not mean. Because it will not know about rc.conf.d. Because quoting inside a value is yours to get right, every time, on a file where a bad quote is fatal. And because sysrc is in base, one tool, one man page, nothing to install. I resisted it for years on the grounds that I could edit a text file. I could. I also once left a trailing quote off at 2am. The tool wins.
Related: daemon(8) as a supervisor for the services you enable this way, and the FreeBSD and OpenBSD hub.
