Управление сервисами - efanov/mephi GitHub Wiki
Управление сервисами
Введение
- Задача управления системными и сетевыми сервисами. Менеджеры init, systemd, launchcd (macOS).
Системный менеджер systemd
-
Назначение, состав и возможности системного менеджера systemd.
-
Понятие юнита. Пример.
$ cat /usr/lib/systemd/system/sshd.service [Unit] Description=OpenSSH server daemon Documentation=man:sshd(8) man:sshd_config(5) After=network.target sshd-keygen.service Wants=sshd-keygen.service [Service] Type=notify EnvironmentFile=/etc/sysconfig/sshd ExecStart=/usr/sbin/sshd -D $OPTIONS ExecReload=/bin/kill -HUP $MAINPID KillMode=process Restart=on-failure RestartSec=42s [Install] WantedBy=multi-user.target
-
Типы юнитов: service, target, socket, busname, snapshot, device, mount, automount, swap, timer, path, slice, scope.
Available systemd Unit Types
Unit Type | File Extension | Description |
---|---|---|
Service unit | .service | A system service. |
Target unit | .target | A group of systemd units. |
Automount unit | .automount | A file system automount point. |
Device unit | .device | A device file recognized by the kernel. |
Mount unit | .mount | A file system mount point. |
Path unit | .path | A file or directory in a file system. |
Scope unit | .scope | An externally created process. |
Slice unit | .slice | A group of hierarchically organized units that manage system processes. |
Snapshot unit | .snapshot | A saved state of the systemd manager. |
Socket unit | .socket | An inter-process communication socket. |
Swap unit | .swap | A swap device or a swap file. |
Timer unit | .timer | A systemd timer. |
Вывести информацию о доступных типах юнитов:
$ systemctl -t help
Available unit types:
service
socket
busname
target
snapshot
device
mount
automount
swap
timer
path
slice
scope
- Состояния юнита.
- Зависимости юнитов.
- Расположение юнитов в файловой системе - Systemd Unit Files Locations
Directory | Description |
---|---|
/usr/lib/systemd/system/ | Systemd unit files distributed with installed RPM packages. |
/run/systemd/system/ | Systemd unit files created at run time. This directory takes precedence over the directory with installed service unit files. |
/etc/systemd/system/ | Systemd unit files created by systemctl enable as well as unit files added for extending a service. This directory takes precedence over the directory with runtime unit files. |
Вывести алиасы для сервиса
$ systemctl show auditd.service -p Names
Names=auditd.service
Управление service-юнитами
- Управление service-юнитами. Разрешенные сервисы (запускаются при загрузке, enable) и запрещённые (disable). Запуск, останов и перезагрузка сервиса.
systemctl | Description |
---|---|
systemctl start name.service | Starts a service. |
systemctl stop name.service | Stops a service. |
systemctl restart name.service | Restarts a service. |
systemctl try-restart name.service | Restarts a service only if it is running. |
systemctl reload name.service | Reloads configuration. |
systemctl status name.service systemctl is-active name.service | Checks if a service is running. |
systemctl list-units --type service --all | Displays the status of all services. |
systemctl enable name.service | Enables a service. |
systemctl disable name.service | Disables a service. |
systemctl status name.service systemctl is-enabled name.service | Checks if a service is enabled. |
systemctl list-unit-files --type service | Lists all services and checks if they are enabled. |
systemctl list-dependencies --after | Lists services that are ordered to start before the specified unit. |
systemctl list-dependencies --before | Lists services that are ordered to start after the specified unit. |
Управление target-юнитами
- Управление target-юнитами, target-юнит по умолчанию.
- Режим работы системы rescue (однопользовательский режим, монтируются локальные файловые системы, сеть не активируется).
- Режим работы системы emergency (минимальное окружение, корневая файловая система только для чтения, локальные файловые системы не монтируются, сеть не активируется).
- Управление работой системы и питанием компьютера. Выгрузка системы (с отключением питание компьютера poweroff и без halt), перезагрузка reboot, приостановка suspend и остановка hibernate системы.
Создание собственных юнитов
- Имя юнита
unit_name.type_extension
. - Структура юнита, секции и их опции.
Команды и файлы
- Команды:
systemctl
,journalctl
. - Файлы и каталоги:
/usr/lib/systemd/system/
,/run/systemd/system/
,/etc/systemd/system/
.
Лабораторная работа
Лабораторная работа "Управление системными и сетевыми сервисами"
Ссылки
- Официальный сайт systemd
- Understanding Systemd
- Chapter 8. Managing Services with systemd
- systemd для администраторов
- http://man7.org/linux/man-pages/man5/systemd.unit.5.html
- http://man7.org/linux/man-pages/man7/bootparam.7.html
- https://www.kernel.org/doc/html/latest/admin-guide/kernel-parameters.html