How to Start a Service on Boot using systemd on CentOS

Posted on

Modern Linux systems feature a process known as “systemd” which took over from the old “init.d” way of doing things. This deals with starting, stopping, and enabling services, as well as taking care of dependencies, scheduling, logging etc. For new users however, it can get a bit confusing. In this tutorial, I’m going to take you through a simple service to illustrate the answers to a few questions:

  1. What’s the difference between “starting” and “enabling” a service?
  2. What causes a service to start automatically on boot?
  3. How are symbolic links used to achieve this?

Many tutorials on installing software make sure to include lines like this:

systemctl enable [servicename]

Sometimes it’s necessary, and sometimes it’s not. Let’s take an in-depth look at an example.

Using the “rng-tools” Package as an Example

The package “rng-tools” allows us to generate genuine random numbers based off the entropy of the hardware whenever available. After installation, it runs a daemon for random number generation. Let’s take a look at how it starts up. We begin by first installing the rng-tools package via yum (for CentOS) as shown here:

Now we take a look at two key locations:

  1. /usr/lib/systemd/system/
  2. /etc/systemd/system/

We see that both these locations have a new file called “rngd.service”. The second one located in a subfolder in “/etc/systemd/system” is a symbolic link to the first. For now, let’s just remember that the package “rng-tools” installed these two files in these two locations.

Starting the Service and Rebooting

To start with, the service is not running. So I start it, verify that it’s running and reboot the system.

To start the service, I use the command:

systemctl start rngd

Now after rebooting, I check the status of the service to see if it’s started automatically on startup:

The answer is “yes”. the service is running automatically on startup. But how? Why?

Deleting the Symbolic Link in /etc/systemd/system and Rebooting

We saw that the rng-tools package had installed a symbolic link in the folder “/etc/systemd/system” pointing to the one in “/usr/lib/systemd/system/”.  Let’s see what happens when we delete that link.

Removing a symbolic link is just like removing any other file. After removal, we see that the service is still running. But now what happens when we reboot the system?

This time however, we see that the service is “no longer” starting up automatically at boot! What does this mean? It means that the symbolic link was the critical piece that was causing the service to start up automatically at boot.

“Enabling” and “Disabling” a Service

In fact, this is exactly what “enabling” a service means. If it’s not already there, “enabling” a service will create a symbolic link to the service file in a number of places as specified in the “Install” section. For example, if we open up the “rngd.service” file in a text editor like vi, we see the following lines:

This means that when we run the command:

systemctl enable rngd.service

Linux will create a symbolic link to the file in the sub-folder “” within /etc/systemd/system/. We can verify that this indeed happens as shown here:

Enabling a service has nothing to do with starting it. A service can be started or stopped irrespective of whether or not it’s enabled. In this case, enabling rngd only creates a symbolic link to one location – for starting it on boot. Other services might make it so that it only starts up when a certain device is plugged in. The choice is up to the developer.

So to start a service on boot, it must be enabled. Either via the built in enable command, or manually creating the link.

Leave a Reply

Your email address will not be published. Required fields are marked *