Skip to content

Instantly share code, notes, and snippets.

@omgreenfield
Last active March 1, 2021 21:18
Show Gist options
  • Save omgreenfield/a922c0ccd9b9ae64153990891594dab9 to your computer and use it in GitHub Desktop.
Save omgreenfield/a922c0ccd9b9ae64153990891594dab9 to your computer and use it in GitHub Desktop.
Developing for Ubuntu on Windows

Outline

  • Install Windows Terminal
  • Install VS Code
  • Install WSL2
  • Install Ubuntu
  • Install Remote Development extension for VS Code
  • Forward ports
  • Develop

Forward ports

Get Ubuntu IP, run on WSL

hostname -I

Set port forwarding rules

netsh interface portproxy add v4tov4 listenport=3001 listenaddress=0.0.0.0 connectport=<port> connectaddress=<Ubuntu IP>

You can also use attached PowerShell script

Develop

# launch default WSL at home directory
wsl ~

# go to your project directory
cd workspace/something

# open VS Code from folder
code .

Important notes

  • Changing things in Windows hosts file should be sufficient for Ubuntu hosts
  • For Docker, use Docker Desktop. Tutorial
  • Never use files in Windows filesystem. If you need to access Ubuntu file system from Windows explorer, go to \\wsl$
$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 {
Write-Output "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, 3001, 3005, 3006, 3500, 4001);
#[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
Invoke-Expression "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
#adding Exception Rules for inbound and outbound Rules
Invoke-Expression "New-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' -Direction Outbound -LocalPort $ports_a -Action Allow -Protocol TCP";
Invoke-Expression "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];
Invoke-Expression "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
Invoke-Expression "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
}
Invoke-Expression "netsh interface portproxy show all";
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment