Linux Fu: The Windows X11 Connection

The life of a Linux user can be a bit difficult. Sometimes you have to — or want to — run Windows. Why Windows? Sometimes you have a work computer or a laptop that Linux doesn’t support well. Or it might be software. Although there are plenty of programs that can edit, say, Word documents, there’s always that one document that doesn’t quite translate correctly. Things like videoconferencing software sometimes works on Linux but might have fewer features.

So what do you do? You can dual boot, of course, but that’s not very handy. You can run Windows in a virtual machine if you have enough horsepower. There’s also Wine, but that often has its own set of problems with features and stability of complex programs. However, recent versions of Windows provide the Windows Subsystem for Linux (WSL).

With WSL, you can have most of what you like about Linux inside your Windows session. You just have to know how to set it up, and I’m going to show you one way that works for me with reasonably stable versions of Windows 10.

About WSL

WSL is pretty powerful since it gives you a good Linux environment and it is tightly optimized with Windows. However, there are a few caveats. WSL actually has two variants. The first, version 1, works well enough, but doesn’t have complete compatibility and has slower disk I/O speeds. Most normal programs will work, but things like Docker and FUSE won’t. Version 2 requires virtualization support on your computer, but operates much much faster. It also provides a real Linux kernel, so most everything will run using WSL2.

If you are willing to take the recent developer versions of Windows 10 you can even run graphical programs if you are willing to run that far beyond the edge. But turns out, you don’t have to use the developer version at all. If there’s one thing X is good at, it is having a server running on one machine and clients running on another. So there’s no reason you can’t run X clients inside WSL and use a Windows-based X server to display.

First, Get an X Server

There’s no shortage of Windows-based X servers both free and commercial. I used Cygwin’s and, more specifically, I used a preconfigured Cygwin X11 setup called Swan. Unfortunately, the Swan project seems defunct, but Cygwin still exists and works fine.

Another option is VcXsrv and several other similar projects. The best part about these is they don’t have a lot of superfluous stuff. Cygwin has lots of things that, normally, will be very useful, but those things will be duplicates of what you will want to install under WSL.

I suggest you get the X server running first and test it out from another Linux machine or a virtual machine. If you have a system like Cygwin, you can just run native apps. If your X server doesn’t work for some reason, WSL won’t be able to use it either.

Version 2 for Performance

If you’ve used WSL for a bit, you should check to see if you are still using version 1. You can pretty easily switch between versions and even have multiple installations with different versions. To figure out which you have, open a PowerShell as an administrator:

wsl --list --verbose

You should see a line for each Linux distro you have installed and a version number. If you have version 1, make sure you have virtualization support turned on in your BIOS and also make sure the “Virtual Machine Platform” Windows feature is turned on. Then enter:

--set-version Ubuntu 2

Of course, use whatever name you saw in the list which may or may not be “Ubuntu.” This will take a bit of time or you will get an error message if you don’t have the BIOS and Windows settings right. You may also get some directions to download some updated kernel support. Just follow the instructions. If the command doesn’t take a bit to run, it probably is complaining about something not set up correctly. You can verify it with the list command again.

The version is important because the networking setup is different from the old version. While it is possible to configure the older WSL the same way, you might as well enjoy the performance increase. However, if your machine or OS don’t meet the requirements, you might have to stick with version 1.

You can also set version 2 to be the default from PowerShell:

wsl --set-default-version 2

If you happen to be running Windows 10 version 2004 or later, it is easier to install wsl now. However, you still want to make sure you are running version 2.

Connectivity

X11 clients know how to reach their display via the DISPLAY variable. Normally, you’ll see it set to :0 or :0.0 and that’s understood to be the first screen on the current machine. That won’t work here, though, because WSL is on a virtual network that isn’t the same as your Windows OS.

There are basically three steps you’ll need to work through to get things to work:

  1. Open port 6000 on your PC firewall, if you use one
  2. Tell the X server to allow connections from a different computer (in this case, a virtual computer)
  3. Set the DISPLAY variable to point to the WSL address that corresponds to the Windows computer

The exact steps will depend on what firewall and server you use. However, the general steps are the same for any tool you happen to use.

If you use the Windows Defender firewall you can open port 6000 by searching settings for Windows Defender Firewall. Select Advanced and then create a new inbound rule for TCP port 6000. However, Windows may pop up a firewall window the first time you run the server asking you if you want to allow connections. In that case, you probably won’t have to configure the firewall manually.

Setting access control for your X server will depend on the server you use. For Cygwin, try running xhost + from a Cygwin prompt. For VcXsv, you’ll want to check the startup option that allows external connections. You may not like the idea of opening up your X server, but if you can restrict your firewall settings or use an XAuthority file. First, get it working, then you can tighten up security. If you are behind a home router you can block things using its firewall.

