How to tell in Linux under WSL if using WSL1 vs WSL2 ?
Documentation missing instructions in how to tell within Linux under WSL whether using WSL1 or WSL2.
Here is a helpful doc - https://docs.microsoft.com/en-us/windows/wsl/wsl2-install
while setting a distro to be backed by WSL 2, I would run the following command in Powershell:
wsl -s
You can find your Distro by running: wsl -l
You can find your Distro by running: wsl -l
Yes, but there is no particularly good way right now to know your own distribution name once you are inside. There was another ask for that recently #4479.
Probably (possibly?) one way is to run a regex on /proc/version. So WSL1 looks like:
Linux version 4.4.0-18985-Microsoft ([email protected]) (gcc version 5.4.0 (GCC) ) #1-Microsoft Fri Sep 13 14:26:00 PST 2019
While WSL2:
Linux version 4.19.67-microsoft-standard (oe-user@oe-host) (gcc version 8.2.0 (GCC)) #1 SMP Sun Aug 18 13:37:54 UTC 2019
If it were me I wouldn't make a hard bet on the WSL2 string being set in concrete, but conversely, I can't imagine the WSL1 format changing gratuitously (not without grumbling, anyway). Matching that "gcc version n.x.x" and testing for n < 8 probably pretty future proof. There are other heuristics that would work. Calling dmesg and looking for a stable string like "Hypervisor" (or pick another).
Here is a sample C program on how /init takes care of WSL1 vs WSL2:
#include <stdio.h>
#include <string.h>
#include <sys/utsname.h>
int main(void)
{
struct utsname buf;
memset(&buf, 0, sizeof buf);
int ret = uname(&buf);
if (ret == 0)
{
if (strstr(buf.release, "Microsoft"))
printf("WSL1\n");
else
printf("WSL2\n");
}
else
printf("uname error\n");
}
+1 for code always. But implied in my post was maybe better not to rely on oe-user@oe-host never ever changing. That strstr() is leaning pretty hard on a capital letter 'M'. [The string "icrosoft" matches both.]
There may be hundreds of ad-hoc way to do it. For example, only WSL2 has $WSL_INTEROP, /proc/config.gz, /dev/vsock, /dev/sda, tty shows /dev/pts etc.
Money ➡️ mouth:
// g++ -std=c++17 wslver.cpp -o wslver
#include <fstream>
#include <iostream>
#include <array>
#include <string>
#include <charconv>
#include <regex>
using namespace std::string_literals;
int main(int argc, const char *argv[])
{
std::ifstream ifs;
ifs.open("/proc/version");
constexpr size_t bufsz = 128;
std::array<char, bufsz> buf;
ifs.getline(buf.data(), bufsz, '\n');
std::string proc_ver(buf.data(), buf.size());
std::regex rx("Linux version [0-9]+\\.[0-9]+\\.[0-9]+.*[Mm]icrosoft.*gcc version ([0-9]+)\\.[0-9]+\\.[0-9]+");
std::smatch match;
auto gcc_major = (std::regex_search(proc_ver, match, rx) && match.size() == 2) ? match[1] : "5"s;
int maj = 0;
std::from_chars(gcc_major.data(), gcc_major.data() + gcc_major.size(), maj);
std::cout << ((maj < 8) ? "1\n" : "2\n");
return 0;
}
detect wsl1:)
uname -r | grep Microsoft > /dev/null && echo "WSL1"
[ $(grep -oE 'gcc version ([0-9]+)' /proc/version | awk '{print $3}') -gt 5 ] && \
echo "WSL2" || echo "WSL1"
Update (Windows 10 version 2004):
I'm brand new to WSL2, however...
uname -rsaysmicrosoft-standardin it, so all checks for the word[mM]icrosoftwould fail- (opinion) The gcc method seems arbitrary, assuming WSL1 never changes to a new gcc
- (opinion)
WSL_INTEROPstill works, and seems like the best test for now, but that's just my opinion
WSL2 has the directory /run/WSL/ and not WSL1.
I think that perhaps a belt & braces approach is needed. We have don't have any control over WSL1 getting a similar folder added. Likewise, whether 'Microsoft' has upper or lower case.
Using [[ -d "/run/WSL" ]] && echo "WSL2 present" is nice and easy, I acknowledge. But IMO a better method is to test for the underlying hyper-v running system that WSL2 uses.
Try lshw | grep smp for instance. WSL1 shows 'smp' while WSL2 will show both smp & vsyscall32.
We can write something like WSL2_Flag="$(lshw | grep vsyscall32) ; if [[ $WSL2_Flag ]] ; then echo "WSL2 present" ; fi
Or alternatively, we can look in '/proc'. We can't guarantee 'lshw' to be present in every Linux distro, although it usually is:
In WSL1 cat /proc/interrupts is empty whereas with WSL2 it is positively active, especially the line for HVS Hyper -V !
eg. WSL2_Flag="$(cat /proc/interrupts ) ; if [[ $WSL2_Flag ]] ; then echo "WSL2 present" ; fi
What about the environment variable WSL_DISTRO_NAME? [[ -n "$WSL_DISTRO_NAME" ]]. Is that a suitable & cheap way to detect WSL 1 or 2?
No. WSL_DISTRO_NAME variable is set by /init binary blob for both WSL1 and WSL2.
@Biswa96 I asked if it's suitable for WSL1 OR WSL 2, not WSL 2 vs WSL 1 so it sounds like the answer to my question is actually yes?
Yes but not retroactively.
@therealkenc is the way you described (distinguish between WSL 1 and WSL 2 using the GCC version) going to be guaranteed stable, even in future versions of WSL 1?
Which is the guaranteed way to distinguish between these two and nōn-WSL Linux then? The presence of the string icrosoft?
Someone suggested checking the root filesystem type, which for WSL 1 is wslfs, but that won’t help for WSL 2. Any other filesystem can be umounted… and some people have trouble with custom kernels and the detection mechanism you provided (especially in the “WSL vs Linux” case).
Anything that needs superuser privilegues (lshw…) is also out, for hopefully obvious reasons.
No guarantees in life. Although given that the unspoken textual API has now irretrievably leaked into userspace, yes, I would treat regex [Mm] difference as pretty future proof.
That said, users can (and do) compile their own kernels in WSL2 and then misname the their kernel, or compile with clang, so you can't count on the kernel string in WSL2 to be anything in particular. Fortunately (notwithstanding the env pollution) recent WSL has the WSL_DISTRO_NAME and WSL_INTEROP environment variables. WSL_INTEROP is only present in WSL2. Using them is not backwards compatible with older WSL though, and they won't EOL for years.
Possible approach:
- Test for existence
WSL_DISTRO_NAME. If it exists, test forWSL_INTEROP. Yes, WSL2; no, WSL1. - If
WSL_DISTRO_NAMEdoes not exist, fall back on the version string regex.
This might do. I was thinking of simplifying this a bit:
- if
/iswslfs, then it’s WSL 1, else it’s either Linux/Android or WSL 2 - if
WSL_INTEROPor evenWSL_DISTRO_NAMEis set or the version containsicrosoftit’s WSL 2, else Linux/Android
Doing this without /proc (which is not guaranteed to be mounted) is more tricky. After statfs("/", &buf), buf.f_type is 0x53464846 on my test setup. Is this true for all WSL 1 systems?
The WSL 2 detection is even more fragile. Addition of an API from the start would have helped :/
For version detection, uname(2) exists, so this can also be done without procfs. The WSL 2 caveats apply :/
Use uname(2) if you can't depend on /proc. The gcc test was belt+suspenders overkill, and given existence possibility clang probably does more harm than good. strstr(buf.release, "Microsoft") means WSL1. Else Linux/Android/WSL2. For your purposes WSL2 is Linux anyway.
therealkenc dixit:
In
uname(2)if you can't depend on/proc. Thegcctest was belt+suspenders overkill, and given existence possibilityclangprobably does more harm than good.strstr(buf.release, "Microsoft")means WSL1. Else Linux/Android/WSL2. For [your
OK, great, that will work.
purposes](https://github.com/tarent/ECN-Bits/blob/18320ef9e0b0a674e09668392224770d7ba6a7be/c/lib/tc.c#L53) WSL2 is Linux anyway.
Oh, kudos for finding this one. For that purpose, WSL 2 probably is “don’t use” anyway, considering the networking issues. But I guess there are other people around who might wish to figure out WSL 2, too. Perhaps an identifier can be added in a next version?
Thanks, I’ll adapt the code to use uname(2) and check for WSL 1 only and document that.