Linux Labs

Packages, services, and logs / Stage 5 of 13

Systemd services: state and startup

Not running Starting usually ~20 s
Leave the lesson

Step 4 of 6 · on a machine · 5 min

Turn autostart on

The second state is changed by enable and disable. Here you can see where systemd keeps the list of services to start at boot.

bash
sudo systemctl enable cron          # turn autostart on
sudo systemctl disable cron         # turn autostart off
sudo systemctl enable --now cron    # turn autostart on and start
sudo systemctl disable --now cron   # turn autostart off and stop

At boot systemd brings the system to the target multi-user.target, a server's normal working state, and starts everything attached to it. The attached services are symbolic links in the multi-user.target.wants directory:

bash
ls /etc/systemd/system/multi-user.target.wants/

enable creates a link to the unit file there, and disable removes it; that is what they report: Created symlink … or Removed …. The Synchronizing state … SysV lines before that are housekeeping: Ubuntu also updates the old start scripts. The --now option adds a start to enable and a stop to disable; without it, both commands change autostart only.

For some units is-enabled prints static: their file does not say which target to attach to, and another unit or systemd itself starts them. systemd-journald is one of them; such units need no enabling.

Check yourself 1 question

cron is stopped and its autostart is off. You run sudo systemctl enable cron. What will systemctl is-active cron print?

  • enabled comes from is-enabled. is-active reports on running now, and cron is still stopped.

  • enable without --now only creates the boot link. cron starts running after start or the next boot.

  • failed is the mark of a failure. Nobody started cron; it is simply stopped.

enable created the link in multi-user.target.wants but did not start cron. cron starts running after sudo systemctl start cron or at the next boot.

Key points

  • enable puts a link to the unit into multi-user.target.wants, and disable takes it away; systemd starts services at boot by these links.
  • --now adds a start to enable and a stop to disable.
Ubuntu 24.04
Loading…