WSL - kamack38/Essentials GitHub Wiki

Setup WSL 2

Enable WSL 2 and update the linux kernel (Source)

# In PowerShell as Administrator

# Enable WSL and VirtualMachinePlatform features
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

# Download and install the Linux kernel update package
$wslUpdateInstallerUrl = "https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi"
$downloadFolderPath = (New-Object -ComObject Shell.Application).NameSpace('shell:Downloads').Self.Path
$wslUpdateInstallerFilePath = "$downloadFolderPath/wsl_update_x64.msi"
$wc = New-Object System.Net.WebClient
$wc.DownloadFile($wslUpdateInstallerUrl, $wslUpdateInstallerFilePath)
Start-Process -Filepath "$wslUpdateInstallerFilePath"

# Set WSL default version to 2
wsl --set-default-version 2

Install common dependencies

#!/bin/bash

sudo apt update && sudo apt install -y \
    apt-transport-https \
    ca-certificates \
    curl \
    gnupg-agent \
    software-properties-common \
    git \
    make \
    tig \
    tree \
    zip unzip

TPC Network Forwarding

Introduction

With the introduction of WSL 2 Beta, Microsoft has made changes to the system architecture. The changes include changing from the default bridged network adapter to a hyper-v virtual network adapter. The implementation was not completed during the launch of the beta program. This makes accessing of network resources under WSL 2 complex. The workaround is to forward the TCP ports of WSL 2 services to the host OS. The virtual adapter on WSL 2 machine changes it's ip address during reboot which makes it tough to implement a run once solution. Also, a side note, Windows firewall will block the redirected port.

The work around is to use a script that does :

  1. Get Ip Address of WSL 2 machine
  2. Remove previous port forwarding rules
  3. Add port Forwarding rules
  4. Remove previously added firewall rules
  5. Add new Firewall Rules

Configuration

The script must be run at login ,under highest privileges to work, and Powershell must be allowed to run external sources.

PowerShell Configuration

Enable PowerShell to run external scripts, run the command below in PowerShell with administrative privileges.

How To

Go to search, search for task scheduler. In the actions menu on the right, click on create task. Enter Name, go to triggers tab. Create a new trigger, with a begin task as you login, set delay to 10s. Go to the actions and add the script. If you are using Laptop, go to settings and enable run on power.

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
$found = $remoteport -match '\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}';

if( $found ){
  $remoteport = $matches[0];
} else{
  echo "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

#[Ports]

#All the ports you want to forward separated by coma
$ports=@(80,443,10000,3000,5000);


#[Static ip]
#You can change the addr to your ip config to listen to a specific address
$addr='0.0.0.0';
$ports_a = $ports -join ",";


#Remove Firewall Exception Rules
iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";

#adding Exception Rules for inbound and outbound Rules
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
iex "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Inbound -LocalPort $ports_a -Action Allow -Protocol TCP";

for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
  iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}

Credits

edwindijas

Ubuntu GUI

Install xrdp, xface4 and ifconfig

sudo apt update && sudo apt -y upgrade
sudo apt-get purge xrdp
sudo apt install -y xrdp
sudo apt install -y xfce4
sudo apt install -y xfce4-goodies
sudo apt install -y ifconfig

Configuration

sudo cp /etc/xrdp/xrdp.ini /etc/xrdp/xrdp.ini.bak
sudo sed -i 's/3389/3390/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/max_bpp=32/#max_bpp=32\nmax_bpp=128/g' /etc/xrdp/xrdp.ini
sudo sed -i 's/xserverbpp=24/#xserverbpp=24\nxserverbpp=128/g' /etc/xrdp/xrdp.ini
echo xfce4-session > ~/.xsession
  1. Edit /etc/xrdp/startwm.sh
sudo nano /etc/xrdp/startwm.sh
  1. Comment these lines :
#test -x /etc/X11/Xsession && exec /etc/X11/Xsession
#exec /bin/sh /etc/X11/Xsession
  1. Add these lines:
# xfce
startxfce4

sudo /etc/init.d/xrdp start
  1. Now in Windows, use Remote Desktop Connection
mstsc.exe /v:localhost:3390

Connection form outside

In PowerShell as administrator

$remoteport = bash.exe -c "ifconfig eth0 | grep 'inet '"
netsh interface portproxy add v4tov4 listenport=3390 listenaddress=0.0.0.0 connectport=3390 connectaddress=$remoteport

Setting up an SSH server

Install SSH Server

sudo apt remove openssh-server
sudo apt install openssh-server

Enable password login (optional)

  1. Edit the sshd_config file by running the command sudo vim /etc/ssh/sshd_config
  2. In the sshd_config file:
    • Change PasswordAuthentication to yes
    • Add your login user to the bottom of the file by using this command: AllowUsers yourusername. Don't forget to replace yourusername with your actually username.
    • Do :wq to Save and Exit

Start or restart the SSH service

  • Check the status of the ssh service:
service ssh status
  • Start ssh server
sudo service ssh start
  • Restart ssh server
sudo service ssh --full-restart

Allow SSH service to start without password

  1. Edit visudo
sudo visudo
  1. Add the following line
%sudo ALL=NOPASSWD: /usr/sbin/sshd

after %sudo ALL=(ALL:ALL) ALL

You can test that you don't need a sudo password when you start ssh by running sudo service ssh --full-restart (if ssh is already running) or sudo service ssh start(if ssh is not running)

Add port forward rule

Now you need to set up port forwarding to be able to connect to your WSL server and not interfere in any SSH Servers on your Windows machine.

Note: You DO NOT need to do this if you don't have any SSH servers on your Windows machine

  • listenport= - could be any opened and unused port

  • connectport - is your ssh server port on WSL (by default 22)

  • connectaddress= - is your WSL address (ip addr | grep 'eth0' | grep 'inet ' | awk '{print $2}'). From WSL Build 18945 you can simply use localhost.

  • Example :

netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=22 connectaddress=localhost

Test SSH Connection

ssh username@your_computers_ipv4