For the complete documentation index, see llms.txt. This page is also available as Markdown.

15.5 Managing Services in FreeBSD

Overview

FreeBSD uses the traditional BSD init (initialization system) to manage system services. Unlike modern initialization systems such as systemd, BSD init employs a script-based service management approach. All other processes are started directly or indirectly by init. The rc startup script system is used for system initialization and service management.

A simple script example is as follows:

#!/bin/sh
#
# PROVIDE: utility
# REQUIRE: DAEMON
# KEYWORD: shutdown

. /etc/rc.subr

name=utility
rcvar=utility_enable

command="/usr/local/sbin/utility"

load_rc_config $name

#
# Do not modify default values here; set them in /etc/rc.conf
#
utility_enable=${utility_enable-"NO"}
pidfile=${utility_pidfile-"/var/run/utility.pid"}

run_rc_command "$1"

FreeBSD provides two core service management commands: the service command is used to control startup scripts in the rc.d system, supporting operations such as start, stop, restart, status, and can list available services.

The sysrc command is used to safely modify system configuration values in rc.conf(5). It reads the rc_conf_files variable from /etc/defaults/rc.conf to determine the list of files to operate on, with a default value of /etc/rc.conf /etc/rc.conf.local, avoiding syntax errors from manual editing. When modifying a variable, if the variable already exists in these files, the last occurrence is replaced; if it does not exist, it is appended to the first file in the rc_conf_files list.

Directory Structure

The above files are primarily located in the libexec/rc path in the FreeBSD source code.

Note

You should not edit the default settings contained in /etc/defaults/rc.conf. All system-specific changes should be written to /etc/rc.conf. The /etc/rc.conf file takes precedence over the /etc/defaults/rc.conf file. In other words, /etc/rc.conf overrides configuration items with the same name in /etc/defaults/rc.conf.

Tip

Both /etc/rc.conf and /etc/rc.conf.local are parsed by sh(1). This enables system operators to create complex configuration scenarios.

Managing System-Specific Configuration

The primary location for system configuration is /etc/rc.conf. This file contains extensive configuration information that is read at system startup to configure the system. It provides configuration information for rc* files.

Entries in /etc/rc.conf override the default settings in /etc/defaults/rc.conf. It is recommended to place system-specific configuration in the /etc/rc.conf.local file.

Tip

System updates will not overwrite /etc/rc.conf, so system configuration information will not be lost.

In cluster applications, various strategies can be employed to separate site-wide configuration from system-specific configuration to reduce management overhead.

For example, these entries in the /etc/rc.conf file apply to all systems:

While these entries in /etc/rc.conf.local apply only to this system:

Use applications such as rsync or puppet to distribute /etc/rc.conf to each system, while /etc/rc.conf.local remains unique.

Common Command Set

The BSD init system provides the service command as a unified interface for service management, combined with the sysrc command to implement service startup, stopping, and boot-time auto-start configuration. The following is the common command set.

Start a service:

Stop a service:

Temporarily start a service (even if not enabled in the rc.conf file):

Temporarily stop a service (even if not enabled in the rc.conf file):

Restart a service:

Add a service and set it to start at boot:

Note

The service xxx enable command is not applicable to all services and has limitations; see: FreeBSD Project. rc keywords: enable, disable, delete cannot manage certain built-in rc startup items.[EB/OL]. [2026-03-26]. https://bugs.freebsd.org/bugzilla/show_bug.cgi?id=285543. Same below.

Disable boot-time startup:

Delete a boot entry:

Note

The keywords enable, disable, and delete see: FreeBSD Project. Add new rc keywords: enable, disable, delete[EB/OL]. [2026-03-26]. https://reviews.freebsd.org/D17113.

System services are not enabled by default after installation; the above commands cannot be executed directly without first enabling the service.

Edit the /etc/rc.conf file and add a line: XXX_enable="YES", where XXX represents the service name (e.g., nginx, samba, etc.); this is a fixed format.

Tip

The double quotes around "YES" in the above command can be omitted; the system will add them automatically. The same applies to "NO".

The script path for base system services is /etc/rc.d/, and for third-party application services is /usr/local/etc/rc.d/. You can directly invoke scripts in these two directories.

Reload service configuration:

Stop a service:

Content and Structure of the Default rc.conf Configuration File

The source code for the default rc.conf configuration file is located at libexec/rc/rc.conf, corresponding to the commit Set virtual_oss_enable="NO" in /etc/defaults/rc.conf.

Shutdown and Reboot

reboot and halt are the same program in FreeBSD, exhibiting different behaviors depending on the name by which they are invoked. In Linux, these commands may be symlinks to systemd with different behaviors. FreeBSD's design is closer to traditional UNIX behavior.

Shutdown:

  • Using shutdown now does not power off; instead, it switches to "single-user mode", displaying the prompt: Enter full pathname of shell or RETURN for /bin/sh: — press Enter to enter single-user mode;

  • Using shutdown -h now does not completely power off; it only stops the system, displaying: The operating system has halted. Please press any key to reboot. Press any key here to reboot the system;

  • For shutdown and power off, shutdown -p now is recommended; this command sends a signal to init(8) to run the /etc/rc.shutdown script (source located at libexec/rc/rc.shutdown) for orderly service shutdown before powering off. You can also use halt -p, which directly flushes the file system cache and sends SIGTERM and SIGKILL signals to all processes before powering off, without executing /etc/rc.shutdown.

Reboot:

  • The reboot command is the same as on Linux, both using reboot, but the parameters are not interchangeable.

  • On FreeBSD, reboot directly flushes the file system cache, sends SIGTERM and SIGKILL signals to all processes, and then reboots the system without executing /etc/rc.shutdown. For orderly service shutdown followed by reboot, use shutdown -r now, which runs /etc/rc.shutdown through init(8). For a quick reboot (skipping signal sending to processes, which may cause data loss), use reboot -q, which only flushes the file system cache before immediately rebooting. fastboot is an alias for reboot with the same behavior.

Note

On FreeBSD, the shutdown command can be executed by root users and members of the operator group; reboot and halt commands are restricted to root users only.

Exercises

  1. Create a custom service script in the /usr/local/etc/rc.d/ directory, analyze the dependency checking implementation logic in rc.subr, and evaluate the differences compared to the systemd unit dependency model.

  2. Modify rc_debug to YES, trace the complete process of a service startup, compare the output differences between normal and debug startup, and analyze the execution order of each stage in the debug output.

  3. Disable rc_startmsgs and modify the startup order of key services (using REQUIRE and BEFORE keywords), and document the changes in system boot behavior.

Last updated