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
- Upgrade WSL to 0.67.6.0
- Enable systemd.
sudo loginctl enable-linger <username>[1]wsl --shutdown- 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
I solved it by running
sudo systemctl restart user@1000. After this command,busandsystemdappear again in the/run/user/1000directory.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:
Really need this to run
systemctl --usercommands 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.
works for me (Fedora37)wsl.exe -u root --shell-type login -- login -f <username>UPD:
but when start it normally:
I could make the user session partially work with native systemd support by providing
/bin/loginbinary (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 --usercommands 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:So, YMMV, but it works at least on my system:
/etc/wsl.conf
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.
Versions
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
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 directorycan be fixed by adding these lines to
~/.bashrcThe 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
I get the same error when I tried to start a Homebrew service in Ubuntu 22.04 in WSL 2.
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 zshwhether 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@1000in .bash_profile did solve my issue, however I want to understand what is going wrong.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🤔Is it because I run
sudo loginctl enable-linger <username>before?