WSL 포트포워딩으로 가상의 IP 매핑하여 고정적 IP 할당하기
회사에서 개발시에는 WSL로 우분투 환경에서 작업을 하는 상황
이번에 백엔드 개발을 하면서 프론트엔드 개발자님께 서버를 하나 열어 드려야 했는데
나의 컴퓨터 아이피로 열어둔 서버로 접근이 될 줄 알았는데 안되더라
다른 개발자는 우분투 환경에서 서버 열어도 해당 pc IP로 접속 된다던데.. 안되길래 찾아보니 wsl버전이 다르더라
WSL1 같은 경우엔 윈도우에서 리눅스 쉘을 열어주지만
WSL2는 가상환경 (Hyper-V) 위에서 리눅스 쉘을 실행하며 그 쪽에서 172~로 시작하는 가상 아이피를 할당하게 된다고 한다.
실제로 Ipconfig로 확인하면 WSL 아이피는 따로 돌아가게 된다.
https://github.com/microsoft/WSL/issues/4150#issuecomment-504209723
[WSL 2] NIC Bridge mode 🖧 (Has TCP Workaround🔨) · Issue #4150 · microsoft/WSL
Issue WSL 2 seems to NAT it's virtual network, instead of making it bridged to the host NIC. My goal is for a service running in Ubuntu in WSL 2 to be accessible from anywhere on my local netwo...
github.com
위의 글을 참고하면 해결 방법은
The work around is to use a script that does :
- Get Ip Address of WSL 2 machine : WSL2의 아이피 주소를 알아내기
- Remove previous port forwarding rules : 이전 포트포워딩 룰 없애기
- Add port Forwarding rules : 포트포워딩 룰 추가
- Remove previously added firewall rules : 이전에 추가한 방화벽 규칙 지우기
- Add new Firewall Rules : 새로운 방화벽 규칙 추가
아래의 스크립트를 실행하면 해결이 된다.
wsl 아이피 주소를 쓰면 되고 매핑할 포트를 적어주고 실행하면 된다.
# 로컬에서 WSL2로의 Port Foward 연결 $my_wsl_address = 172.22.192.40 $port = 8000 netsh interface portproxy add v4tov4 listenport=$port listenaddress='0.0.0.0' connectport=$port connectaddress=$my_wsl_address
이건 매번 변경되는 WSL2의 IP를 신경쓰기 번거로울 수 있다
하단의 스크립트를 저장하여 윈도우 작업 스케쥴러에서 컴퓨터 시작시 실행되게 하면 편하다.
$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 coma $ports=@(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]; iex "netsh interface portproxy delete v4tov4 listenport=$port listenaddress=$addr"; iex "netsh interface portproxy add v4tov4 listenport=$port listenaddress=$addr connectport=$port connectaddress=$remoteport"; }
$ports에 사용할 포트를 넣어주면 된다.
실행시
ifconfig config not found
가 뜰 수도 있다.
-> 아래와 같이 해결하였음 ( 하단의 링크를 참조했다 )
스크립트의 bash.exe -c "ifconfig eth0 | grep 'inet '" 인 bash로 명령어 실행 과정에서 ifconfig 명령어가 작동하지 않아 생긴 문제
WSL2 상의 환경에서 ifconfig를 입력해보시고 command not found 메세지가 뜨는 경우,
apt install net-tools 를 통해 패키지를 설치
실행법
스크립트를 powershell 확장자로 저장하고 바탕화면에 저장
powershell 관리자 권한으로 실행 후
바탕화면 위치로 이동
저장해놓은 powershell script 실행한다.
.\\wsl_port.ps1
도움 받은 글 :
https://codeac.tistory.com/118
WSL2, 외부 네트워크와 연결하기
node.js를 WSL2에서 구동하였는데 로컬만 접속되고 외부에서는 접속이 안돼요 Django를 WSL2에서 구동하였는데 외부에서 접속이 안돼요 와 같은 문제점을 해결하는 글입니다 현재 Window10 preview 2004 버
blog.aaronroh.org