CLOVER🍀

That was when it all began.

systemdのターゲット(SystemVのランレベル相当)を変更する

これは、なにをしたくて書いたもの?

そういえば、systemdになってからSystemVのランレベルに相当するものを変更したことがないなと思いまして。

multi-user.targetなどの言葉には見覚えがあるのですが、これを切り替えたことがない、という話です。

systemdにおけるターゲット

このあたりの話ですね。

Ubuntu Manpage: systemd.target - Target unit configuration

第14章 systemd ターゲットでの作業 Red Hat Enterprise Linux 9 | Red Hat Customer Portal

ターゲットは、systemdのターゲットユニットに関する情報をエンコードしたもので、ユニットのグループ化や起動時の同期ポイントとして
使われます。

また、ターゲットユニットはSystemVにおけるランレベルを置き換えるものです。

Among other things, target units are a more flexible replacement for SysV runlevels in the classic SysV init system. (And for compatibility reasons special target units such as runlevel3.target exist which are used by the SysV runlevel compatibility code in systemd. See systemd.special(7) for details).

Ubuntu Manpage: systemd.target - Target unit configuration

そして、systemdのmanページを見るとsystemd.specialを見るように書かれています。

Ubuntu Manpage: systemd.special - Special systemd units

こちらに、multi-user.targetgraphical.targetなどが登場します。

少し確認しつつ、起動時のターゲットを変更してみましょう。

環境

今回の環境は、こちら。Ubuntu Linux 22.04 LTSです。

$ lsb_release -a
No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 22.04.1 LTS
Release:        22.04
Codename:       jammy


$ uname -srvmpio
Linux 5.15.0-56-generic #62-Ubuntu SMP Tue Nov 22 19:54:14 UTC 2022 x86_64 x86_64 x86_64 GNU/Linux

現在のデフォルトのターゲットを表示する

現在のデフォルトのターゲットを表示するには、systemctl get-defaultで行います。

$ systemctl get-default
graphical.target

存在していれば、/etc/systemd/system/default.targetというファイルを見ることでも確認できます。

/usr/lib/systemd/system/default.targetというファイルもありますが、これはデフォルトのターゲットが明示的に設定されていない場合に
使われるもののようです。

/usr/lib/systemd/system/default.target

#  SPDX-License-Identifier: LGPL-2.1-or-later
#
#  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=Graphical Interface
Documentation=man:systemd.special(7)
Requires=multi-user.target
Wants=display-manager.service
Conflicts=rescue.service rescue.target
After=multi-user.target rescue.service rescue.target display-manager.service
AllowIsolate=yes

このファイルはシンボリックリンクになっています。

$ ll /usr/lib/systemd/system/default.target
lrwxrwxrwx 1 root root 16  9月 10 03:47 /usr/lib/systemd/system/default.target -> graphical.target

現在のターゲットを確認する

現在のターゲットを確認するには、systemctl list-units --type targetを実行します。

$ systemctl list-units --type target --no-pager
  UNIT                  LOAD   ACTIVE SUB    DESCRIPTION
  basic.target          loaded active active Basic System
  cryptsetup.target     loaded active active Local Encrypted Volumes
  getty-pre.target      loaded active active Preparation for Logins
  getty.target          loaded active active Login Prompts
  graphical.target      loaded active active Graphical Interface
  local-fs-pre.target   loaded active active Preparation for Local File Systems
  local-fs.target       loaded active active Local File Systems
  multi-user.target     loaded active active Multi-User System
  network-online.target loaded active active Network is Online
  network-pre.target    loaded active active Preparation for Network
  network.target        loaded active active Network
  nss-lookup.target     loaded active active Host and Network Name Lookups
  paths.target          loaded active active Path Units
  remote-fs-pre.target  loaded active active Preparation for Remote File Systems
  remote-fs.target      loaded active active Remote File Systems
  slices.target         loaded active active Slice Units
  sockets.target        loaded active active Socket Units
  swap.target           loaded active active Swaps
  sysinit.target        loaded active active System Initialization
  time-set.target       loaded active active System Time Set
  timers.target         loaded active active Timer Units
  veritysetup.target    loaded active active Local Verity Protected Volumes

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
22 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

