systemd
跳转到导航
跳转到搜索
技巧
启动后不要清除 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 将网络设备名由 eth0、wlan0 修改为 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 并退出。
自动卸载文件系统
在 /etc/fstab 及 mount 单元中设置的挂载点,当其对应的设备文件发生变化时,systemd 会自动尝试卸载该文件系统。如要防止这种情况,需要设置 x-systemd.device-bound=no 选项。[4]例如:
btrfs device add /dev/mapper/disk1 /mnt
btrfs device remove /dev/mapper/disk2 /mnt
此时 systemd 将尝试卸载 /mnt。
SYSTEMD_LOG_TARGET- 日志输出目标。默认为「auto」,即终端或者 journal。可设置为「console」。定义于
src/basic/log.h:45 SYSTEMD_LOG_LEVEL- 日志级别,如「debug」
参见
外部链接
- systemd.index 中文手册 [金步国]
- Systemd - ArchWiki
- SysVinit to Systemd Cheatsheet - FedoraProject (中文)
- 高速启动,现在开始systemd _ 穆童博客
- Arch migrates to SystemD ..and gets a little-bit better Gnome support! | woGue,包含 sysvinit、upstart 和 systemd 的对比
- 开机自动更换MAC地址 - @roylez: bizarrely trapped
- openSUSE:How to write a systemd service - openSUSE
- [1], systemd Status Update, 介绍了一些 systemd 特性
- Boot time comparison: OpenRC vs systemd - YouTube
- How to use systemd timers - Jason's Blog
- The Three Levels of "Off"
- Jorgen’s Weblog: Why systemd?
- Chris's Wiki :: Things that systemd gets right
- systemd 關機時, 進程結束超時定位方法