はじめに

WSL2でsystemdが利用できるらしいので有効にした時のメモ
https://learn.microsoft.com/ja-jp/windows/wsl/systemd

環境

1
2
3
Windows 11 Pro
WSL2 Ubuntu 24.04 LTS
WSL バージョン: 2.2.4.0
1
wsl.exe --version
1
2
3
4
5
6
7
WSL バージョン: 2.2.4.0
カーネル バージョン: 5.15.153.1-2
WSLg バージョン: 1.0.61
MSRDC バージョン: 1.2.5326
Direct3D バージョン: 1.611.1-81528511
DXCore バージョン: 10.0.26091.1-240325-1447.ge-release
Windows バージョン: 10.0.22631.4169

要件

  • WSL バージョンが 0.67.6 以降であること

手順

  1. WSL2の利用しているディストリビューション(ここではUbuntu24.04 LTS)を開く。
  2. /etc/wsl.confを開く
1
sudo vim /etc/wsl.conf
  1. wsl.conf を開き、下記を追記する。
1
2
[boot]
systemd=true
  1. wslをシャットダウンする
1
wsl.exe --shutdown

※シャットダウン後、自動でWSLが起動するので少し待つ。

  1. WSL起動後、ディストリビューションを開き、下記コマンドでsystemdのサービスを確認する。
1
systemctl list-unit-files --type=service
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
 ~  systemctl list-unit-files --type=service                                                            1 х │ 06:28:58
UNIT FILE                                    STATE           PRESET
apparmor.service                             enabled         enabled
apport-autoreport.service                    static          -
[email protected]                static          -
[email protected]                      static          -
apport.service                               enabled         enabled
apt-daily-upgrade.service                    static          -
apt-daily.service                            static          -
apt-news.service                             static          -
[email protected]                              alias           -
cloud-config.service                         enabled         enabled
cloud-final.service                          enabled         enabled
cloud-init-hotplugd.service                  static          -
cloud-init-local.service                     enabled         enabled
cloud-init.service                           enabled         enabled
console-getty.service                        enabled-runtime disabled
console-setup.service                        enabled         enabled
[email protected]                     static          -
cron.service                                 enabled         enabled
cryptdisks-early.service                     masked          enabled
cryptdisks.service                           masked          enabled
dbus-org.freedesktop.hostname1.service       alias           -
dbus-org.freedesktop.locale1.service         alias           -
dbus-org.freedesktop.login1.service          alias           -
dbus-org.freedesktop.resolve1.service        alias           -
dbus-org.freedesktop.timedate1.service       alias           -
dbus-org.freedesktop.timesync1.service       alias           -
dbus.service                                 static          -
...

参考

おわりに

systemdをWSL2で利用したかったので有効にしてみた。
systemd-timerとかも利用できるらしいので、これで定期実行ははかどりそう。