Installing a Server in a WSL

WSL stands for “Windows Subsystem for Linux” and is a surprisingly successful initiative by Microsoft that allows Windows users to run Linux apps.

WSL is particularly useful for Windows users that need to interact with Linux servers, by providing an easy to use terminal window from which he can use the tools that every Linux user applies to interact with other Linux machines, such as networking commands, ssh, sftp, etc.

Installing WSL is possible in all Windows 10 and 11 versions, and can be achieved by simply opening up a Windows Powershell window, and issuing a command:

wsl –install

This will in fact install a working Ubuntu instance that the user can run within his running Windows OS.

For desktop usage, it is much easier to use than a dual boot (having the machine boot alternatively in Windows or Linux) and specially will allow the user to run both at the same time, for example, interacting between a windows app and a Linux service running in the WSL.

One may wonder if it would be possible have a server working in the WSL, being accessed from the outside? The answer is yes! But some knowledge about the way the networking is done to access the WSL instance is needed, because routing is required for the TCP/IP packets to be directed to the Linux box.

WSL works inside a Virtual Machine, although the user doesn’t notice it because the process is fully managed by Windows. If you open the WSL window and issue a command to check the interface IP address:

Ifconfig -a

you will see that the ethernet interface uses an address within 172.X.X.X. In my machine this shows as:

eth0: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500

        inet 172.25.137.91  netmask 255.255.240.0  broadcast 172.25.143.255

        inet6 fe80::215:5dff:feb0:a8ae  prefixlen 64  scopeid 0x20<link>

        ether 00:15:5d:b0:a8:ae  txqueuelen 1000  (Ethernet)

        RX packets 922  bytes 293092 (293.0 KB)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 109  bytes 7558 (7.5 KB)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536

        inet 127.0.0.1  netmask 255.0.0.0

        inet6 ::1  prefixlen 128  scopeid 0x10<host>

        loop  txqueuelen 1000  (Local Loopback)

        RX packets 0  bytes 0 (0.0 B)

        RX errors 0  dropped 0  overruns 0  frame 0

        TX packets 0  bytes 0 (0.0 B)

        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

Thus the shown IP address for the eth0 interface is 172.25.137.91. This address could also be obtained by using a simple command within a WSL terminal window:

ip addr show eth0| grep-oP ‘(?<=inet\s)\d+(\.\d+){3}’

WSL uses NAT (Network Address Transalation) to ensure communication between systems with different IP networks. Accessing the WSL from the Windows OS can be done accessing localhost, because the Windows performs automatically the NAT conversion. But, if for example you setup a Web server on the WSL and access your Windows machine from the outside, it will need some additional configuration to work.

Let’s try it. We can easily install an Apache Web server in the WSL box by issuing the following commands:

sudo apt-get install apache2

sudo service apache2 start

The HTML pages that are located in /var/www/html will be made available through the new apache server. Upon installation, you will see just the default page, and you can test this by using a Windows browser accessing:

http://localhost

or

http://172.25.137.91

If however you want to user your Windows machine IP interface, in practice setting up a web server in the machine WSL, an extra step is needed. You need to tell the Windows machine that packets directed to the port 80 (http) will be directed to the WSL IP address:

netsh interface portproxy add v4tov4 listenport=80 listenaddress=0.0.0.0 connectport=80 connectaddress=172.25.137.91

You can check the success of the command with:

netsh interface portproxy show v4tov4

That’s it, you have a Web Server working on your WSL.

Deixe um comentário

O seu endereço de email não será publicado. Campos obrigatórios marcados com *