4

You can check which version is each installed distribution with the command,

wsl -l -v

as this superuser question states.

But is it possible to know the WSL version (1 or 2) from inside the Linux installation?

Is there any reliable mechanism to check the version?

What I found?

Environment and "interop"

The only difference in environment variables, is that WSL 2 has one named WSL_INTEROP that version 1 has not.

That variable points to a path special file.

Kernel version

This reddit post reply points to use the following command,

uname -r | tr '[:upper:]' '[:lower:]'

WSL 1 output:

4.4.0-19041-microsoft

WSL 2 output:

5.4.72-microsoft-standard-wsl2

Could this be an option except the replies given,

Probably better not to go with a kernel check as you can build and install your own kernel, and some people would strip that out.

Although this askubuntu accepted answer states

If the kernel version => 4.19, it's WSL Version 2.

Any other options/ideas?

Addendum: github/WSL repo has the issue #4555.

2 Answers 2

3

Running WSL within WSL will work:

/mnt/c/Windows/System32/wsl.exe -l -v

The output isn't ascii so you could convert then match on the env var WSL_DISTRO_NAME:

/mnt/c/Windows/System32/wsl.exe -l -v | iconv -f unicode | dos2unix |grep $WSL_DISTRO_NAME | awk '{print $NF}'

'

Your installation may need to do some other character conversion.

Edit

If running multiple distributions of similar names, using egrep will be more reliable:

/mnt/c/Windows/System32/wsl.exe -l -v | iconv -f unicode | dos2unix |egrep "\b$WSL_DISTRO_NAME\s+Running" | awk '{print $NF}'
6
  • The could not say anything related to WSL version 1 or 2, so does not help.
    – lucasvc
    Commented Feb 22, 2021 at 11:29
  • 1
    I misread question
    – DuncG
    Commented Feb 22, 2021 at 11:44
  • 1
    I've replaced the answer, but am not able to test fully as I've only one distro installed.
    – DuncG
    Commented Feb 22, 2021 at 12:11
  • Good catch! Inception style :P
    – lucasvc
    Commented Feb 22, 2021 at 17:13
  • 1
    Changing the grep to egrep "\b$WSL_DISTRO_NAME\s+Running" should help fix that so that similarly named distributions are filtered
    – DuncG
    Commented Feb 25, 2021 at 9:12
5

I think I prefer @DuncG's answer. I think the only way it could fail would be if Interop was disabled in /etc/wsl.conf. As long as you are confident that interop is always turned on, that seems like the way to go.

But I'll add that there are quite a few differences between WSL1 and WSL2 that could be used as classification.


Assuming you are using automounting of the Windows' drives:

  • mount | grep "/mnt/c" | grep -q drvfs returns success for WSL1 and fails on WSL2
  • mount | grep "/mnt/c" | grep -q 9p returns success for WSL2 and fails on WSL1

Also, lscpu, but this command is missing from Alpine by default (available on Ubuntu and Kali, for sure, and I would assume on Debian):

  • lscpu | grep -q "Hypervisor vendor:.*Microsoft" will succeed on WSL2 but fail on WSL1, since only WSL2 runs under Hyper-V.

I'm not 100% sure on this one, but in every case I tested (Ubuntu 20.04 on WSL1 and WSL2, Kali on WSL1 and WSL2, and Alpine on WSL1 and WSL2):

  • /proc/cmdline on WSL1 is BOOT_IMAGE=/kernel init=/init
  • /proc/cmdline on WSL2 is initrd=\initrd.img panic=-1 pty.legacy_count=0 nr_cpus=16 (update note: recent WSL2 releases with Systemd support change this slightly. And, of course, using .wslconfig to set kernel cmdline arguments will change this as well. However, the examples below still work in either of those cases.)

So:

  • grep -q "^BOOT_IMAGE" /proc/cmdline returns success on WSL1 but error on WSL2
  • grep -q "^initrd" /proc/cmdline returns success on WSL2 but error on WSL1

I'm sure there are others, but these are some possibilities if you ever need them.

2
  • Wow! Nice investigation!
    – lucasvc
    Commented Feb 24, 2021 at 7:04
  • 2
    Really nice work. Probably we can create a shell using this information. However, I think there are many border cases to consider. For instance, you may change the /mnt/c mount point in the wsl.conf. Probably the hypervisor signature may appear if you run a heavy-weight virtual machine too. I think a complete solution will require a combination of checks.
    – Jaime
    Commented Feb 27, 2021 at 13:10

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .