WSL 포트포워딩

0xf4d3c0d3·2022년 5월 6일
0

외부에서 WSL로 포트포워딩 해주기 위해 몇가지 작업이 필요하다.

먼저 아래 ports와 addr를 편의에 맞게 설정하여 powershell로 실행한다.

#[Ports]
#All the ports you want to forward separated by coma
$ports=@(5001);


#[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 ",";


$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;
}

#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";
}

만약 ... cannot be loaded because the execution of scripts is disabled on this system. 같은 에러가 보인다면, Set-ExecutionPolicy RemoteSigned로 해결한다.

공유기를 사용중이라면 NAT 포트포워딩 설정이 추가로 필요하다.

나는 LG Uplus 공유기를 사용했으나 iptime이라도 비슷하게 해주면 된다. 내부 IP주소를 모른다면 포워딩할 대상 호스트의 ip를 넣으면 된다. Windows라면 ipconfig 명령어의 IPv4 Address로 확인 가능하다.

0개의 댓글