System and tool description

  • Ubuntu 16.05 LTS
  • Windows Terminal
  • Ps: powershell
  • WSL: Windows subsystem Linux

The problem

When doing the user monitoring experiment of Linux operating system, I found that there was no response when I entered the who command under WSL. The title was displayed when I entered the command, but the login user name was not displayed. In this case, I would not be able to get the user name ($1) from the User list to implement the user login monitoring experiment. The simple and crude solution is to escape the problem and experiment directly with the virtual machine Linux without any bugs.

Haha, but of course I hope to solve the problem as soon as possible

why

The cause of this issue is clearly not a simple command issue, I have looked up a lot of information, and it seems to be an internal issue with the launch of WSL by Microsoft. An empty (0 bytes) /var/run/utmp file should be created by /init. One solution here is to establish runlevels. (But I don’t really understand how it works… (ㄒ ㄒ o))

The solution

1 Start Windows Powershell and log in to the WSL

In PS, you can enter Linux directly by typing WSL (or bash, for the same reason), and then typing who/w gets no response at all (as expected).

2 Establish a run level

The command is as follows:

Sudo bash -c "echo '[1] [00053] [~~] [runlevel] [~] [5.4.91-Microsoft-standard-wsl2] [0.0.0.0] sudo bash -c "echo '[1] [00053] [~~] [runlevel] [~] [5.4.91-Microsoft-standard-wsl2] [0.0.0.0] [the 2021-04-05 T23:10:15, 040218 + 00:00] '| utmpdump - r > / var/run/the utmp "2 > / dev/null

3 exit WSL and return to PS

The command is as follows:

exit

4 Log in to the WSL from PS

wsl sudo login -f <username>

5 View the runlevel

/sbin/runlevel

6 Test the who command

w who whoami

interface

Users log in to monitor experimental tests

The successful test indicates that the user name in who has been successfully obtained in the shell programThe $1Item!!!

Later, I realized that there is another way for me to quickly complete the experimental task of shell programming user monitoring, which is to log in to your remote Linux server for operation, as shown in the picture below. Perfect ⭐