-
Notifications
You must be signed in to change notification settings - Fork 0
WSL
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#!/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 unzipWith 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 :
- Get Ip Address of WSL 2 machine
- Remove previous port forwarding rules
- Add port Forwarding rules
- Remove previously added firewall rules
- Add new Firewall Rules
The script must be run at login ,under highest privileges to work, and Powershell must be allowed to run external sources.
Enable PowerShell to run external scripts, run the command below in PowerShell with administrative privileges.
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";
}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 ifconfigsudo 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- Edit
/etc/xrdp/startwm.sh
sudo nano /etc/xrdp/startwm.sh- Comment these lines :
#test -x /etc/X11/Xsession && exec /etc/X11/Xsession
#exec /bin/sh /etc/X11/Xsession- Add these lines:
# xfce
startxfce4
sudo /etc/init.d/xrdp start- Now in Windows, use Remote Desktop Connection
mstsc.exe /v:localhost:3390In 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
sudo apt remove openssh-server
sudo apt install openssh-server- Edit the sshd_config file by running the command
sudo vim /etc/ssh/sshd_config - In the sshd_config file:
- Change
PasswordAuthenticationto yes - Add your login user to the bottom of the file by using this command:
AllowUsers yourusername. Don't forget to replaceyourusernamewith your actually username. - Do
:wqto Save and Exit
- Change
- 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- Edit visudo
sudo visudo- Add the following line
%sudo ALL=NOPASSWD: /usr/sbin/sshdafter %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)
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}'). FromWSL Build 18945you can simply uselocalhost. -
Example :
netsh interface portproxy add v4tov4 listenport=8080 listenaddress=0.0.0.0 connectport=22 connectaddress=localhostssh username@your_computers_ipv4