WSL2で動くUbuntu18.04でsystemdを有効にする
WSL2でUbuntuインストール直後はPIDの「1」がMSカスタムの「init」に割り当てられています。
そのため「systemctl」による制御がエラーになります。
「systemctl」を有効にするためには「systemd」をPID1で動かす必要があります。
※Ubuntuのバージョンは18.04です。
■systemctlでエラーとなる
***@***:~$ sudo systemctl status sshd
System has not been booted with systemd as init system (PID 1). Can't operate.
そのためには「genie」を使います。
https://github.com/arkane-systems/genie
このページの「INSTALLATION」に従ってインストールします。
1.daemonize, dbus, policykit-1, systemdのインストール
それぞれ「apt install」を実行します。
2.dotnet-runtime-3.1のインストール
MSの下記ページの「18.04」の章に従ってdotnetをインストールします。
3./etc/apt/sources.list.d/wsl-translinux.listの作成
***@***:~$ sudo vi /etc/apt/sources.list.d/wsl-translinux.list
wsl-translinux.listに下記設定
deb [trusted=yes] https://wsl-translinux.arkane-systems.net/apt/ /
インストール
***@***:~$ sudo apt update
***@***:~$ sudo apt install -y systemd-genie
インストール出来たら「genie -c bash」を実行してsystemdをPID1で動かす。
「ps aux」と叩いて確認。
「systemctl」が有効になりました。
Ubuntu起動時に「genie -c bash」を起動してsshサーバーを起動するように「~/.bashrc」に下記追記します。
if [ "`ps -eo pid,cmd | grep systemd | grep -v grep | sort -n -k 1 | awk 'NR==1 { print $1 }'`" != "1" ]; then
genie -c bash
sudo systemctl start sshd
fi
下記参考