systemd

来自百合仙子's Wiki
跳转到导航 跳转到搜索

技巧

启动后不要清除 tty1 的内容

/etc/systemd/system/getty@tty1.service.d/dontclear.conf 里写上:

[Service]
TTYVTDisallocate=no

不要杀死服务 fork 出来的进程

例如不要杀掉 ssh fork 出来的 mux 进程,添加如下字段:

[Service]
KillMode=process

用户级的 sleep.target

建立一个用户的sleep.target

[Unit]
Description=Sleep
Documentation=man:systemd.special(7)
DefaultDependencies=no
StopWhenUnneeded=yes

然后建立并启用一个系统级的服务user-sleep@.service来执行这个 target:

[Unit]
Description=sleep.target of a systemd user session
Before=sleep.target
StopWhenUnneeded=yes

[Service]
Type=oneshot
User=%I
Environment=DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/%I/bus
RemainAfterExit=yes
ExecStart=/usr/bin/systemctl --user start sleep.target
ExecStop=/usr/bin/systemctl --user stop sleep.target

[Install]
WantedBy=sleep.target
sudo systemctl enable user-sleep@1000.service

加固服务

常用的设置[1]

PrivateTmp=true
PrivateDevices=true
ProtectSystem=full
ProtectHome=true
NoNewPrivileges=true
CapabilityBoundingSet=

coredump

禁用 systemd 的 coredump 管理[2]

ln -s /dev/null /etc/sysctl.d/50-coredump.conf
# 重新载入配置并不改变 kernel.core_pattern 的设置。需要重启。
# /lib/systemd/systemd-sysctl

参见 man 手册 sysctl.d(5)

注意:systemd-coredump 的 core 大小是硬编码的(2013年6月5日,systemd 204)

网络设备名

要禁止 systemd-udevd 将网络设备名由 eth0wlan0 修改为 enp0s3 等,可添加空的 udev 规则文件 /etc/udev/rules.d/80-net-setup-link.rules(旧版本为 /etc/udev/rules.d/80-net-name-slot.rules)。[3]

挂载文件系统

systemd-remount-fs 会读取 /etc/fstab 并挂载其中提到的文件系统。如果挂载失败,会进入 emergency 模式。如果失败的文件系统不是必要的(比如 /boot),可以在 emergency 模式中注释掉此文件系统的条目,然后执行 systemctl daemon-reload 并退出。

环境变量

SYSTEMD_LOG_TARGET
日志输出目标。默认为「auto」,即终端或者 journal。可设置为「console」。定义于 src/basic/log.h:45
SYSTEMD_LOG_LEVEL
日志级别,如「debug」

参见

外部链接

批评

参考资料