Skip to content

Instantly share code, notes, and snippets.

@gordallott
Last active November 3, 2019 18:35
Show Gist options
  • Save gordallott/8e208c33be4dae64b31520d8028f1fe7 to your computer and use it in GitHub Desktop.
Save gordallott/8e208c33be4dae64b31520d8028f1fe7 to your computer and use it in GitHub Desktop.
things I had to do for wsl2 sanity

Get openssh working

  • ensure openssh-server is running, modify /etc/ssh/sshd_config. ensure PasswordAuthentication yes.
  • run sudo dpkg-reconfigure openssh-server to regenerate the ssh keys which aren't always there i guess
  • sudo service ssh --full-restart

Get ports publicly accessible to the network

$remoteport = wsl.exe -u gord -d "Axiom" bash -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;
}


echo "remoteport: " $remoteport
#[Ports]

#All the ports you want to forward separated by commaa
$ports=@(22,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];
  echo "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";
  iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr";

  #echo "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";
  #iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport";

  echo "netsh interface portproxy add v4tov4 listenport=$port connectport=$port connectaddress=$remoteport";
  iex "netsh interface portproxy add v4tov4 listenport=$port connectport=$port connectaddress=$remoteport";
}

the above powershell script will setup port forwarding for a wsl distribution named "Axiom" for a user "gord". change those as needed. this can be set as trigger

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.

setting a default user for a wsl distro

Find your UID for your username in your Linux distro typing in the command id -u . Remember this value. Open registry edit and navigate to HKEY_CURRENT_USER\Software\Microsoft\Windows\CurrentVersion\Lxss{MY-UUID}. Please be careful when editing registry keys! Replace the DefaultUid value with the UID value of the user in your distro.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment