WSL: `systemd` doesn't create user (d)bus

Version

Windows version: 10.0.22621.521

WSL Version

  • WSL 2
  • WSL 1

Kernel Version

5.15.62.1-microsoft-standard-WSL2

Distro Version

ArchWSL

Other Software

  • systemd 251 (251.4-1-arch)
  • D-Bus Message Bus Daemon 1.14.0

Repro Steps

  1. Upgrade WSL to 0.67.6.0
  2. Enable systemd.
  3. sudo loginctl enable-linger <username> [1]
  4. wsl --shutdown
  5. Open WSL

Expected Behavior

User systemd service should launched.

Actual Behavior

User systemd service is not launched. Checked via below commands. Neither succeeded.

❯ systemctl list-units --type=service --user
Failed to connect to bus: No such file or directory

❯ systemctl status --user
Failed to connect to bus: No such file or directory

Diagnostic Logs

I noticed that now WSL set below variables correctly.

❯ echo $XDG_RUNTIME_DIR\n$DBUS_SESSION_BUS_ADDRESS
/run/user/1000/
unix:path=/run/user/1000/bus

But then I suppose that this is due to init now handled by systemd, which is the normal behavior. However, the user (d)bus is not present in the above directory.

❯ exa -l /run/user/1000/
drwx------ - mangkoran 22 Sep 05:30 dbus-1
|rw------- 0 mangkoran 22 Sep 05:31 fish_universal_variables.notifier
srwx------ 0 mangkoran 22 Sep 05:31 lf.mangkoran.sock
drwx------ - mangkoran 22 Sep 05:30 pulse
srwxrwxrwx 0 mangkoran 22 Sep 05:30 wayland-0
.rw-rw---- 0 mangkoran 22 Sep 05:30 wayland-0.lock

About this issue

  • Original URL
  • State: closed
  • Created 4 years ago
  • Reactions: 22
  • Comments: 29

Most upvoted comments

I solved it by running sudo systemctl restart user@1000. After this command, bus and systemd appear again in the /run/user/1000 directory.

This is an artifact of how WSL creates shells/commands when you run them; i.e., just spawning them directly.

It doesn’t go through the normal Linux login process (or variants of same) and thus doesn’t invoke PAM, and it’s the systemd PAM module that creates a login session and spawns a user systemd (and thus session dbus) for it.

(You can see this if you run loginctl, which will tell you there are no established login sessions.)

I got around this in genie by installing systemd-machined, which then let me use wsl sudo machinectl shell username@.host to create a proper login session. This same workaround does the same under the Microsoft systemd implementation, and produces the expected user systemd and session dbus.

Hopefully in the future the WSL team can incorporate the needed PAM-triggering functionality directly into wsl.exe.

Based on the theory that it’s a race during startup, starting wsl as root before running my own shell fixes it:

wsl --user root true
wsl

Really need this to run systemctl --user commands still. I just ran into this today. Logging in twice doesn’t seem like a reasonable solution to me.

Still an issue in 0.68.2, although I note that WSL now automounts the /mnt/wslg/runtime-dir at /run/user/1000, which saves a peck of trouble.

Edited: unless something you do does start a systemd user session, at which point user-runtime-dir@.service blows the contents of that folder and thus /mnt/wslg/runtime-dir away. That’s problematic.

wsl.exe -u root --shell-type login -- login -f <username> works for me (Fedora37)

UPD:

wsl --shutdown
wsl -u root -- su <username>
ls -al /run/user/1000

total 0
drwx------ 4 username username 120 Nov 30 15:57 .
drwxr-xr-x 4 root     root      80 Nov 30 15:57 ..
srw-rw-rw- 1 username username   0 Nov 30 15:57 bus
srw-rw-rw- 1 username username   0 Nov 30 15:57 pipewire-0
drwxr-xr-x 2 username username  60 Nov 30 15:57 pulse
drwxr-xr-x 5 username username 140 Nov 30 15:57 systemd

but when start it normally:

wsl --shutdown
wsl -u <username>
ls -al /run/user/1000

total 0
drwx------ 4 username username 120 Nov 30 16:00 .
drwxr-xr-x 3 root     root      60 Nov 30 16:00 ..
drwx------ 3 username username  60 Nov 30 16:00 dbus-1
drwx------ 2 username username  80 Nov 30 16:00 pulse
srwxrwxrwx 1 username username   0 Nov 30 16:00 wayland-0
-rw-rw---- 1 username username   0 Nov 30 16:00 wayland-0.lock

I could make the user session partially work with native systemd support by providing /bin/login binary (it can be just a symlink to the actual login binary). It’s launched by WSL only once, but it keeps the session running even without linger enabled.

It gives me running systemd user units and it makes systemctl --user commands usable. Note it’s still not a complete solution, see #9213 - the PAM is not invoked for the actual shell and there’re some hardcoded enviroment variables set probably by the WSL /init:

XDG_RUNTIME_DIR=/run/user/1000/
DBUS_SESSION_BUS_ADDRESS=unix:path=/run/user/1000/bus
$ strings /init | grep /run/user
/run/user/%d/
unix:path=/run/user/%d/bus

So, YMMV, but it works at least on my system:

>wsl --version
WSL version: 1.0.0.0
Kernel version: 5.15.74.2
WSLg version: 1.0.47
MSRDC version: 1.2.3575
Direct3D version: 1.606.4
DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp
Windows version: 10.0.19044.2311
NixOS version: 22.05.20221125
Systemd version: 250 (250.8)

/etc/wsl.conf

[boot]
systemd=true

[automount]
enabled=true
mountFsTab=false
options=metadata,uid=1000,gid=100
root=/mnt/

[interop]
appendWindowsPath=false
enabled=true

[network]
generateHosts=true
generateResolvConf=true
hostname=nixos

[user]
default=nixos

Hi all. I just revisited this issue and I could confirm that the issue seems already fixed. I use freshly-installed Arch Linux WSL2 with a bit of additional config for systemd.

19:54:56 ❯ systemctl list-units --type=service --user
  UNIT                           LOAD   ACTIVE SUB     DESCRIPTION
  ssh-agent.service              loaded active running OpenSSH key agent
  systemd-tmpfiles-setup.service loaded active exited  Create User Files and Directories

Legend: 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.

2 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.

19:54:58 ❯ set -S XDG_RUNTIME_DIR
$XDG_RUNTIME_DIR: set in global scope, exported, with 1 elements
$XDG_RUNTIME_DIR[1]: |/run/user/1000/|
$XDG_RUNTIME_DIR: originally inherited as |/run/user/1000/|

19:55:05 ❯ eza -la $XDG_RUNTIME_DIR
srw-rw-rw- - mangkoran  2 Jun 08:12 bus
drwxr-xr-x - mangkoran  2 Jun 08:12 dbus-1
drwx------ - mangkoran  2 Jun 08:12 gnupg
drwxr-xr-x - mangkoran  2 Jun 08:12 podman
drwxr-xr-x - mangkoran  2 Jun 08:12 pulse
srw------- - mangkoran  2 Jun 08:12 ssh-agent.socket
drwxr-xr-x - mangkoran  2 Jun 08:12 systemd
lrwxrwxrwx - root       2 Jun 08:12 wayland-0 -> /mnt/wslg/runtime-dir/wayland-0
lrwxrwxrwx - root       2 Jun 08:12 wayland-0.lock -> /mnt/wslg/runtime-dir/wayland-0.lock

Versions

WSL version: 2.5.7.0
Kernel version: 6.6.87.1-1
WSLg version: 1.0.66
MSRDC version: 1.2.6074
Direct3D version: 1.611.1-81528511
DXCore version: 10.0.26100.1-240331-1435.ge-release
Windows version: 10.0.19044.5854
WSL distro: Arch Linux

For anyone that happens to experience the same issue originally reported above, please kindly update your WSL to 2.5.7 and apply config to fix systemd. If you still experience the issue, it’s better to open a new issue ticket and fill the relevant details as this issue was reported with WSL 0.67.6 which is almost 3 years old.