--allをつけると、deadやemptyのものも表示されます。

$ systemctl list-units --type target --all --no-pager
  UNIT                                                                          LOAD      ACTIVE   SUB    DESCRIPTION
  basic.target                                                                  loaded    active   active Basic System
  blockdev@dev-disk-by\x2did-dm\x2duuid\x2dLVM\x2dTodM1ZBUwXudQw5R3kuQniy0DW4q… loaded    inactive dead   Block Device Preparation for /dev/disk/by-id/dm-uuid-LVM-TodM1ZBUwXudQw5R3ku…
  blockdev@dev-disk-by\x2duuid-b5d8cb77\x2dbab2\x2d42e4\x2d8ea1\x2ddf2e76e3a2c… loaded    inactive dead   Block Device Preparation for /dev/disk/by-uuid/b5d8cb77-bab2-42e4-8ea1-df2e7…
  blockdev@dev-loop0.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop0
  blockdev@dev-loop1.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop1
  blockdev@dev-loop2.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop2
  blockdev@dev-loop3.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop3
  blockdev@dev-loop4.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop4
  blockdev@dev-vda2.target                                                      loaded    inactive dead   Block Device Preparation for /dev/vda2
● cloud-init.target                                                             not-found inactive dead   cloud-init.target
  cryptsetup.target                                                             loaded    active   active Local Encrypted Volumes
  emergency.target                                                              loaded    inactive dead   Emergency Mode
  first-boot-complete.target                                                    loaded    inactive dead   First Boot Complete
  getty-pre.target                                                              loaded    active   active Preparation for Logins
  getty.target                                                                  loaded    active   active Login Prompts
  graphical.target                                                              loaded    active   active Graphical Interface
  local-fs-pre.target                                                           loaded    active   active Preparation for Local File Systems
  local-fs.target                                                               loaded    active   active Local File Systems
  multi-user.target                                                             loaded    active   active Multi-User System
  network-online.target                                                         loaded    active   active Network is Online
  network-pre.target                                                            loaded    active   active Preparation for Network
  network.target                                                                loaded    active   active Network
  nss-lookup.target                                                             loaded    active   active Host and Network Name Lookups
  nss-user-lookup.target                                                        loaded    inactive dead   User and Group Name Lookups
  paths.target                                                                  loaded    active   active Path Units
  remote-fs-pre.target                                                          loaded    active   active Preparation for Remote File Systems
  remote-fs.target                                                              loaded    active   active Remote File Systems
  rescue.target                                                                 loaded    inactive dead   Rescue Mode
  shutdown.target                                                               loaded    inactive dead   System Shutdown
  sleep.target                                                                  loaded    inactive dead   Sleep
  slices.target                                                                 loaded    active   active Slice Units
  sockets.target                                                                loaded    active   active Socket Units
  swap.target                                                                   loaded    active   active Swaps
  sysinit.target                                                                loaded    active   active System Initialization
  time-set.target                                                               loaded    active   active System Time Set
  time-sync.target                                                              loaded    inactive dead   System Time Synchronized
  timers.target                                                                 loaded    active   active Timer Units
  umount.target                                                                 loaded    inactive dead   Unmount All Filesystems
  veritysetup.target                                                            loaded    active   active Local Verity Protected Volumes

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
39 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.

ランレベルとターゲットの対応確認

SystemVのランレベルとの対応を確認するには、/lib/systemd/system/runlevel*.targetを確認すればOKです。

$ ll /lib/systemd/system/runlevel*.target
lrwxrwxrwx 1 root root 15  9月 10 03:47 /lib/systemd/system/runlevel0.target -> poweroff.target
lrwxrwxrwx 1 root root 13  9月 10 03:47 /lib/systemd/system/runlevel1.target -> rescue.target
lrwxrwxrwx 1 root root 17  9月 10 03:47 /lib/systemd/system/runlevel2.target -> multi-user.target
lrwxrwxrwx 1 root root 17  9月 10 03:47 /lib/systemd/system/runlevel3.target -> multi-user.target
lrwxrwxrwx 1 root root 17  9月 10 03:47 /lib/systemd/system/runlevel4.target -> multi-user.target
lrwxrwxrwx 1 root root 16  9月 10 03:47 /lib/systemd/system/runlevel5.target -> graphical.target
lrwxrwxrwx 1 root root 13  9月 10 03:47 /lib/systemd/system/runlevel6.target -> reboot.target

現在のセッションのターゲットを変更する

現在のセッションのターゲットを変更するにはsystemctl isolateで行います。

$ sudo systemctl isolate multi-user

これは一時的な変更なので、システムのデフォルト設定は変更されません。

$ systemctl get-default
graphical.target

存在していれば、/etc/systemd/system/default.targetファイルのシンボリックリンク先を確認すれば変更されていないことが確認できるでしょう。

こちらには反映されています。graphical.targetが表示されなくなりました。

$ systemctl list-units --type target
  UNIT                  LOAD   ACTIVE SUB    DESCRIPTION
  basic.target          loaded active active Basic System
  cryptsetup.target     loaded active active Local Encrypted Volumes
  getty-pre.target      loaded active active Preparation for Logins
  getty.target          loaded active active Login Prompts
  local-fs-pre.target   loaded active active Preparation for Local File Systems
  local-fs.target       loaded active active Local File Systems
  multi-user.target     loaded active active Multi-User System
  network-online.target loaded active active Network is Online
  network-pre.target    loaded active active Preparation for Network
  network.target        loaded active active Network
  nss-lookup.target     loaded active active Host and Network Name Lookups
  paths.target          loaded active active Path Units
  remote-fs-pre.target  loaded active active Preparation for Remote File Systems
  remote-fs.target      loaded active active Remote File Systems
  slices.target         loaded active active Slice Units
  sockets.target        loaded active active Socket Units
  swap.target           loaded active active Swaps
  sysinit.target        loaded active active System Initialization
  time-set.target       loaded active active System Time Set
  timers.target         loaded active active Timer Units
  veritysetup.target    loaded active active Local Verity Protected Volumes

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
21 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

--allオプションを付与すると、graphical.targetinactiveに変わり、deadであることが確認できます。

$ systemctl list-units --type target --all --no-pager
  UNIT                                                                          LOAD      ACTIVE   SUB    DESCRIPTION
  basic.target                                                                  loaded    active   active Basic System
  blockdev@dev-disk-by\x2did-dm\x2duuid\x2dLVM\x2dTodM1ZBUwXudQw5R3kuQniy0DW4q… loaded    inactive dead   Block Device Preparation for /dev/disk/by-id/dm-uuid-LVM-TodM1ZBUwXudQw5R3ku…
  blockdev@dev-disk-by\x2duuid-b5d8cb77\x2dbab2\x2d42e4\x2d8ea1\x2ddf2e76e3a2c… loaded    inactive dead   Block Device Preparation for /dev/disk/by-uuid/b5d8cb77-bab2-42e4-8ea1-df2e7…
  blockdev@dev-loop0.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop0
  blockdev@dev-loop1.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop1
  blockdev@dev-loop2.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop2
  blockdev@dev-loop3.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop3
  blockdev@dev-loop4.target                                                     loaded    inactive dead   Block Device Preparation for /dev/loop4
  blockdev@dev-vda2.target                                                      loaded    inactive dead   Block Device Preparation for /dev/vda2