The DISPLAY variable is the easiest of them all. A quick way to learn the Windows IP address from the WSL side is to look at how the nameserver is set. Dump /etc/resolv.conf and you should see something like this:

# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateResolvConf = false
nameserver 172.25.32.1

This means the DISPLAY variable should be set to 175.25.32.1:0.0 like this:

export DISPLAY=175.25.32.1:0.0

You could automate this with, say, awk but remember a proper shell script won’t set your environment. You’d need to source the script to allow that. For example, here’s a script that resides in /usr/local/bin/setWinX:

WINIP=$(awk '/^nameserver / { gsub("nameserver ",""); print; }' /etc/resolv.conf)
export DISPLAY="$WINIP:0.0"

You could, of course, arrange for that to run on startup, too by sourcing it from .profile:

source /usr/local/bin/setWinX

Depending on your X server and applications you might need to coax programs to use particular output drivers. For example, I’ve heard that this line will fix some problems:

export LIBGL_ALWAYS_INDIRECT=1

Once you have nice Linux/Windows integration, you can add some launcher support. Set your X server to autostart and you can merrily mix and match Windows and Linux apps on the same screen.

Unfair!

It hardly seems fair. Getting Windows stuff to run under Linux is tricky because Windows is closed. Making Linux run under Windows is much easier because Linux is open. Of course, sometimes you really have to run Linux for real. For example, I needed to directly read a mouse and found that the way WSL routes input events isn’t much like a regular Linux installation. Still, it works awfully well and performs nicely, too.

This isn’t the first time we’ve looked at WSL and that post has some good tricks if you are just starting out. How do you deal with using Windows and Linux? The ideal answer is you don’t and you just stick with one or the other. But tell us what you are doing in the comments.

