systemd-custom.xml 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <!DOCTYPE sect1 PUBLIC "-//OASIS//DTD DocBook XML V4.5//EN"
  3. "http://www.oasis-open.org/docbook/xml/4.5/docbookx.dtd" [
  4. <!ENTITY % general-entities SYSTEM "../general.ent">
  5. %general-entities;
  6. ]>
  7. <sect1 id="ch-scripts-systemd-custom" revision="systemd">
  8. <?dbhtml filename="systemd-custom.html"?>
  9. <title>Systemd 使用和配置</title>
  10. <indexterm zone="ch-scripts-systemd-custom">
  11. <primary sortas="e-Systemd">Systemd Customization</primary>
  12. </indexterm>
  13. <sect2>
  14. <title>基础设置</title>
  15. <para><filename>/etc/systemd/system.conf</filename>
  16. 文件包含一组控制 systemd 基本功能的选项。
  17. 默认文件中所有条目都被注释掉,并标明了默认值。
  18. 可以在这里修改日志级别,以及其他一些基本日志设定。
  19. 参阅 <filename>systemd-system.conf(5)</filename>
  20. man 手册页面了解每个选项的详细信息。</para>
  21. </sect2>
  22. <sect2>
  23. <title>禁用引导时自动清屏</title>
  24. <para>Systemd 的默认行为是在引导过程结束时清除屏幕。
  25. 如果希望的话,您可以运行以下命令,修改这一行为:</para>
  26. <screen role="nodump"><userinput>mkdir -pv /etc/systemd/system/getty@tty1.service.d
  27. cat &gt; /etc/systemd/system/getty@tty1.service.d/noclear.conf &lt;&lt; EOF
  28. <literal>[Service]
  29. TTYVTDisallocate=no</literal>
  30. EOF</userinput></screen>
  31. <para>您总是可以用 root 身份运行
  32. <userinput>journalctl -b</userinput> 命令,查阅引导消息。</para>
  33. </sect2>
  34. <sect2>
  35. <title>禁止将 tmpfs 挂载到 /tmp</title>
  36. <para>默认情况下,
  37. <filename class="directory">/tmp</filename> 被挂载 tmpfs 文件系统。
  38. 如果不希望这样,可以执行以下命令覆盖这一行为:</para>
  39. <screen role="nodump"><userinput>ln -sfv /dev/null /etc/systemd/system/tmp.mount</userinput></screen>
  40. <para>或者,如果希望使用一个单独的
  41. <filename class="directory">/tmp</filename> 分区,
  42. 在 <filename>/etc/fstab</filename> 中为其添加一个条目。</para>
  43. <warning>
  44. <para>
  45. 如果使用了单独的
  46. <filename class="directory">/tmp</filename> 分区,
  47. 不要创建上面的符号链接。这会导致根文件系统(/)
  48. 无法重新挂载为可读写,使得系统在引导后不可用。
  49. </para>
  50. </warning>
  51. </sect2>
  52. <sect2>
  53. <title>配置文件自动创建和删除</title>
  54. <para>有一些创建或删除文件、目录的服务:</para>
  55. <itemizedlist>
  56. <listitem><para>systemd-tmpfiles-clean.service</para></listitem>
  57. <listitem><para>systemd-tmpfiles-setup-dev.service</para></listitem>
  58. <listitem><para>systemd-tmpfiles-setup.service</para></listitem>
  59. </itemizedlist>
  60. <para>它们的系统配置文件位于
  61. <filename>/usr/lib/tmpfiles.d/*.conf</filename>,
  62. 本地配置文件位于
  63. <filename class="directory">/etc/tmpfiles.d</filename>。
  64. <filename class="directory">/etc/tmpfiles.d</filename>
  65. 中的文件覆盖
  66. <filename class="directory">/usr/lib/tmpfiles.d</filename>
  67. 中的同名文件。参阅
  68. <filename>tmpfiles.d(5)</filename> man 手册页面,
  69. 了解配置文件格式的细节。</para>
  70. <para>
  71. 注意
  72. <filename>/usr/lib/tmpfiles.d/*.conf</filename> 文件的语法较难理解。
  73. 例如,删除 /tmp 目录下文件的默认规则是文件
  74. <filename>/usr/lib/tmpfiles.d/tmp.conf</filename> 的一行:
  75. <screen role="nodump">q /tmp 1777 root root 10d</screen>
  76. <!--The type field, q, discusses creating a subvolume with quotas which
  77. is really only applicable to btrfs filesystems. It references type v
  78. which in turn references type d (directory). This then creates the
  79. specified directory if is is not present and adjusts the permissions
  80. and ownership as specified. Contents of the directory will be
  81. subject to time based cleanup if the age argument is specified. -->
  82. 类别字段 q 表示创建一个带有配额的子卷,它实际上只适用于 btrfs
  83. 文件系统。它引用类别 v ,类别 v 又引用类别 d ,
  84. 类别 d 在目录不存在时自动创建它,并根据配置文件调整其权限和所有者。
  85. 如果 age 参数被指定(如上面的 10d),该目录中较老的文件会被自动清理。
  86. </para>
  87. <!--para>
  88. If the default parameters are not desired, then the file should
  89. be copied to <filename class="directory">/etc/tmpfiles.d</filename>
  90. and edited as desired. For example:-->
  91. <para>如果默认参数不符合您的期望,您可以将文件复制到
  92. <filename class="directory">/etc/tmpfiles.d</filename>
  93. 目录,再编辑复制得到的副本。例如:
  94. <screen role="nodump"><userinput>mkdir -p /etc/tmpfiles.d
  95. cp /usr/lib/tmpfiles.d/tmp.conf /etc/tmpfiles.d</userinput></screen>
  96. </para>
  97. </sect2>
  98. <sect2>
  99. <title>覆盖系统服务默认行为</title>
  100. <para>Systemd 单元的参数可以通过在
  101. <filename class="directory">/etc/systemd/system</filename>
  102. 中创建一个包含配置文件的目录而覆盖。例如:</para>
  103. <screen role="nodump"><userinput>mkdir -pv /etc/systemd/system/foobar.service.d
  104. cat > /etc/systemd/system/foobar.service.d/foobar.conf &lt;&lt; EOF
  105. <literal>[Service]
  106. Restart=always
  107. RestartSec=30</literal>
  108. EOF</userinput></screen>
  109. <para>参阅 <filename>systemd.unit(5)</filename>
  110. man 手册页面获取更多信息。在创建配置文件后,执行
  111. <userinput>systemctl daemon-reload</userinput> 和 <userinput>systemctl
  112. restart foobar</userinput>,激活对服务进行的修改。</para>
  113. </sect2>
  114. <sect2>
  115. <title>调试引导过程</title>
  116. <!--para>Rather than plain shell scripts used in SysVinit or BSD style init
  117. systems, systemd uses a unified format for different types of startup
  118. files (or units). The command <command>systemctl</command> is used to
  119. enable, disable, control state, and obtain status of unit files. Here
  120. are some examples of frequently used commands:</para-->
  121. <para>与 SysVinit 或 BSD 风格 init 系统不同,
  122. systemd 使用统一格式处理不同种类的启动文件(或称为单元)。
  123. 命令 <command>systemctl</command> 能够启用、禁用单元文件,
  124. 或控制、查询单元文件的状态。以下是一些常用的命令:</para>
  125. <itemizedlist>
  126. <listitem>
  127. <para><command>systemctl list-units -t service [--all]</command>:
  128. 列出已加载的服务(service)类型单元文件。</para>
  129. </listitem>
  130. <listitem>
  131. <para><command>systemctl list-units -t target [--all]</command>:
  132. 列出已加载的目标(target)类型单元文件。</para>
  133. </listitem>
  134. <listitem>
  135. <para><command>systemctl show -p Wants <replaceable>multi-user.target</replaceable></command>:
  136. 显示所有依赖于 multi-user 目标的单元,
  137. 目标(target)是一种和 SysVinit 中运行级别(runlevel)
  138. 地位相同的特殊单元文件。</para>
  139. </listitem>
  140. <listitem>
  141. <para><command>systemctl status <replaceable>&lt;servicename.service&gt;</replaceable></command>:
  142. 显示名为 servicename 的服务的状态。如果没有同名的其他类型单元文件,
  143. 可以省略 .service 后缀。其他类型的单元文件有 .socket 文件
  144. (它创建一个监听套接字,提供和 inetd/xinetd 类似的功能)。</para>
  145. </listitem>
  146. </itemizedlist>
  147. </sect2>
  148. <sect2>
  149. <title>使用 systemd 日志</title>
  150. <!--para>Logging on a system booted with systemd is handled with
  151. systemd-journald (by default), rather than a typical unix syslog daemon.
  152. You can also add a normal syslog daemon and have both work side by
  153. side if desired. The systemd-journald program stores journal entries in a
  154. binary format rather than a plain text log file. To assist with
  155. parsing the file, the command <command>journalctl</command> is provided.
  156. Here are some examples of frequently used commands:</para-->
  157. <para>(默认情况下)在使用 systemd 引导的系统上,
  158. systemd-journald 服务负责处理日志,它取代了传统的 Unix syslog
  159. 守护进程。如果您希望的话,也可以添加一个普通 syslog 守护进程,
  160. 它和 systemd-journald 可以一起工作。systemd-journald
  161. 程序将日志项储存为二进制格式,而不是纯文本日志文件。
  162. 为了解析日志文件,需要使用 systemd 提供的
  163. <command>journalctl</command> 命令。下面是该命令的常见用法:</para>
  164. <itemizedlist>
  165. <listitem>
  166. <para><command>journalctl -r</command>:按时间顺序,
  167. 倒序显示所有日志内容。</para>
  168. </listitem>
  169. <listitem>
  170. <para><command>journalctl -u <replaceable>UNIT</replaceable></command>:
  171. 显示与给定单元 UNIT 关联的日志。</para>
  172. </listitem>
  173. <listitem>
  174. <para><command>journalctl -b[=ID] -r</command>:
  175. 按时间倒序,显示自上次引导以来(或编号为 ID 的引导中)
  176. 的所有日志。</para>
  177. </listitem>
  178. <listitem>
  179. <para><command>journalctl -f</command>: 提供类似
  180. tail -f 的功能(不断将新日志项输出到屏幕)。</para>
  181. </listitem>
  182. </itemizedlist>
  183. </sect2>
  184. <sect2>
  185. <title>持续运行进程</title>
  186. <!--para>Beginning with systemd-230, all user processes are killed when a user
  187. session is ended, even if nohup is used, or the process uses the
  188. <function>daemon()</function> or <function>setsid()</function> functions.
  189. This is a deliberate change from a historically permissive environment to a
  190. more restrictive one. The new behavior may cause issues if you depend on
  191. long running programs (e.g., <command>screen</command> or
  192. <command>tmux</command>) to remain active after ending your user session.
  193. There are three ways to enable lingering processes to remain after a user
  194. session is ended.</para-->
  195. <para>从 systemd 的 230 版本开始,在用户会话结束时,
  196. 所有用户进程都被杀死,即使使用了 nohup 或
  197. <function>daemon()</function> 、<function>setsid</function>
  198. 等函数也不例外。这是开发者有意做出的修改,
  199. 将传统的宽松环境改为更加严格的环境。如果您需要让持续运行的程序
  200. (例如 <command>screen</command> 或 <command>tmux</command>)
  201. 在用户会话结束后保持运行,这项新的行为会导致问题。
  202. 有三种方法使得这类驻留进程在用户会话结束后继续运行:</para>
  203. <itemizedlist>
  204. <listitem>
  205. <para>
  206. <emphasis>为选定的用户启用进程驻留</emphasis>:
  207. <!--Normal users have permission to enable process lingering
  208. with the command <command>loginctl enable-linger</command> for their
  209. own user. System administrators can use the same command with a
  210. <parameter>user</parameter> argument to enable for a user. That user
  211. can then use the <command>systemd-run</command> command to start
  212. long running processes. For example: <command>systemd-run scope
  213. user /usr/bin/screen</command>. If you enable lingering for your
  214. user, the user@.service will remain even after all login sessions are
  215. closed, and will automatically start at system boot. This has the
  216. advantage of explicitly allowing and disallowing processes to run
  217. after the user session has ended, but breaks backwards compatibility
  218. with tools like <command>nohup</command> and utilities that use
  219. <function>deamon()</function>.-->
  220. 普通用户有执行命令 <command>loginctl enable-linger</command>
  221. 启用进程驻留的权限,管理员可以使用带
  222. <parameter>user</parameter> 参数的该命令,为特定用户启用进程驻留。
  223. 在启用进程驻留后,可以使用 <command>systemd-run</command>
  224. 命令启动持续运行的进程。例如,
  225. <command>systemd-run --scope --user /usr/bin/screen</command>。
  226. 如果您为您的用户启用了进程驻留,则 user@.service 将持续运行,
  227. 甚至在所有登录会话关闭后仍然运行,而且会在系统引导时自动启动。
  228. 这种方法的好处是可以显式地允许或禁止进程在用户会话结束后继续运行,
  229. 但却破坏了和 <command>nohup</command> 等工具,和使用
  230. <function>daemon()</function> 函数的工具的兼容性。
  231. </para>
  232. </listitem>
  233. <listitem>
  234. <para>
  235. <emphasis>为整个系统启用进程驻留</emphasis>:
  236. 您可以在 <filename>/etc/logind.conf</filename> 中设定
  237. <parameter>KillUserProcesses=no</parameter> 参数,
  238. 为所有用户全局地启用进程驻留。
  239. 它的好处是允许所有用户继续使用旧方法,但无法进行明确控制。
  240. </para>
  241. </listitem>
  242. <listitem>
  243. <para>
  244. <emphasis>在构建时禁用新功能</emphasis>: 您可以在构建
  245. systemd 时向 <command>mseon</command> 命令添加参数
  246. <parameter>-Ddefault-kill-user-process=no</parameter>,
  247. 使得 systemd 默认启用进程驻留。
  248. 这完全禁用了 systemd 在会话结束时杀死用户进程的功能。
  249. </para>
  250. </listitem>
  251. </itemizedlist>
  252. </sect2>
  253. </sect1>