● cloud-init.target                                                             not-found inactive dead   cloud-init.target
  cryptsetup.target                                                             loaded    active   active Local Encrypted Volumes
  emergency.target                                                              loaded    inactive dead   Emergency Mode
  first-boot-complete.target                                                    loaded    inactive dead   First Boot Complete
  getty-pre.target                                                              loaded    active   active Preparation for Logins
  getty.target                                                                  loaded    active   active Login Prompts
  graphical.target                                                              loaded    inactive dead   Graphical Interface
  local-fs-pre.target                                                           loaded    active   active Preparation for Local File Systems
  local-fs.target                                                               loaded    active   active Local File Systems
  multi-user.target                                                             loaded    active   active Multi-User System
  network-online.target                                                         loaded    active   active Network is Online
  network-pre.target                                                            loaded    active   active Preparation for Network
  network.target                                                                loaded    active   active Network
  nss-lookup.target                                                             loaded    active   active Host and Network Name Lookups
  nss-user-lookup.target                                                        loaded    inactive dead   User and Group Name Lookups
  paths.target                                                                  loaded    active   active Path Units
  remote-fs-pre.target                                                          loaded    active   active Preparation for Remote File Systems
  remote-fs.target                                                              loaded    active   active Remote File Systems
  rescue.target                                                                 loaded    inactive dead   Rescue Mode
  shutdown.target                                                               loaded    inactive dead   System Shutdown
  sleep.target                                                                  loaded    inactive dead   Sleep
  slices.target                                                                 loaded    active   active Slice Units
  sockets.target                                                                loaded    active   active Socket Units
  swap.target                                                                   loaded    active   active Swaps
  sysinit.target                                                                loaded    active   active System Initialization
  time-set.target                                                               loaded    active   active System Time Set
  time-sync.target                                                              loaded    inactive dead   System Time Synchronized
  timers.target                                                                 loaded    active   active Timer Units
  umount.target                                                                 loaded    inactive dead   Unmount All Filesystems
  veritysetup.target                                                            loaded    active   active Local Verity Protected Volumes

LOAD   = Reflects whether the unit definition was properly loaded.
ACTIVE = The high-level unit activation state, i.e. generalization of SUB.
SUB    = The low-level unit activation state, values depend on unit type.
39 loaded units listed.
To show all installed unit files use 'systemctl list-unit-files'.

これは一時的な設定変更なので、再起動したりすると元に戻ります。

デフォルトのターゲットを変更する

デフォルトのターゲットを変更するにはsystemctl set-defaultで行います。multi-userターゲットに変更してみましょう。

$ sudo systemctl set-default multi-user
Created symlink /etc/systemd/system/default.target → /lib/systemd/system/multi-user.target.

このコマンドだと、切り替わりましたね。

$ systemctl get-default`
multi-user.target

/etc/systemd/system/default.targetも作成されます。

$ ll /etc/systemd/system/default.target
lrwxrwxrwx 1 root root 37  1月  4 04:00 /etc/systemd/system/default.target -> /lib/systemd/system/multi-user.target

ファイルの中身。

/etc/systemd/system/default.target

#  SPDX-License-Identifier: LGPL-2.1-or-later
#
#  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=Multi-User System
Documentation=man:systemd.special(7)
Requires=basic.target
Conflicts=rescue.service rescue.target
After=basic.target rescue.service rescue.target
AllowIsolate=yes

これはデフォルトの変更になるので、再起動などを行っても元に戻ったりしません。

/usr/lib/systemd/system/default.targetについては、デフォルトのターゲットが設定されていない場合に使われるものなので、
systemctl set-defaultでは変更されません。

$ ll /usr/lib/systemd/system/default.target
lrwxrwxrwx 1 root root 16  9月 10 03:47 /usr/lib/systemd/system/default.target -> graphical.target

まとめ

systemdのターゲットを変更する方法を確認してみました。

ちょっと気になったので調べてみましたが、あまり使う機会はないかもしれません。まあ、覚えておきましょう。