This page looks best with JavaScript enabled

Managing services in FreeBSD

 ·  🎃 kr0m

Managing services in FreeBSD is very simple thanks to the RC startup system and the service tool. In this article, we will learn the basic operation of the startup system and the most common service commands.

The first thing we need to know is that RC is the FreeBSD startup system, which runs scripts located in two directories:

/etc/rc.d
/usr/local/etc/rc.d

Services are enabled/disabled from the /etc/rc.conf file or the files contained in /etc/rc.conf.d, depending on how we manage our system, we will be more interested in using the file or the directory.

If we use systems like Puppet / Ansible that replace configuration files instead of editing them, we must use the directory. On the other hand, if the administration is manual, it is more convenient to use sysrc to enable/disable services, and to check the current configuration, we only need to consult a file.

Next, I will give an example of how to enable/disable the SSH service in both ways:
rc.conf:

sysrc sshd_enable=YES
sysrc sshd_enable=NO

rc.conf.d:

echo "sshd_enable=YES" > /etc/rc.conf.d/sshd
rm /etc/rc.conf.d/sshd


Now that we know how to enable/disable services, we can proceed with the basic service commands.

Check available services:

service -e

/etc/rc.d/hostid
/etc/rc.d/zpool
/etc/rc.d/zvol
/etc/rc.d/hostid_save
................
/usr/local/etc/rc.d/nginx
/usr/local/etc/rc.d/node_exporter
/etc/rc.d/sshd
/etc/rc.d/cron
/etc/rc.d/bgfsck

Get service information:

service sshd describe

Secure Shell Daemon

Get the commands allowed by the service:
The fastest way is to run a command that does not exist, such as XXXX.

service sshd XXXX

/etc/rc.d/sshd: unknown directive 'XXXX'.
Usage: /etc/rc.d/sshd [fast|force|one|quiet](start|stop|restart|rcvar|enable|disable|delete|enabled|describe|extracommands|configtest|keygen|reload|status|poll)

First, it shows us the options in brackets and then the commands in parentheses.

The options have the following meanings:

fast Do no checking (used during startup).
force Try harder.
one Start this service despite not being enabled in rc.conf.
quiet Only print service name (used during startup).

And they must be executed together with the desired command. For example, if we want to start SSH without checking the configuration, it would be done as follows:

service faststart sshd

The commands perform the following functions:

start Start the service.
stop Stop the service.
restart Stop and restart the service.
rcvar Print the rc.conf variables for this service.
enabled Return true in shell if enabled (for script use).
describe Print service description.
extracommands Show service-specific commands.

The rcvar command is very useful if we want to start the service with some special options from the CLI, although personally, I prefer to configure all the options in the service configuration file and leave the rc.conf file(s) as clean as possible.

Another interesting command is extracommands, which shows us specific commands for this particular service:

service sshd extracommands

configtest keygen reload

To know the purpose of the additional commands, we must consult the man page of the corresponding service.

If you liked the article, you can treat me to a RedBull here