174

I installed MariaDB server to my machine. While setting up, I was met with the problem of whether I should have it enabled all the time, as the documentation I follow suggests with these steps:

sudo yum install mariadb mariadb-server 
sudo systemctl start mariadb.service  
sudo systemctl enable mariadb.service
1
  • 8
    You can also enable and start with the --now swich of enable subcommand, eg: systemctl enable sshd --now. The same is true for disable command to also stop the service.
    – user359096
    May 8, 2017 at 16:10

3 Answers 3

255

systemctl start and systemctl enable do different things.

enable will hook the specified unit into relevant places, so that it will automatically start on boot, or when relevant hardware is plugged in, or other situations depending on what's specified in the unit file.

start starts the unit right now.

disable and stop are the opposite of these, respectively.

This means that when you first install MariaDB, you might want to run systemctl enable mariadb.service to enable it so it starts on boot. You might also want to run systemctl start mariadb.service, or just reboot, in order to start MariaDB. To stop MariaDB, run systemctl stop mariadb.service (it will start again on next boot or when you manually start it). To disable it so it doesn't start on boot anymore, run systemctl disable mariadb.service.

Update:

As noted in gerardw's answer, starting from version 220, released in may 2015, both enable and disable started to take the optional --now switch in order to also start or stop the unit, depending on the used command.

To both disable and stop a unit with the same command, use systemctl disable mariadb.service --now. Similarly, to both enable and start a unit, use systemctl enable mariadb.service --now.

Source: systemctl man page

5
  • 13
    ++++ for clear explaination
    – sunleo
    Jun 30, 2017 at 9:53
  • So when should we type systemctl enable? Everytime we want to run the systemd service? Jul 18, 2017 at 21:48
  • 2
    No, when you want to make it start automatically. If you want to stop it from starting automatically then you run systemctl disable.
    – vurp0
    Jul 19, 2017 at 6:13
  • That means, is the service start as root ? How can I start service as another user on boot ? Sep 19, 2019 at 9:09
  • @ChamindaBandara Use the --useroption.
    – Samuel
    Feb 15, 2022 at 15:41
26

From the systemctl manpage:

enable NAME...
   Enable one or more unit files or unit file instances, as specified
   on the command line. This will create a number of symlinks as
   encoded in the "[Install]" sections of the unit files. After the
   symlinks have been created, the systemd configuration is reloaded
   (in a way that is equivalent to daemon-reload) to ensure the
   changes are taken into account immediately. Note that this does not
   have the effect of also starting any of the units being enabled. If
   this is desired, either --now should be used together with this
   command, or an additional start command must be invoked for the
   unit.
   ...
   Enabling units should not be confused with starting (activating)
   units, as done by the start command. Enabling and starting units is
   orthogonal: units may be enabled without being started and started
   without being enabled. Enabling simply hooks the unit into various
   suggested places (for example, so that the unit is automatically
   started on boot or when a particular kind of hardware is plugged
   in). Starting actually spawns the daemon process (in case of
   service units), or binds the socket (in case of socket units), and
   so on.

Essentially, enable marks the service for starting up on boot, and start actually starts the service immediately.

5
  • when should we enable? Do we only need to do it once? Jul 18, 2017 at 21:53
  • @Goldname You enable it the first time you need it started on the next boot. Only once is enough,
    – muru
    Jul 19, 2017 at 5:16
  • What about when we edit it? Will we need to re-enable? Jul 19, 2017 at 9:27
  • @Goldname you don't need to reenable it.
    – muru
    Jul 19, 2017 at 9:27
  • Thanks, and the same goes if the file is moved to a different directory and/or the system is rebooted? Jul 19, 2017 at 10:45
16

As of systemctl version 220, enable and disable support a --now switch to start / stop services concurrent with the enabling / disabling.

e.g. systemctl --now enable foobar.service

Use systemctl --version to check your installed version.

2
  • 1
    I just used this successfully in systemctl for systemd 219 (on CentOS 7)
    – mwfearnley
    Nov 13, 2019 at 17:32
  • The systemd github repo says 220, but perhaps CentOS backported the update. github.com/systemd/systemd/blob/…
    – gerardw
    Nov 14, 2019 at 20:38

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .