who command produces no output on WSL2

Solution 1:

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).

Explanation

... 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] [0.0.0.0        ] [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 --system-unit=basic.target
    

    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.