Systemd
-
systemd is goal-oriented. These goals are defined as ‘units’
-
Units are systemd objects used for organizing boot and maintenance tasks. Units consist of mounts, services, sockets, devices, and timers, etc.
-
There are 11 unit types
- Services units tells the init system what it needs to know about the life cycle of an application
- systemd is the init system typically
- Use
systemctl cat sshd.serviceto view the unit file for a service - Use
systemctl edit --full sshd.serviceto edit a unit file - use
systemctl revert sshd.serviceto revert the unit file to the default
-
To prevent a service from being started, you can mask it
- systemctl mask sshd.service
-
Targets are simply logical collections of units.
-
Target files end in the
.targetextension. -
Systemd includes several predefined targets:
- halt: shuts down and halts the system
- poweroff: shuts down and powers off the system
- shutdown: shuts down the system
- rescue: boots into single user mode for recovery. All local file systems are mounted. Networking is disabled. Some essential services are started
- emergency: Runs an emergency shell. The root file system is mounted in read-only mode, other file systems are not mounted. Network and other services are disabled
- multi-user: full network support, but without a GUI
- graphical: full network support with a GUI
- reboot: shuts down and reboots the system
- default: a special soft link that points to the default system boot target (multi-user or graphical)
- hibernate: Puts the system into hibernation
-
systemctl get-defaultwill show you the default target -
use
systemctl set-default multi-user.targetto set the default operating mode, then reboot -
Useful targets:
-
emergency.target = root file system is read-only. Minimal amount of programs loaded
-
rescue.target = a few services are loaded and you are dropped into a root shell
- You must have a password set for the root user to use either of these operating modes
-
You can switch to a target without booting by typing
systemctl isolate graphical.target, but this does not change the default boot target
-
-
Each unit has its own config file
-
When you boot a system, you’re activating a default unit, usually a target unit called
default.targetthat groups together a number of service and mount units as dependencies. -
There are two main directories that store systemd unit files:
/lib/systemd/systemor/usr/lib/system/system- system unit directory (avoid making changes. The operating system will maintain these files for you.)/etc/systemd/system- system configuration directory (make changes here)- You can check the current systemd configuration search path with this command:
systemctl -p UnitPath show
-
You can interact with systemd using the
systemctlcommand -
One of
systemds features is the ability to delay a daemon startup until it is absolutely needed -
While upgrading software, if systemd’s components are upgraded, you will typically need to reboot
Systemd example
Let’s create a simple echo service
First, define a socket (create a file named echo.socket in /etc/systemd/system)::
[Unit]
Description=my echo socket
[Socket]
ListenStream=8081
Accept=true
Next, define a service for echoing a response (create a file named echo@.service in /etc/systemd/system):
[Unit]
Description=my echo service
[Service]
ExecStart=/bin/cat
StandardInput=socket
Now, we need to start the socket we created in step 1.
systemctl start echo.socket
We can get the status of our socket:
ryan:system/ $ sudo systemctl status echo.socket
● echo.socket - my echo socket
Loaded: loaded (/etc/systemd/system/echo.socket; static)
Active: active (listening) since Tue 2023-01-17 06:02:51 EST; 7s ago
Listen: [::]:8081 (Stream)
Accepted: 0; Connected: 0;
Tasks: 0 (limit: 38033)
Memory: 8.0K
CPU: 911us
CGroup: /system.slice/echo.socket
Jan 17 06:02:51 xerxes systemd[1]: Listening on my echo socket.
Now you can connect to the socket and see it repeat whatever you say!
ryan:system/ $ nc localhost 8081
hello
hello
nice day, isn't it?
nice day, isn't it?