Thysbelon.Logo of a Hummingbird moth.

Jekyll WSL2 localhost 0.0.0.0 View on Mobile Fix

If you're making a website, you may want to test that website on smartphones. If you're making your website with Jekyll, you may want to run it in WSL2 to avoid any Windows compatibility issues. But what if you want to do both of these things? You may run into trouble.

Unlike WSL1, WSL2 is a Virtual Machine. A consequence of this is that the virtual Linux machine in WSL2 has a different IP address from the host Windows machine. So if you run jekyll serve --host 0.0.0.0, then try to connect to your Windows machine's IP using your phone browser, it won't connect. If you use a tool like ifconfig in the Linux machine to find its IP address, then navigate to that in your phone, it still won't work, because the only device that can access your virtual Linux machine's IP is the host machine.

There are many different ways to solve this, one of which is to just ditch WSL2 and use either WSL1 or Ruby for Windows; if you only use Jekyll, it may be worth it. I didn't want to change my setup that drastically (because I'm lazy), and I didn't want to install any new software either (like an SSH server). My quick fix was to download the Python Windows embeddable package, shift + right-click python.exe to copy the path, run jekyll serve on the Linux machine, navigate to the _site folder of my Jekyll project in Powershell, then paste the python path and run -m http.server (make sure to remove the quotes from the python.exe path or it won't work for some reason! Powershell is weird, but CMD can't navigate to the WSL2 filesystem).

Microsoft's official website mentions this IP address issue, and links to a Powershell script that sets port forwarding rules for WSL2 whenever its IP changes. I'll probably use this.

Tags:
Categories: