109

I installed MariaDB-server to my machine. While setting up I met with a problem whether I have to enable it all the time as the document I follow is given with these steps,

sudo yum install mariadb mariadb-server 
sudo systemctl start mariadb.service  
sudo systemctl enable mariadb.service
  • 4
    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 '17 at 16:10
175

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.

Source: systemctl man page

| improve this answer | |
  • 6
    ++++ for clear explaination – sunleo Jun 30 '17 at 9:53
  • So when should we type systemctl enable? Everytime we want to run the systemd service? – Goldname Jul 18 '17 at 21:48
  • 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 '17 at 6:13
  • That means, is the service start as root ? How can I start service as another user on boot ? – Chaminda Bandara Sep 19 '19 at 9:09
24

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.

| improve this answer | |
  • when should we enable? Do we only need to do it once? – Goldname Jul 18 '17 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 '17 at 5:16
  • What about when we edit it? Will we need to re-enable? – Goldname Jul 19 '17 at 9:27
  • @Goldname you don't need to reenable it. – muru Jul 19 '17 at 9:27
  • Thanks, and the same goes if the file is moved to a different directory and/or the system is rebooted? – Goldname Jul 19 '17 at 10:45
8

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.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.