who command produces no output on WSL2

it flag

I am using Ubuntu 20.04 in the Windows subsystem for Linux on Windows 10. When I type the who command I get no output:

renniej@ratitch:~$ whoami
renniej@ratitch:~$ who

I have tried various things like sudo who and who -a but still I get no output. who --version gives:

who (GNU coreutils) 8.30
Copyright (C) 2018 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Joseph Arceneaux, David MacKenzie, and Michael Stone.

Am I doing something silly here? Or is this a feature of WSL2?

vn flag

Short answer

Nothing silly. There's a Github issue related to the root cause. While the WSL team originally tagged it "by design" and "feature", there was activity on it last year that indicates that it is "high value low cost."

Then again, more recently it's noted that it was created 5 years ago, and has no "thumbs up" from other users wanting it fixed. My guess is that this won't be changed until and unless WSL adopts a more Systemd-like approach to startup (see below for details).


... or more than you probably ever wanted to know about who

There are two reasons why who isn't showing any results:

  • First, the obvious -- who is designed to show (quoting from the man page), "who is logged in". When you start WSL, it doesn't actually send your user through the login password, which is the reason you don't get asked for a password.

  • Second, WSL runs its own /init process as PID1 at startup, which does the "magic" of setting up things like:

    • the Linux network being able to piggyback on the Windows network
    • the auto-mounting of Windows drives
    • the interop that allows Windows .exe's to run
    • and more ...

    A "normal" Linux system, on the other hand, starts up with Systemd or SysVInit (or a smattering of other init systems over the years). The init system is responsible for establishing the runlevel, among other things. And along with that (I'm just learning this myself from that Github issue), the /var/run/utmp construct, which is what tracks who is using the system.

There are a few ways that you could "force" who to work:

  • The first is covered in that Github thread. First, hack a /var/run/utmp with something like:

    sudo bash -c "echo '[1] [00049] [~~  ] [runlevel] [~           ] [4.4.0-17115-Micoroso] [        ] [Wed Feb 28 13:27:14 2018 STD]' | utmpdump -r > /var/run/utmp"

    Then, force a "login" with sudo login -f $USER. You can then see your user logged in using who. If you were to login again via ssh (you'd need to set it up first), then that login would also appear.

  • Second, you could start Systemd in its own PID namespace:

    sudo -b unshare --pid --fork --mount-proc /lib/systemd/systemd

    Wait a few seconds for Systemd to start up, and it will initialize /var/run/utmp. Technically at this point you can sudo login -f $USER and see who.

    Note that Systemd isn't fully usable without additional effort (beyond the scope of this answer), and that you must terminate the Ubuntu WSL instance after doing this in order to return to a stable state. Exit WSL and then wsl --terminate <distro> (where <distro> is likely ubuntu). Once you start back up, everything will be back to normal.


Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.