For the meantime, I will close this issue as I couldn’t reproduce this anymore. Thank you!

https://github.com/microsoft/WSL/issues/8842#issuecomment-1498310224

I solved it by running sudo systemctl restart user@1000. After this command, bus and systemd appear again in the /run/user/1000 directory.

This solved it for me! Thank you!

@nithin-mk

Try this solution:

[10569:0827/004757.735915:ERROR:bus.cc(407)] Failed to connect to the bus: Failed to connect to socket /run/user/1000/bus: No such file or directory

can be fixed by adding these lines to ~/.bashrc

if [ -z "$DBUS_SESSION_BUS_ADDRESS" ]; then
    dbus-daemon --session --address=unix:path=/run/user/1000/bus --fork
    export DBUS_SESSION_BUS_ADDRESS="unix:path=/run/user/1000/bus"
fi

The issue is that when running RStudio, it expects the socket to be located at a fixed path in /run/user/1000/bus, which doesn’t work in WSL2 where the socket is instead created as an abstract socket and is found under /tmp

dbus-launch --sh-syntax --exit-with-session
DBUS_SESSION_BUS_ADDRESS='unix:abstract=**/tmp/dbuslADZpPMOKJ,guid=55593467f737024f5f2ed7c566cd08fd**';
export DBUS_SESSION_BUS_ADDRESS;
DBUS_SESSION_BUS_PID=11021;
DBUS_SESSION_BUS_WINDOWID=6291457;

I get the same error when I tried to start a Homebrew service in Ubuntu 22.04 in WSL 2.

❯ brew services start minio --debug
/home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/brew.rb (Formulary::FromAPILoader): loading minio
/usr/bin/env /home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/shims/shared/git --version
==> /home/linuxbrew/.linuxbrew/bin/systemctl --user status homebrew.minio

Failed to connect to bus: No such file or directory
Error: Failure while executing; `/home/linuxbrew/.linuxbrew/bin/systemctl --user daemon-reload` exited with 1.
/home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/extend/kernel.rb:263:in `safe_system'
/home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-services/lib/service/services_cli.rb:332:in `install_service_file'
/home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-services/lib/service/services_cli.rb:112:in `block in start'
/home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-services/lib/service/services_cli.rb:97:in `each'
/home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-services/lib/service/services_cli.rb:97:in `start'
/home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-services/lib/service/commands/start.rb:12:in `run'
/home/linuxbrew/.linuxbrew/Homebrew/Library/Taps/homebrew/homebrew-services/cmd/services.rb:139:in `run'
/home/linuxbrew/.linuxbrew/Homebrew/Library/Homebrew/brew.rb:92:in `<main>'

What’s the solution?

I have had success working around the wsl pam_env and systemctl --user commands issue by changing the wsl startup command parameters to launch a shell through sudo i.e.

wsl -u root sudo -u username zsh

whether or not this will help op with `loginctl enable-linger username’ I can not say for sure.

What is the hack you are describing: “do is ensure that no WSL session is started (it wait-loops) before systemd is ready for it.” ?

I had the problem starting vscode inside wsl2 (Rocky Linux release 9.1 (Blue Onyx)) requiring /run/user/1000, but that one was missing

I do have the /mnt/wsl/run/user/1000, but I do not have /run/user/1000

sudo systemctl restart user@1000 in .bash_profile did solve my issue, however I want to understand what is going wrong.

cat /etc/wsl.conf
[user]
default=sven

[network]
generateResolvConf = false

[boot]
systemd=true

Hopefully in the future the WSL team can incorporate the needed PAM-triggering functionality directly into wsl.exe.

I think a better way is to give up /init and let systemd take over everything in the VM. Because by using systemd, most of /init’s functionality is redundant. MS engineers just need to implement a few systemd services to be responsible for host interaction things such as sync and interop.

Thank you for your insight. Now that we know what are the missing “step” from WSL invocation.

I do however see my user from loginctl 🤔

❯ loginctl
SESSION  UID USER      SEAT TTY
     c1 1000 mangkoran      pts/2

1 sessions listed.

Is it because I run sudo loginctl enable-linger <username> before?