systemd Basics

This article is mainly the notes from “systemd” Tutorials

Why “systemd”?

What is systemd?

  • A new kind of init system.
  • Replaces SysV Init system
  • Created at Redhat
  • Chief Architect: Lennart Poettering

Why “systemd”?

  • Faster boot-up times, since daemons start in parallel.
  • Easier for developers, because it’s no longer necessary to figure out the order in which the daemons should start
    • No more numbered “S” and “K” links!
  • Simper config files
  • Auto restart from daemon crash.
  • Better Security
    • “auditd” cannot be stoped or restarted by any user, including the root user.
  • Every process now run in its own “cgroup” by default
  • Resources usage for each process
  • Better process separation
  • More consisten administration(commands across all Linux distros are the same)

Shundown and reboot commands

  • “systemctl”
  • On Redhat-type systems, “systemd” replaces:
    • shutdown
    • service
    • chkconfig
shutdown
reboot
sudo systemctl poweroff
sudo systemctl reboot

man systemctl

Daemon Management

systemctl enable name.service
systemctl disable name.service
systemctl is-enabled name.service
# 
systemctl status name.service
# more info
systemctl status -l name.service

systemctl start name.service
systemctl stop name.service

# lists all services
systemctl list-unit-files --type service
systemctl list-unit-files

Tragets vs. Runlevels

sudo systemctl list-units --type target --all

# set to text mode
sudo systemctl set-default multi-user

# set to graphical mode
sudo systemctl set-default graphical
sudo systemctl get-default

# change to graphical mode 
sudo systemctl isolate graphical
# change to text mode
sudo systemctl isolate  multi-user

Automatically restart crashed services with systemd

cd /etc/systemd/system/multi-user.target.wants/ vim httpd.service

[Service]
Restart=always

Edit systemd Service Files Correctly

systemctl edit nginx

Controlling the auditd Service

sudo service auditd start

Setting the hostname with systemd

hostname
hostnamectl set-hostname bighost

Set the timezone with timedatectl

timedatectl

systemd Service Files


[Unit]
Description=ABC service
Documentation=https://example.com/abc.html

[Service]
Environment=VAR1=abc VAR2=abc VAR3=abc
ExecStart=/usr/local/bin/abc
Restart=on-failure
RestartSec=5

[Install]
# mutil-user or graphical
WantedBy=multi-user.target

another file

[Unit]
Description=ABC service
After=network.target

[Service]
User=ubuntu
Group=ubuntu
Type=simple
Restart=always
RestartSec=5s
WorkingDirectory=/data/abc
ExecStart=/data/abc/abc

[Install]
WantedBy=multi-user.target

manual

man systemd