windows:wsl

This is an old revision of the document!


wsl vhdx compact, method

WSL2 port forwarding

 netsh interface portproxy add v4tov4 listenport=$PORT listenaddress=0.0.0.0 connectport=$PORT connectaddress=127.0.0.1 
# on unprivileged user
$remoteport = bash.exe -c "ip a s 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;
}
 
Write-Host @remoteport
 
 
 
# on privileged user
#[Ports]
 
#All the ports you want to forward separated by coma
$ports=@(1433,1434);
 
 
#[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 ",";
 
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=172.31.28.155";
}
 
 
# Show rules
netsh interface portproxy show all

Windows system linux

Windows terminal config

  • windows/wsl.1630580348.txt.gz
  • Last modified: 2021/09/02 10:59
  • by admin