Systemd The giant monster that ate Linux
SysV Init is comfy Dates back to AT&T System III from 1982 Modified up until AT&T Sys V in 1983
systemd is Easy
Systemd Managing The System
System Service Management Unified System Management Tab completion for commands Aggressive parallelization Transactional activation logic Power management Backward compatibility
Unified System Management Multiple non-integrated tools System management by committee Can hinder forward progress X11? -> Wayland & Mir
Unified System Management (cont.) One system to start them all, One system to stop them, One system to log them all and with ease manage them.
Tab Completion Makes life easier First tab adds a space Subsequent tabs work like BASH tab completion
Tab Completion Example [jaybrown@localhost ~]$ systemd-<tab><tab><tab> systemd-analyze systemd-machine-id-setup systemd-ask-password systemd-notify systemd-cat systemd-nspawn systemd-cgls systemd-path systemd-cgtop systemd-run systemd-delta systemd-stdio-bridge systemd-detect-virt systemd-sysusers systemd-escape systemd-tmpfiles systemd-firstboot systemd-tty-ask-password-agent systemd-inhibit
Can Be Overwhelming [jaybrown@localhost ~]$ sysctl<tab><tab><tab> Display all 745 possibilities? (y or n)
Power Management systemctl reboot systemctl poweroff systemctl suspend systemctl hibernate systemctl hybrid-sleep
Aggressive parallelization SysV init: inherently serial Upstart: parallelization using a dependency graph Systemd: socket activation can make all service startup parallel
Transactional activation Before activating or deactivating a unit, systemd calculates its dependencies, creates a temporary transaction, and verifies that this transaction is consistent. If a transaction is inconsistent, systemd automatically attempts to correct it and remove non-essential jobs from it before reporting an error. systemd performs dependency checking and attempts to handle dependencies before reporting an error
Backward compatibility systemd processes the scripts in /etc/init.d legacy commands telinit, init, halt, shutdown, reboot, runlevel
Unit Files Defining Services
Types of Unit Files systemctl -t help service socket target device mount automount snapshot timer swap path slice scope
Locations - System Mode /usr/lib/systemd/system/ - Installed Packages /etc/systemd/system/ - System Administrator
Locations - User Mode /usr/lib/systemd/user/ - Provided by installed packages /etc/systemd/user/ - System-wide user services ~/.config/systemd/user/ - User specific services
Writing http://www.freedesktop. org/software/systemd/man/systemd.unit.html systemd.service(5), systemd.socket(5), systemd.device(5), systemd.mount(5), systemd.automount(5), systemd.swap(5), systemd.target(5), systemd.path(5), systemd.timer(5), systemd.snapshot(5). systemd.slice(5). systemd.scope(5)
Example Service Unit File [Unit] Description=OpenSSH server daemon After=syslog.target network.target auditd.service [root@ursus jaybrown]# cat /etc/init.d/sshd wc -l 236 [Service] EnvironmentFile=/etc/sysconfig/sshd ExecStartPre=/usr/sbin/sshd-keygen ExecStart=/usr/sbin/sshd -D $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
Socket Units listens on a IP port or UNIX socket queues request until the service is available
Example.socket file [Unit] Description=OpenSSH Server Socket Conflicts=sshd.service [Socket] ListenStream=22 Accept=yes [Install] WantedBy=sockets.target
Service Units listens for D-Bus communication and activate services uses D-Bus service files for bus-based activation
Example D-Bus.service file [Unit] Description=Bluetooth service [Service] Type=dbus BusName=org.bluez ExecStart=/usr/sbin/bluetoothd -n [Install] WantedBy=bluetooth.target Alias=dbus-org.bluez.service
Device Units enables services based on device connection or activation named after the device paths they control dev-sda5.device = /dev/sda5 configured directly from the udev database
Example.device file [jaybrown@localhost Documents]$ systemctl status dev-sda1.device dev-sda1.device - KINGSTON_SNS4151S316G 1 Follow: unit currently follows state of sys-devices-pci0000:00-0000:00:1f.2-ata1-host0- target0:0:0-0:0:0:0-block-sda-sda1.device Loaded: loaded Active: active (plugged) since Sat 2014-12-27 06:16:37 CST; 1h 14min ago Device: /sys/devices/pci0000:00/0000:00:1f.2/ata1/host0/target0:0:0/0:0:0:0/block/sda/sda1
Path Units encodes information about a path monitored by systemd takes action on filesystem activity in path
Example.path file [Unit] Description=Forward Password Requests to Wall Directory Watch Documentation=man:systemd-ask-password-console.service(8) DefaultDependencies=no Conflicts=shutdown.target Before=paths.target shutdown.target [Path] DirectoryNotEmpty=/run/systemd/ask-password MakeDirectory=yes
Mount and automount point management reads /etc/fstab and dynamically generates native units /etc/fstab is preferred over static mount units must be named for its mountpoint /home/user file is home-user.mount
Example.automount file [Unit] Description=Arbitrary Executable File Formats File System Automount Point Documentation=https://www.kernel. org/doc/documentation/binfmt_misc.txt Documentation=http://www.freedesktop. org/wiki/software/systemd/apifilesystems DefaultDependencies=no Before=sysinit.target ConditionPathExists=/proc/sys/fs/binfmt_misc/ ConditionPathIsReadWrite=/proc/sys/ [Automount] Where=/proc/sys/fs/binfmt_misc
Example.mount file [Unit] Description=Debug File System Documentation=https://www.kernel. org/doc/documentation/filesystems/debugfs.txt Documentation=http://www.freedesktop. org/wiki/software/systemd/apifilesystems DefaultDependencies=no ConditionPathExists=/sys/kernel/debug Before=sysinit.target [Mount] What=debugfs Where=/sys/kernel/debug Type=debugfs
Managing Units New Commands
Listing Units In Specific State or States [jaybrown@localhost ~]$ systemctl list-unit-files --state=failed UNIT FILE STATE 0 unit files listed. [jaybrown@localhost ~]$ systemctl list-unit-files --state=disabled,enabled UNIT FILE cups.path abrt-ccpp.service abrt-oops.service abrt-pstoreoops.service STATE enabled enabled enabled disabled
Listing Only Enabled Socket Units [jaybrown@localhost ~]$ systemctl list-unit-files -t socket --state=enabled UNIT FILE STATE avahi-daemon.socket enabled cups.socket dm-event.socket iscsid.socket iscsiuio.socket enabled enabled enabled enabled lvm2-lvmetad.socket enabled rpcbind.socket enabled
Disabling a Unit [jaybrown@localhost ~]$ systemctl disable cups Removed symlink /etc/systemd/system/printer.target.wants/cups.service. Removed symlink /etc/systemd/system/multi-user.target.wants/cups.path. Removed symlink /etc/systemd/system/sockets.target.wants/cups.socket.
Enabling a Unit [jaybrown@localhost ~]$ systemctl enable cups Created symlink from /etc/systemd/system/printer.target.wants/cups.service to /usr/lib/systemd/system/cups.service. Created symlink from /etc/systemd/system/sockets.target.wants/cups.socket to /usr/lib/systemd/system/cups.socket. Created symlink from /etc/systemd/system/multi-user.target.wants/cups.path to /usr/lib/systemd/system/cups.path.
systemd Journal journalctl provides the ability to work with the systemd journal as written by systemd-journal.service Without any parameters it displays the full contents of the journal journalctl --since "20 min ago" Can use / to search within results
System state snapshots Save the state of the system configuration in a snapshot systemctl <snapshot name> creates a snapshot of the current state under the name <snapshot name> systemctl isolate <snapshot name>.service restores the system state to the state saved in the snapshot <snapshot name> systemctl delete <snapshot name> deletes a previously saved snapshot man 5 systemd.snapshot & man systemctl
Reference Where to get more detailed information
Commands Full man page index at http://0pointer.de/public/systemd-man/ /usr/bin/busctl /usr/bin/coredumpctl /usr/sbin/halt /usr/bin/hostname /usr/bin/hostnamectl /usr/sbin/init /usr/bin/journalctl /usr/bin/localectl /usr/bin/loginctl /usr/bin/machinectl /usr/sbin/reboot /usr/sbin/runlevel /usr/sbin/shutdown /usr/sbin/sysctl /usr/bin/systemctl /usr/sbin/telinit /usr/bin/timedatectl /usr/bin/udevadm
Good Pages https://wiki.archlinux.org/index.php/systemd http://freedesktop.org/wiki/software/systemd/ Beyond init: systemd RedHat 7 Documentation systemd man pages