Repository navigation
Expand file tree
/
Copy pathUsing Systemd
More file actions
113 lines (89 loc) · 5.74 KB
/
Copy pathUsing Systemd
File metadata and controls
113 lines (89 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
Linux system unit configuration files location:
/lib/systemd/system
/etc/systemd/system
There are eight systemd unit types:
- automount
- device
- mount
- path
- service: A service unit is for managing daemons on your Linux server.
- snapshot
- socket
- target: A target unit is simply a group of other units.
Search for a unit type:
# systemctl list-units | grep *.service or | grep *.target
List all unit of type
# systemctl list-unit-files --type=service --all or --type=target --all
# systemctl list-unit-files --type=service --all | grep sshd
# cat /lib/systemd/system/sshd.service (check out a config file *.service)
Description: A free-form description of the service.
After: lists units that should be activated before this service is started.
Environment File: The service’s configuration file.
ExecStart: The command used to start this service.
ExecReload: The command used to reload this service.
WantedBy: The target unit this service belongs to.
Look at units:
#ls /usr/lib/systemd/system/*.target or *.<unit>
Then view the *.target or *.<unit>
# cat /lib/systemd/system/multi-user.target (This config has added parameters)
Requires: If this multi-user.target gets activated, the listed target unit is
also activated. If the listed target unit is deactivated or fails, then multi-user.
target is deactivated. If there are no After and Before options, then both multiuser.
target and listed target unit activate simultaneously.
AllowIsolate: If set to yes, then this target unit, multi-user.target,
is activated along with its dependencies and all others are deactivated.
Conflicts: This setting avoids conflicts in services. Starting multi-user.
target stops the listed targets and services, and vice-versa.
View units that a target unit will activate.
# systemctl show --property "Wants" multi-user.target \ | fmt -10 | sed 's/Wants=//g' | sort
View units a target unit must activate or requires because that unit will fail.
# systemctl show --property "Requires" multi-user.target
See all the services that are being offered by a Linux server using systemd:
# systemctl list-unit-files --type=service | grep -v disabled
List dependencies on a service:
# systemctl list-dependencies ssh.service
systemd do runlevels are call target; therefore, we can see systemd's *.target names to each runlevel:
# ls -ld /usr/lib/systemd/system/runlevel*
More info on SysVinit systems runlevel:
| Runlevel | Name | Description |
| -------- | ------------------- | --------------------------------------------------------------------- |
| `0` | Halt | Shuts down the system safely. |
| `1` | Single-user mode | Maintenance mode: no networking, only root shell. Ideal for recovery. |
| `2` | Multi-user (no NFS) | Like 3 but without remote (network) filesystems. Often not used. |
| `3` | Multi-user | Full multi-user text mode (no GUI). Used for headless servers. |
| `4` | Undefined | User-definable. Rarely used. |
| `5` | Graphical | Multi-user with GUI (X11/Wayland). Default for desktops. |
| `6` | Reboot | Restarts the system. |
Info on how systemd uses SysVinit systems runlevel:
| Situation | What to Use |
| ------------------------------- | ----------------------------------- |
| Boot into text mode only | Runlevel 3 / `multi-user.target` |
| Need maintenance as root | Runlevel 1 / `rescue.target` |
| Want full desktop login | Runlevel 5 / `graphical.target` |
| Clean reboot | Runlevel 6 / `reboot.target` |
| Emergency shell with minimal fs | `emergency.target` |
| Custom server mode | Define your own Runlevel 4 / target |
How to use systemd runlevel *.target: (These commands take affect after reboot)
# systemctl get-default Check current target
# systemctl set-default graphical.target Change default target
# systemctl isolate rescue.target Boot into rescue mode
# systemctl isolate emergency.target Boot into emergency mode
Remember that systemd can be used as a replacement for cron to run schedule for jobs.
Control and manage units using systemd subcommands with systemctl:
# systemctl status cups.service
# systemctl stop cups.service
# systemctl start cups.service : Use this when you need service right now, but will not persist after reboot.
# systemctl restart cups.service
# systemctl condrestart cups.service : Conditional restart only restarts a service if it is currently running.
# systemctl reload sshd.service : Doing a reload on a service, instead of a restart, prevents any pending service
operations from being aborted.
# systemctl enable cups.service : This service will now persist after reboot.
# systemctl disable cups.service
Use systemd way of taking logging information using journalctl:
journalctl List all journal listings
journalctl -n 5 show the last 5 log entries
journalctl -f Show live updated entries
journalctl -b Show current boot sessions
# systemctl status cups.service (Using SysVinit services)
# service cups start or service cups stop or restart or status or reload (reload is use to restart the service
and the service will not shut down the system)