systemd is the third generation of linux init system, following sysvinit, upstart.
The main advantage of systemd is that it brings up all processes in parelell, instead of sequential.
systemd is controversial for several reasons: It’s a replacement for something that a lot of Linux users don’t think needs to be replaced, and the antics of the systemd developers have not won hearts and minds. But rather the opposite, as evidenced in this famous LKML thread where Linus Torvalds banned systemd dev Kay Sievers from the Linux kernel. Despite that, systemd is being widely deployed in the distributions like ubuntu (15.04+), Fedora, Redhat Enterprise/Centos (7.0+).
systemd is not just the name of the init daemon but also refers to the entire software bundle around it, which, in addition to the systemd init daemon, includes the daemons journald, logind and networkd, and many other low-level components.
systemd records initialization instructions for each daemon in a configuration file (referred to as a “unit file”) that uses a declarative language, replacing the traditionally used per-daemon startup shell scripts. Unit file types include service, socket, device, mount, automount, swap, target, path, timer (which can be used as a cron-like job scheduler[14]), snapshot, slice and scope. below is a unit for for syslog.
eng@weng-u1604:/etc/systemd/system$ cat syslog.service
[ Unit]
Description = System Logging Service
Requires = syslog.socket
Documentation = man:rsyslogd( 8)
Documentation = http://www.rsyslog.com/doc/
[ Service]
Type = notify
ExecStart = /usr/sbin/rsyslogd -n
StandardOutput = null
Restart = on-failure
[ Install]
WantedBy = multi-user.target
Alias = syslog.service
weng@weng-u1604:/etc/systemd/system$
systemctl
The main command used to introspect and control systemd is systemctl. Some of its uses are examining the system state and managing the system and services.
“systemctl status” will show the the system status, all processes started by systemd in a tree fashion.
weng@weng-u1604:~$ systemctl status
● weng-u1604
State: running
Jobs: 0 queued
Failed: 0 units
Since: Fri 2016-11-04 14:17:38 PDT; 29min ago
CGroup: /
├─537 /sbin/cgmanager -m name = systemd
├─init.scope
│ └─1 /sbin/init splash
├─system.slice
│ ├─avahi-daemon.service
│ │ ├─567 avahi-daemon: running [ weng-u1604.local
│ │ └─602 avahi-daemon: chroot helpe
│ ├─dbus.service
│ │ └─541 /usr/bin/dbus-daemon --system --address = systemd: --nofork --nopidfile --systemd-activation
│ ├─ModemManager.service
│ │ └─563 /usr/sbin/ModemManager
│ ├─cron.service
│ │ └─573 /usr/sbin/cron -f
│ ├─wpa_supplicant.service
│ │ └─706 /sbin/wpa_supplicant -u -s -O /run/wpa_supplicant
│ ├─lightdm.service
│ │ ├─732 /usr/sbin/lightdm
│ │ └─745 /usr/lib/xorg/Xorg -core :0 -seat seat0 -auth /var/run/lightdm/root/:0 -nolisten tcp vt7 -novtswitch
│ ├─accounts-daemon.service
│ │ └─536 /usr/lib/accountsservice/accounts-daemon
│ ├─colord.service
│ │ └─1220 /usr/lib/colord/colord
│ ├─systemd-journald.service
│ │ └─222 /lib/systemd/systemd-journald
│ ├─udisks2.service
│ │ └─1379 /usr/lib/udisks2/udisksd --no-debug
│ ├─upower.service
│ │ └─1148 /usr/lib/upower/upowerd
│ ├─systemd-timesyncd.service
│ │ └─303 /lib/systemd/systemd-timesyncd
│ ├─cups.service
........
“systemctl list-units” will show all active running units, “systemctl –failed” will show the failed units.
Using units
Units can be, for example, services (.service), mount points (.mount), devices (.device) or sockets (.socket).
When using systemctl, you generally have to specify the complete name of the unit file, including its suffix, for example sshd.socket. There are however a few short forms when specifying the unit in the following systemctl commands:
If you do not specify the suffix, systemctl will assume .service. For example, netctl and netctl.service are equivalent.
Mount points will automatically be translated into the appropriate .mount unit. For example, specifying /home is equivalent to home.mount.
Similar to mount points, devices are automatically translated into the appropriate .device unit, therefore specifying /dev/sda2 is equivalent to dev-sda2.device.
weng@weng-u1604:/lib/systemd/system$ systemctl
UNIT LOAD ACTIVE SUB DESCRIPTION
proc-sys-fs-binfmt_misc.automount loaded active waiting Arbitrary Executable File Formats File System Automount Point
sys-devices-pci0000:00-0000:00:01.1-ata2-host1-target1:0:0-1:0:0:0-block-sr0.device loaded active plugged VBOX_CD-ROM
sys-devices-pci0000:00-0000:00:03.0-net-enp0s3.device loaded active plugged 82540EM Gigabit Ethernet Controller ( PRO/1000 MT Desktop Adapter)
sys-devices-pci0000:00-0000:00:05.0-sound-card0.device loaded active plugged 82801AA AC'97 Audio Controller
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda-sda1.device loaded active plugged VBOX_HARDDISK 1
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda-sda2.device loaded active plugged VBOX_HARDDISK 2
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda-sda5.device loaded active plugged VBOX_HARDDISK 5
sys-devices-pci0000:00-0000:00:0d.0-ata3-host2-target2:0:0-2:0:0:0-block-sda.device loaded active plugged VBOX_HARDDISK
sys-devices-platform-serial8250-tty-ttyS0.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS0
sys-devices-platform-serial8250-tty-ttyS1.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS1
sys-devices-platform-serial8250-tty-ttyS10.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS10
sys-devices-platform-serial8250-tty-ttyS11.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS11
sys-devices-platform-serial8250-tty-ttyS12.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS12
sys-devices-platform-serial8250-tty-ttyS13.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS13
sys-devices-platform-serial8250-tty-ttyS14.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS14
sys-devices-platform-serial8250-tty-ttyS15.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS15
sys-devices-platform-serial8250-tty-ttyS16.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS16
sys-devices-platform-serial8250-tty-ttyS17.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS17
sys-devices-platform-serial8250-tty-ttyS18.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS18
sys-devices-platform-serial8250-tty-ttyS19.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS19
sys-devices-platform-serial8250-tty-ttyS2.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS2
sys-devices-platform-serial8250-tty-ttyS20.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS20
sys-devices-platform-serial8250-tty-ttyS21.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS21
sys-devices-platform-serial8250-tty-ttyS22.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS22
sys-devices-platform-serial8250-tty-ttyS23.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS23
sys-devices-platform-serial8250-tty-ttyS24.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS24
sys-devices-platform-serial8250-tty-ttyS25.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS25
sys-devices-platform-serial8250-tty-ttyS26.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS26
sys-devices-platform-serial8250-tty-ttyS27.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS27
sys-devices-platform-serial8250-tty-ttyS28.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS28
sys-devices-platform-serial8250-tty-ttyS29.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS29
sys-devices-platform-serial8250-tty-ttyS3.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS3
sys-devices-platform-serial8250-tty-ttyS30.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS30
sys-devices-platform-serial8250-tty-ttyS31.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS31
sys-devices-platform-serial8250-tty-ttyS4.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS4
sys-devices-platform-serial8250-tty-ttyS5.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS5
sys-devices-platform-serial8250-tty-ttyS6.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS6
sys-devices-platform-serial8250-tty-ttyS7.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS7
sys-devices-platform-serial8250-tty-ttyS8.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS8
sys-devices-platform-serial8250-tty-ttyS9.device loaded active plugged /sys/devices/platform/serial8250/tty/ttyS9
sys-devices-virtual-block-ram0.device loaded active plugged /sys/devices/virtual/block/ram0
sys-devices-virtual-block-ram1.device loaded active plugged /sys/devices/virtual/block/ram1
sys-devices-virtual-block-ram10.device loaded active plugged /sys/devices/virtual/block/ram10
sys-devices-virtual-block-ram11.device loaded active plugged /sys/devices/virtual/block/ram11
sys-devices-virtual-block-ram12.device loaded active plugged /sys/devices/virtual/block/ram12
sys-devices-virtual-block-ram13.device loaded active plugged /sys/devices/virtual/block/ram13
sys-devices-virtual-block-ram14.device loaded active plugged /sys/devices/virtual/block/ram14
sys-devices-virtual-block-ram15.device loaded active plugged /sys/devices/virtual/block/ram15
sys-devices-virtual-block-ram2.device loaded active plugged /sys/devices/virtual/block/ram2
sys-devices-virtual-block-ram3.device loaded active plugged /sys/devices/virtual/block/ram3
sys-devices-virtual-block-ram4.device loaded active plugged /sys/devices/virtual/block/ram4
sys-devices-virtual-block-ram5.device loaded active plugged /sys/devices/virtual/block/ram5
sys-devices-virtual-block-ram6.device loaded active plugged /sys/devices/virtual/block/ram6
sys-devices-virtual-block-ram7.device loaded active plugged /sys/devices/virtual/block/ram7
sys-devices-virtual-block-ram8.device loaded active plugged /sys/devices/virtual/block/ram8
sys-devices-virtual-block-ram9.device loaded active plugged /sys/devices/virtual/block/ram9
sys-devices-virtual-misc-rfkill.device loaded active plugged /sys/devices/virtual/misc/rfkill
sys-devices-virtual-tty-ttyprintk.device loaded active plugged /sys/devices/virtual/tty/ttyprintk
sys-module-fuse.device loaded active plugged /sys/module/fuse
sys-subsystem-net-devices-enp0s3.device loaded active plugged 82540EM Gigabit Ethernet Controller (PRO/1000 MT Desktop Adapter)
-.mount loaded active mounted /
dev-hugepages.mount loaded active mounted Huge Pages File System
dev-mqueue.mount loaded active mounted POSIX Message Queue File System
media-sf_vb\x2dwin7\x2dshare.mount loaded active mounted /media/sf_vb-win7-share
mnt-vb\x2dwin7\x2dshare.mount loaded active mounted /mnt/vb-win7-share
run-cgmanager-fs.mount loaded active mounted /run/cgmanager/fs
run-user-1000-gvfs.mount loaded active mounted /run/user/1000/gvfs
run-user-1000.mount loaded active mounted /run/user/1000
sys-fs-fuse-connections.mount loaded active mounted FUSE Control File System
sys-kernel-debug.mount loaded active mounted Debug File System
cups.path loaded active waiting CUPS Scheduler
systemd-ask-password-plymouth.path loaded active waiting Forward Password Requests to Plymouth Directory Watch
systemd-ask-password-wall.path loaded active waiting Forward Password Requests to Wall Directory Watch
systemd-networkd-resolvconf-update.path loaded active waiting Trigger resolvconf update for networkd DNS
init.scope loaded active running System and Service Manager
session-c1.scope loaded active running Session c1 of user weng
accounts-daemon.service loaded active running Accounts Service
apparmor.service loaded active exited LSB: AppArmor initialization
apport.service loaded active exited LSB: automatic crash report generation
avahi-daemon.service loaded active running Avahi mDNS/DNS-SD Stack
cgmanager.service loaded active running Cgroup management daemon
colord.service loaded active running Manage, Install and Generate Color Profiles
cron.service loaded active running Regular background program processing daemon
cups-browsed.service loaded active running Make remote CUPS printers available locally
cups.service loaded active running CUPS Scheduler
dbus.service loaded active running D-Bus System Message Bus
getty@tty1.service loaded active running Getty on tty1
grub-common.service loaded active exited LSB: Record successful boot for GRUB
ifup-wait-all-auto.service loaded active exited Wait for all "auto" /etc/network/interfaces to be up for network-online.target
irqbalance.service loaded active running LSB: daemon to balance interrupts for SMP systems
kerneloops.service loaded active running LSB: Tool to automatically collect and submit kernel crash signatures
kmod-static-nodes.service loaded active exited Create list of required static device nodes for the current kernel
lightdm.service loaded active running Light Display Manager
ModemManager.service loaded active running Modem Manager
networking.service loaded active exited LSB: Raise network interfaces.
NetworkManager.service loaded active running Network Manager
ondemand.service loaded active exited LSB: Set the CPU Frequency Scaling governor to "ondemand"
polkitd.service loaded active running Authenticate and Authorize Users to Run Privileged Tasks
rc-local.service loaded active exited /etc/rc.local Compatibility
resolvconf.service loaded active exited Nameserver information manager
rsyslog.service loaded active running System Logging Service
rtkit-daemon.service loaded active running RealtimeKit Scheduling Policy Service
speech-dispatcher.service loaded active exited LSB: Speech Dispatcher
systemd-journal-flush.service loaded active exited Flush Journal to Persistent Storage
systemd-journald.service loaded active running Journal Service
systemd-logind.service loaded active running Login Service
systemd-modules-load.service loaded active exited Load Kernel Modules
systemd-random-seed.service loaded active exited Load/Save Random Seed
systemd-remount-fs.service loaded active exited Remount Root and Kernel File Systems
systemd-sysctl.service loaded active exited Apply Kernel Variables
systemd-timesyncd.service loaded active running Network Time Synchronization
systemd-tmpfiles-setup-dev.service loaded active exited Create Static Device Nodes in /dev
systemd-tmpfiles-setup.service loaded active exited Create Volatile Files and Directories
systemd-udev-trigger.service loaded active exited udev Coldplug all Devices
systemd-udevd.service loaded active running udev Kernel Device Manager
systemd-update-utmp.service loaded active exited Update UTMP about System Boot/Shutdown
systemd-user-sessions.service loaded active exited Permit User Sessions
systemd-vconsole-setup.service loaded active exited Setup Virtual Console
udisks2.service loaded active running Disk Manager
ufw.service loaded active exited Uncomplicated firewall
upower.service loaded active running Daemon for power management
user@1000.service loaded active running User Manager for UID 1000
vboxadd-service.service loaded active running vboxadd-service.service
vboxadd-x11.service loaded active exited vboxadd-x11.service
vboxadd.service loaded active exited vboxadd.service
whoopsie.service loaded active running crash report submission daemon
wpa_supplicant.service loaded active running WPA supplicant
-.slice loaded active active Root Slice
system-getty.slice loaded active active system-getty.slice
system.slice loaded active active System Slice
user-1000.slice loaded active active User Slice of weng
user.slice loaded active active User and Session Slice
acpid.socket loaded active listening ACPID Listen Socket
avahi-daemon.socket loaded active running Avahi mDNS/DNS-SD Stack Activation Socket
cups.socket loaded active running CUPS Scheduler
dbus.socket loaded active running D-Bus System Message Bus Socket
syslog.socket loaded active running Syslog Socket
systemd-fsckd.socket loaded active listening fsck to fsckd communication Socket
systemd-initctl.socket loaded active listening /dev/initctl Compatibility Named Pipe
systemd-journald-audit.socket loaded active running Journal Audit Socket
systemd-journald-dev-log.socket loaded active running Journal Socket (/dev/log)
systemd-journald.socket loaded active running Journal Socket
systemd-rfkill.socket loaded active listening Load/Save RF Kill Switch Status /dev/rfkill Watch
systemd-udevd-control.socket loaded active running udev Control Socket
systemd-udevd-kernel.socket loaded active running udev Kernel Socket
uuidd.socket loaded active listening UUID daemon activation socket
dev-disk-by\x2duuid-27b422b2\x2d8460\x2d41a3\x2d88c7\x2d298ed5822204.swap loaded active active /dev/disk/by-uuid/27b422b2-8460-41a3-88c7-298ed5822204
basic.target loaded active active Basic System
cryptsetup.target loaded active active Encrypted Volumes
getty.target loaded active active Login Prompts
graphical.target loaded active active Graphical Interface
local-fs-pre.target loaded active active Local File Systems (Pre)
local-fs.target loaded active active Local File Systems
multi-user.target loaded active active Multi-User System
network-online.target loaded active active Network is Online
network.target loaded active active Network
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target loaded active active Paths
remote-fs-pre.target loaded active active Remote File Systems (Pre)
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
time-sync.target loaded active active System Time Synchronized
timers.target loaded active active Timers
apt-daily.timer loaded active waiting Daily apt activities
systemd-tmpfiles-clean.timer loaded active waiting Daily Cleanup of Temporary Directories
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
169 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use ' systemctl list-unit-files'.
weng@weng-u1604:/lib/systemd/system$
A unit can be operated using “systemctl [ start
stop
restart
reload
enable
disable
mask
unmask ] unit-name”.
Power management
using “systemctl [ reboot | poweroff | suspend | hibernate ]
Using unit file
Unit files are loaded from two locations. From lowest to highest precedence they are:
/usr/lib/systemd/system/: units provided by installed packages
/etc/systemd/system/: units installed by the system administrator
Drop-in files: To create drop-in files for the unit file /usr/lib/systemd/system/unit, create the directory /etc/systemd/system/unit.d/ and place .conf files there to override or add new options. systemd will parse these .conf files and apply them on top of the original unit.
Service types
There are several different start-up types to consider when writing a custom service file. This is set with the Type= parameter in the [Service] section:
Type=simple (default): systemd considers the service to be started up immediately. The process must not fork. Do not use this type if other services need to be ordered on this service, unless it is socket activated.
Type=forking: systemd considers the service started up once the process forks and the parent has exited. For classic daemons use this type unless you know that it is not necessary. You should specify PIDFile= as well so systemd can keep track of the main process.
Type=oneshot: this is useful for scripts that do a single job and then exit. You may want to set RemainAfterExit=yes as well so that systemd still considers the service as active after the process has exited.
Type=notify: identical to Type=simple, but with the stipulation that the daemon will send a signal to systemd when it is ready. The reference implementation for this notification is provided by libsystemd-daemon.so.
Type=dbus: the service is considered ready when the specified BusName appears on DBus's system bus.
Type=idle: systemd will delay execution of the service binary until all jobs are dispatched. Other than that behavior is very similar to Type=simple.
See the systemd.service(5) man page for a more detailed explanation of the Type values.
target
systemd uses targets which serve a similar purpose as runlevels but act a little different. Each target is named instead of numbered and is intended to serve a specific purpose with the possibility of having multiple ones active at the same time. Some targets are implemented by inheriting all of the services of another target and adding additional services to it. There are systemd targets that mimic the common SystemVinit runlevels so you can still switch targets using the familiar telinit RUNLEVEL command.
Get current targets
eng@weng-u1604:/lib/systemd/system$ systemctl list-units --type = target
UNIT LOAD ACTIVE SUB DESCRIPTION
basic.target loaded active active Basic System
cryptsetup.target loaded active active Encrypted Volumes
getty.target loaded active active Login Prompts
graphical.target loaded active active Graphical Interface
local-fs-pre.target loaded active active Local File Systems ( Pre)
local-fs.target loaded active active Local File Systems
multi-user.target loaded active active Multi-User System
network-online.target loaded active active Network is Online
network.target loaded active active Network
nss-user-lookup.target loaded active active User and Group Name Lookups
paths.target loaded active active Paths
remote-fs-pre.target loaded active active Remote File Systems ( Pre)
remote-fs.target loaded active active Remote File Systems
slices.target loaded active active Slices
sockets.target loaded active active Sockets
sound.target loaded active active Sound Card
swap.target loaded active active Swap
sysinit.target loaded active active System Initialization
time-sync.target loaded active active System Time Synchronized
timers.target loaded active active Timers
LOAD = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB = The low-level unit activation state, values depend on unit type.
20 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files' .
weng@weng-u1604:/lib/systemd/system$
Targets table
The relation between targets defines the system boot sequence, the typical system boot sequence is like below:
Understand Systemd from Redhat has a great explanation for that.
Unit sample file
[ Unit]
Description = Virtual Distributed Ethernet
After = syslog.target
[ Service]
Type = forking
PIDFile = /var/run/vde.pid
ExecStart = /usr/bin/vde_switch --tap tap0 --mode 0660 \
--dirmode 0750 --group qemu \
--daemon --pidfile /var/run/vde.pid
Restart = on-abort
[ Install]
WantedBy = multi-user.target
reference
https://wiki.archlinux.org/index.php/Systemd
https://en.wikipedia.org/wiki/Systemd