38 thoughts on “Linux Fu: The Windows X11 Connection

    1. I too run Win7 in a VM when needed. Win7 was the last version of Windows I purchased. Never again. KUbuntu 20.04 LTS is the bare-metal OS on all home systems (laptops/servers/desktops). There is only one application (Print Shop) that my wife likes to use and it will only run in Windows…. Hence the VM for that one special case. Tried Wine, but no go….

      I personally don’t see what the draw to Windows is anymore, but that is just me.

    2. Leaving X server open without any firewall is not a good idea, unless you explicitly trust everything on the local network. Recommend being a little more specific on what’s allowed to talk to X server.

      1. I don’t recall a single instance where somebody did something to my local X server like put up a window or whatever. By and large xhost + is not very dangerous.

        Then again, I don’t think I met a desktop or laptop that had problems installing Linux.

        1. Those may not be problems this century, but in the early 90s getting X to work wasn’t so simple. My first attempt at using Linux, my video card (Diamond Speedstar 24x on a AMD 486DX-33) wasn’t usable yet. The kernel didn’t support it, nor did we know the dot clock settings. Sometimes getting these wrong would ruin your monitor. So, my Linux experience got put on hold until there were settings for it. Eventually, when I got a newer computer, the 486 got a cheap video card that was compatible and my tinkering went on. Late 90s, most mainstream components were fine for Linux and X, but you still checked before buying.

  1. I have 5 HP DL360 servers and 4 Dell C6100 blade servers. All have Linux on them. I also have several NUC’s. My primary NUC has Windows 10 with WSL 2 installed. Whether I configuring, scripting or programming events I switch from Windows to WSL for those functions I need. To read emails and such I use Windows 10 as it works with Corporate stuff….
    I’ve always been a big fan of Linux and OpenSource, but with WSL I just have a more convenient environment in one package. Enjoyed the article.

    1. You’re either an admin or spends way too much time on r/homelab. I have one of those HP servers and the fan never drops below 40% even in 50° F temperature and it’s too loud in the garage. I can only imagine 5 of those going with those PEs. What do you do with all of that processing and space?

  2. Been running Linux since Debian 2.1 (Slink). After Win2k went bye bye I fully switched and never looked back. All I know is Microsoft will screw things up with the Linux/BSD world if we keep letting this go on.

    1. You try running something that has been written with no intention to run on anything but the unix based families – or even more specific than that and just for Linux you must either put in lots of work yourself to write whatever bits are missing and compile the port for Windows (or the other unix likes), or now just run it on the Linux subsystem in Windoze (at least in theory it should just work as if it was a native Linux install). Same thing the other way round – which is why folks run VM’s on their Linux system to let Windoze apps run, and the WINE/ Proton project exists.

      So the use case really is just providing that familiar environment in which stuff works as you expect (or just works correctly/at all), but sitting ontop of Windows…

      So the cynic in me says the only point is that MS don’t want to loose almost all of the really smart, interested, and highly computer literate folk from their platform – make it easy to run opensource goodness inside your closed platform and many of that group will not bother purging the windows install most computers ship with, they can access everything they want conveniently anyway… Only those hardcore FOSS enthusiast or those with a reason to not want to offer up their digital self on the altar of M$ are likely to choose a from scratch install of something FOSS.

      1. Plus when its not your machine but the companies you have to use the tools you are given – well here is a way around that at least somewhat – you can use tools from the other basket too.

  3. Back in the Vista days I had a choice, either run Linux under Windows or run Windows under Linux. I went with the later. Most of my Windows stuff ran using Wine. For the others I used VirtualBox to run XP. Over time less programs ran under XP so now I have a 37GB Window 10 virtual drive. Maybe once every couple of weeks I fire it up to run a program that doesn’t work with Wine. I definitely made the right choice, Linux is great and way less trouble than Windows and there is free software to do almost everything. I run Mint, give it a try.

    1. Don’t get me wrong. All of my personal machines save one laptop are all Linux However, I frequently work on machines I don’t control that have Windows on them. This can make it nicer assuming I can install on them.

  4. It’s funny to see conference software as an example of something one might need Windows for.

    I thought that was going to be a problem when my workplace switched to home last year. It’s kind of a Microsoft shop and they opted on Teams, a Microsoft product. But Teams worked just fine in Linux. I even had fun with the Video4Linux dummy wrapper and Insecam.org to display all sorts of places instead of my messy home office.

    OTOH, the first time I had to work with someone in another company they used some Oracle product and there was zero Linux support. By the next time I worked with them they had switched to Teams for whatever their own reasons were so everything was fine after that.

  5. WSL.. Isn’t that backwards? Back in the 90s, it would have been correctly called Linux Subsystem (for Windows), wouldn’t it?

    Otherwise, it would have been called Windows’ Subsystem for Linux (notice the ” ‘ “, which expresses that Windows is the owner/host), hm?

    I mean, there’s the POSIX subsystem, the OS/2 subsystem, the Win32 subsystem etc..

    https://en.wikipedia.org/wiki/Microsoft_POSIX_subsystem

    The current, official naming (WSL) just doesn’t sound right or professional to me. Maybe it’s just me? 😔

    1. I consistently think the same thing whenever I hear WSL — whenever I hear “WSL” I have to remind myself that it is indeed named “Windows Subsystem for Linux”, not “WLS” (or any other more sensible name).

      I’ve been thinking about WSL on and off since it was introduced a few years ago, and I’ve used it a little bit. I think it’s strange that Microsoft decided to integrate Linux into windows; I would assume more Linux users would have preferred the opposite. That is, I would rather have seen Microsoft invest development time into Wine. A Microsoft backed Wine would be a LOT more stable and compatible than Wine currently is.

      But this isn’t about what Linux user want. I have to remind myself that Microsoft, at the end of the day, profits from users being part of its ecosystem; it’s a better business decision to integrate Linux into Windows.

      Personally, I’m skeptical of WSL.

      I’m skeptical for the same reasons as other commenters; Microsoft’s “embrace and extend” tactic is very real. That being said, I don’t think WSL has the power to kill Linux; it’s hard to beat a truly free product (especially with WSL, which is still somewhat clunky compared to real Linux). However, it might have the power to reduce Linux’s market share by capturing the attention of those who are somewhat bi-curious about Linux, but have spent their entire lives on Windows.

      I refuse to run WSL at home. Truth be told, though, it is nice to have WSL when you’re forced to work on a Windows PC.

      1. On the odd world of turning acronyms into “names”… I think WSL fits well enough… either for “Whistle” in (as I see it) feigned innocence when trying not to act guilty of something… or Weasel as in, well, being a weasel in the trying to sneak something past you… Either way is not a “pleasant” thing but usually one of some sort of kluge necessity… I understand the lure of running Windoze under Linus for some of the more obstinate applications, but why one would want to run Linux under the “FUBAR” that is Windoze?!? I will keep them as separate as possible, thank you very much…

    1. They say that in the article.

      “If you are willing to take the recent developer versions of Windows 10 you can even run graphical programs if you are willing to run that far beyond the edge. But turns out, you don’t have to use the developer version at all.”

  6. Check out MobaXterm! Has X Server built in and easy to configure to many needs. Free version is available, but business version is very inexpensive (<$70 US per yr) and worth every penny. Full bash shell, other remote clients (RDP, VNC). Even supports PowerShell and WSL!

  7. I think WSL1 was a lot better than 2. Better integration with the host OS. Maybe not as performant but you’re not really going to run heavy programs on it anyway. Then it’s better to run Linux natively.

    WSL2 feels like a bit of a cop out with its VM. This has always been possible. Hooking Linux app into the real NT kernel was quite clever.

    1. I’m with you there – I use WSL (1) because it’s a lot more convenient on a work machine that still has a lot of Win10 proprietary applications that have no alternative, so having the link with the file system is really handy with the amount of time I’m running handy scripts etc

  8. While this article is about X11, one thing with noting is that WSL2 does not (yet) have USB support. This tends to come as a surprise to most and will (obviously) get in the way of some tasks, such as embedded development and flashing devices.

    That’s just a heads-up to save some time and frustration, should you need to use a USB device on the Linux side. 🙂

    Apparently there are tools for piping a serial connection from Windows to WSL via IP. At that point, though, it might just be easier to run Linux in a VM and connect Windows to the VM for X11, etc…

    Another thing worth noting is that WSL2 can use up all available memory and not free it. That problem is fixed in Windows Insiders builds of Windows 10, but not in the mainstream Windows 10 (AFAIK).

  9. I use WSL a lot for work to run reports and whatnot since we all have Windows laptops there. I have stuck with WSL 1 since it does what I need, no need for the linux gui. At home I dual boot which works out fine most of the time. Wine has gotten a lot better over the years but games still play best natively in Windows, otherwise I would be Linux all the time!

  10. A important note for OpenGL compatibility: When using VcXserv with indirect rendering, you are limited to OpenGL 1.4 version, due to technical limitations. This bit me hard when I tried to develop graphical linux application, this caused visual glitches I tried to debug for too long. I hope if WSL sometime in the future releases its X server to main channels, it supports newer OpenGL versions.

  11. WSL2 seems an improvement, perhaps the old MS rule of 3 versions will provide the proverbial charm. As hinted, its Windows app store presence makes it acceptable for employer/corporate owned device use where VPNs, Firewalls, and Bitlocker prevail and Linux remains unsupported if not prohibited. Apparently WSL2 has an accepted niche for Docker Drupal development and the use case seems lighter weight than a full Windows Hypervisor / Linux Guest or PortableApps.

    I’m still using Cygwin 64, fighting with Emacs client and printer configs like its 1999. Perhaps X11 can help. If not a VMWare Workstation for Windows license makes Linux environment accessible, until the emergence of WSL 3. I recall an old project using the MKS Toolkit as a build environment and an Internet search now yields PTC MKS Toolkit with a built in X Server.

  12. Cygwin was my standby for a long time. WSL seemed like the holy grail until we found out there’s no USB support. WSL 2 was supposed to be this great thing that had USB support……

    So, now I just use VMs and MobaXterm.

  13. Please remember to sign up for a Microsoft account when using Windows, local accounts will be removed in the future updates. Make sure you agree to all of Microsofts Terms and COndititions…. Please join the user experience program, and let us learn how you use your computer by sending data about the programs you are using and let us learn about the words you are typing, to help you. Cortana appreciates the extra data, and your future overlords will appreciate the data you are supplying, to better understand your ‘customer’. Please enjoy the future of subscription paid computing, use your personal hardware for as little as $30 per month. Enjoy your Microsoft experience.

    ELSE;

    Enjoy the freedom of Open Source computing, developed by a community of peers all across the world. Enjoy the rich features of free powerful applications, on your free for life system or your personal hardware. User accounts and registration not required. We will support your x86 computer from 1995, for the rest of it’s lifespan.

    A wider range of software will continue to be developed, and will always be the alternative for the community.

    ;)

  14. So Windows starts supporting linux via X while linux moves on to Wayland. Great.

    I put cygwin on my Windows laptops just so I can use the linux commands and many of the cygwin supported applications. The only applications I need Windows for is AutoDesk Inventor, WPI’s FIRST Robotics code simulation and WPI’s Driver Station. I am hoping that when our robotics team moves to Fusion 360 I can run that on the web through a browser but we’ll still need a Windows box for Robotics Code deployment and simulation.

    I’ve tried dual booting, and running linux under Windows through VirtualBox but always revert to just using Windows on the laptop. At one time we had converted a Win 7 hard drive setup to run under Virtual Box from Fedora 32 that was only used for TurboTax. WSL looks like a pain. Never had success running any Windows program with Wine. Inventor won’t run remotely and I doubt it would run under a remote X session.

    I have difficulty running LibreOffice on Win 8.1 as it crashes Win 8.1 after almost every Win update. Then I have to uninstall and reinstall LibreOffice. So far Win 10 seems to play nicer. If WSL does the same and you have to go through these instructions after every Windows update its not going to be very useful.

    Currently running one laptop on Win 8.1 that will need to go to linux soon, one Win 10 laptop, PopOS 20.04 LTS on my desktop, Fedora 33 on the family room computer, Fedora 32 on a 12 year old laptop, Debian 10 with linuxcnc, Raspbian with cups server on a Pi 3, and OctoPi on a Pi3 for a 3D printer.

Leave a Reply

Please be kind and respectful to help make the comments section excellent. (Comment Policy)

This site uses Akismet to reduce spam. Learn how your comment data is processed.