본문 바로가기
Linux/Ubuntu Install

WSL - Port forwarding

by 드로니뚜벅이 2023. 10. 13.

Windows Server에 WSL을 설치하여 리눅스를 사용할 경우 Windows 와 WSL 간 네트워크 포트를 매핑해서 사용해야 하는 경우가 있습니다.

 

포트 포워딩 정보 확인

현재 설정된 포트 포워딩 정보를 확인하려면 아래 명령어를 실행합니다.

PS C:\Users\Sunny> netsh interface portproxy show all

ipv4 수신 대기:             ipv4에 연결:

주소            포트        주소            포트
--------------- ----------  --------------- ----------
0.0.0.0         80          172.26.104.184  80
0.0.0.0         8002        172.26.104.184  8002
0.0.0.0         8080        172.26.104.184  8080
0.0.0.0         8090        172.26.104.184  8090

 

포트 포워딩 정보 초기화(삭제)

현재 설정된 포트 포워딩 정보를 초기화(삭제)하려면 아래 명령어를 실행합니다.

PS C:\Users\Sunny> netsh interface portproxy reset

PS C:\Users\Sunny> netsh interface portproxy show all
ipv4 수신 대기:             ipv4에 연결:

주소            포트        주소            포트
--------------- ----------  --------------- ----------

 

포트 포워딩 설정

특정 포트에 대해서 포트 포워딩을 설정하려면 아래와 같은 스크립트를 작성하신 후 실행해 줍니다.

# 파일명: windows2wsl_port_forwarding.ps1

if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{   
	$arguments = "& '" + $myinvocation.mycommand.definition + "'"
	Start-Process powershell -Verb runAs -ArgumentList $arguments
	break
}

$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 comma
$ports = @(80,8002,8080,8090);

# [Static ip]
# You can change the addr to your ip config to listen to a specific address
$listenaddr='0.0.0.0';
$ports_a = $ports -join ",";

# Remove Firewall Exception Rules
# iex "Remove-NetFireWallRule -DisplayName 'WSL 2 Firewall Unlock' ";
$fireWallDisplayName = 'WSL 2 Firewall Unlock';
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=$listenaddr";
  iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$listenaddr connectport=$port connectaddress=$remoteport";
}

iex "netsh interface portproxy show v4tov4";

# After run Set-ExecutionPolicy RemoteSigned

명령어 형식은 아래와 같습니다.

* netsh interface portproxy add v4tov4 listenaddress=localaddress listenport=localport connectaddress=destaddress connectport=destport

 

위 스크립트를 윈도우 터미널(쉘)에서 관리자 권한으로 실행합니다.

PS C:\Users\Sunny> .\windows2wsl_port_forwarding.ps1

 

 

참고사이트