AskOverflow.Dev

AskOverflow.Dev Logo AskOverflow.Dev Logo

AskOverflow.Dev Navigation

  • 主页
  • 系统&网络
  • Ubuntu
  • Unix
  • DBA
  • Computer
  • Coding
  • LangChain

Mobile menu

Close
  • 主页
  • 系统&网络
    • 最新
    • 热门
    • 标签
  • Ubuntu
    • 最新
    • 热门
    • 标签
  • Unix
    • 最新
    • 标签
  • DBA
    • 最新
    • 标签
  • Computer
    • 最新
    • 标签
  • Coding
    • 最新
    • 标签
主页 / ubuntu / 问题 / 659267
Accepted
muru
muru
Asked: 2015-08-09 19:31:49 +0800 CST2015-08-09 19:31:49 +0800 CST 2015-08-09 19:31:49 +0800 CST

如何覆盖或配置 systemd 服务?

  • 772

许多 sysv init 脚本使用相应的文件/etc/default以允许管理员对其进行配置。可以使用.override文件修改 Upstart 作业。既然 systemd 是 Ubuntu 中的默认设置,我该如何覆盖或配置 systemd 单元?

configuration
  • 1 1 个回答
  • 208481 Views

1 个回答

  • Voted
  1. Best Answer
    muru
    2015-08-09T19:31:49+08:002015-08-09T19:31:49+08:00

    systemd单位不需要服从/etc/default. systemd很容易配置,但需要你知道 systemd 单元文件的语法。

    包通常以/lib/systemd/system/. 这些不能编辑。相反,systemd允许您通过在/etc/systemd/system/.

    对于给定的服务foo,包将提供/lib/systemd/system/foo.service。您可以使用 检查其状态systemctl status foo,或使用 查看其日志journalctl -u foo。要覆盖 的定义中的某些内容foo,请执行以下操作:

    sudo systemctl edit foo
    

    这将创建一个/etc/systemd/system以单元命名的目录,并override.conf在该目录中创建一个文件 ( /etc/systemd/system/foo.service.d/override.conf)。.conf您可以使用此文件(或中的其他文件)添加或覆盖设置/etc/systemd/system/foo.service.d/。这也适用于非服务单位——你可以这样做systemctl edit foo.mount,systemctl edit foo.timer等等。

    覆盖命令参数

    以getty服务为例。假设我想让 TTY2 自动登录到我的用户(这是不可取的,但只是一个例子)。TTY2 由getty@tty2服务运行(tty2作为模板的一个实例/lib/systemd/system/getty@service)。为此,我必须修改getty@tty2服务。

    $ systemctl cat getty@tty2
    # /lib/systemd/system/[email protected]
    #  This file is part of systemd.
    #
    #  systemd is free software; you can redistribute it and/or modify it
    #  under the terms of the GNU Lesser General Public License as published by
    #  the Free Software Foundation; either version 2.1 of the License, or
    #  (at your option) any later version.
    
    [Unit]
    Description=Getty on %I
    Documentation=man:agetty(8) man:systemd-getty-generator(8)
    Documentation=http://0pointer.de/blog/projects/serial-console.html
    After=systemd-user-sessions.service plymouth-quit-wait.service
    After=rc-local.service
    
    # If additional gettys are spawned during boot then we should make
    # sure that this is synchronized before getty.target, even though
    # getty.target didn't actually pull it in.
    Before=getty.target
    IgnoreOnIsolate=yes
    
    # On systems without virtual consoles, don't start any getty. Note
    # that serial gettys are covered by [email protected], not this
    # unit.
    ConditionPathExists=/dev/tty0
    
    [Service]
    # the VT is cleared by TTYVTDisallocate
    ExecStart=-/sbin/agetty --noclear %I $TERM
    Type=idle
    Restart=always
    RestartSec=0
    UtmpIdentifier=%I
    TTYPath=/dev/%I
    TTYReset=yes
    TTYVHangup=yes
    TTYVTDisallocate=yes
    KillMode=process
    IgnoreSIGPIPE=no
    SendSIGHUP=yes
    
    # Unset locale for the console getty since the console has problems
    # displaying some internationalized messages.
    Environment=LANG= LANGUAGE= LC_CTYPE= LC_NUMERIC= LC_TIME= LC_COLLATE= LC_MONETARY= LC_MESSAGES= LC_PAPER= LC_NAME= LC_ADDRESS= LC_TELEPHONE= LC_MEASUREMENT= LC_IDENTIFICATION=
    
    [Install]
    WantedBy=getty.target
    DefaultInstance=tty1
    

    特别是,我必须更改当前ExecStart行:

    $ systemctl cat getty@tty2 | grep Exec     
    ExecStart=-/sbin/agetty --noclear %I $TERM
    

    要覆盖它,请执行以下操作:

    sudo systemctl edit getty@tty2
    

    并添加:

    [Service]
    ExecStart=
    ExecStart=-/sbin/agetty -a muru --noclear %I $TERM
    

    注意:

    1. 在再次设置之前我必须明确清除ExecStart它,因为它是一个附加设置,类似于其他列表,如Environment(作为一个整体,而不是每个变量)和EnvironmentFile; 并反对重写RestartSec或之类的设置Type。ExecStart只能为Type=oneshot服务设置多个条目。请注意,像Before、After、Wants等依赖项设置也是列表,但不能使用这种方式清除。您必须为此覆盖/替换整个服务(见下文)。
    2. 我必须使用正确的节标题。在原始文件中,ExecStart在该[Service]部分中,因此我的覆盖也必须放在ExecStart该[Service]部分中。通常,查看实际使用的服务文件systemctl cat会告诉您需要覆盖什么以及它位于哪个部分。

    通常,如果你编辑一个 systemd 单元文件,要让它生效,你需要运行:

    sudo systemctl daemon-reload
    

    但是,systemctl edit它会自动为您执行此操作。

    现在:

    $ systemctl cat getty@tty2 | grep Exec
    ExecStart=-/sbin/agetty --noclear %I $TERM
    ExecStart=
    ExecStart=-/sbin/agetty -a muru --noclear %I $TERM
    
    $ systemctl show getty@tty2 | grep ExecS
    ExecStart={ path=/sbin/agetty ; argv[]=/sbin/agetty -a muru --noclear %I $TERM ; ... }
    

    如果我这样做:

    sudo systemctl restart getty@tty2
    

    然后按CtrlAltF2,很快!我将在该 TTY 上登录我的帐户。

    正如我之前所说,getty@tty2是模板的一个实例。那么,如果我想覆盖该模板的所有实例怎么办?这可以通过编辑模板本身来完成(删除实例标识符 - 在这种情况下tty2):

    systemctl edit getty@
    

    覆盖环境

    文件的一个常见用例/etc/default是设置环境变量。通常,/etc/default是一个 shell 脚本,因此您可以在其中使用 shell 语言构造。但是,对于systemd,情况并非如此。您可以通过两种方式指定环境变量:

    通过文件

    假设您已在文件中设置环境变量:

    $ cat /path/to/some/file
    FOO=bar
    

    然后,您可以添加到覆盖:

    [Service]
    EnvironmentFile=/path/to/some/file
    

    特别是,如果您/etc/default/grub只包含赋值而没有 shell 语法,则可以将它用作EnvironmentFile.

    通过Environment条目

    也可以使用以下覆盖来完成上述操作:

    [Service]
    Environment=FOO=bar
    

    但是,对于多个变量、空格等,这可能会变得棘手。查看我的其他答案之一以获取此类实例的示例。

    编辑的变化

    完全替换现有单元

    如果您想对现有单元进行大量更改,以便有效地完全替换它,您可以这样做:

    systemctl edit --full foo
    

    临时编辑

    在 systemd 文件层次结构中,/run优先于/etc,后者又优先于/lib. 到目前为止所说的一切也适用于 using /run/systemd/systeminstead of /etc/systemd/system。通常/run是一个临时文件系统,其内容在重启时会丢失,所以如果你只想在重启之前覆盖一个单元,你可以这样做:

    systemctl edit --runtime foo
    

    撤消更改

    您可以简单地删除相应的覆盖文件,并systemctl daemon-reload让 systemd 读取更新的单元定义。

    您还可以还原所有更改:

    systemctl revert foo
    

    进一步阅读

    systemd通过这种机制,覆盖单位以及撤消此类更改变得非常容易(通过简单地删除覆盖文件)。这些不是唯一可以修改的设置。

    以下链接会很有用:

    • Arch Wiki 条目在systemd
    • systemd for Administrators,第 IX 部分:关于 /etc/sysconfig 和 /etc/default(由 systemd 的首席开发人员 Lennart Poettering 撰写)
    • systemd联机帮助页,特别是systemd.unit和的联机帮助页systemd.service
    • Systemd 上针对 Upstart 用户的Ubuntu Wiki 条目
    • 319

相关问题

  • 声音,在多个程序之间停止

  • 如何为公共计算机配置 Ubuntu?

  • 如何设置默认启用网络?

  • 如何在键盘上映射未映射的键?[关闭]

  • 保持多个工作站同步

Sidebar

Stats

  • 问题 205573
  • 回答 270741
  • 最佳答案 135370
  • 用户 68524
  • 热门
  • 回答
  • Marko Smith

    如何运行 .sh 脚本?

    • 16 个回答
  • Marko Smith

    如何安装 .tar.gz(或 .tar.bz2)文件?

    • 14 个回答
  • Marko Smith

    如何列出所有已安装的软件包

    • 24 个回答
  • Marko Smith

    无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗?

    • 25 个回答
  • Martin Hope
    Flimm 如何在没有 sudo 的情况下使用 docker? 2014-06-07 00:17:43 +0800 CST
  • Martin Hope
    Ivan 如何列出所有已安装的软件包 2010-12-17 18:08:49 +0800 CST
  • Martin Hope
    La Ode Adam Saputra 无法锁定管理目录 (/var/lib/dpkg/) 是另一个进程在使用它吗? 2010-11-30 18:12:48 +0800 CST
  • Martin Hope
    David Barry 如何从命令行确定目录(文件夹)的总大小? 2010-08-06 10:20:23 +0800 CST
  • Martin Hope
    jfoucher “以下软件包已被保留:”为什么以及如何解决? 2010-08-01 13:59:22 +0800 CST
  • Martin Hope
    David Ashford 如何删除 PPA? 2010-07-30 01:09:42 +0800 CST

热门标签

10.10 10.04 gnome networking server command-line package-management software-recommendation sound xorg

Explore

  • 主页
  • 问题
    • 最新
    • 热门
  • 标签
  • 帮助

Footer

AskOverflow.Dev

关于我们

  • 关于我们
  • 联系我们

Legal Stuff

  • Privacy Policy

Language

  • Pt
  • Server
  • Unix

© 2023 AskOverflow.DEV All Rights Reserve