※ 윈도우 WSL2 설치 참고

 

[Window] 윈도우 10 WSL 2 설치 방법

1. WSL 설치 조건 : Window 11 이상 or Window 10 버전 2004 이상(빌드 19041 이상) PowerShell 혹은 cmd 를 관리자 권한으로 실행 WSL 다운로드 wsl --install ※ Window 빌드 확인 방법 더보기 cmd 를 실행 버전 확인 ver

junghunnote.tistory.com


1. WSL2 포트포워딩

  • WSL → 외부 네트워크 = 이상없음
  • 외부 네트워크 → WSL = 윈도우 PC의 포트로 아무리 연결해도 WSL 접근이 안됨
  • 따라서 윈도우 → WSL 으로 포트포워딩 작업이 필요

2. 스크립트 파일 생성

  • C 드라이브에 WSL_portForwarding.ps1 파일 생성 (파일명은 상관없음)
  • 아래 코드 내용 복사
$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=@(3690, 12345);


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

Invoke-Expression "netsh interface portproxy show v4tov4";
  • ports 변수에 원하는 포트번호 설정 후 저장

3. WSL net-tools 패키지 설치

  • WSL 창 실행
  • ifconfig 확인
ifconfig
  • 오류 발생시 net-tools 패키지 설치
sudo apt install net-tools

4. 스크립트 파일 실행

  • PowerShell 을 관리자 권한으로 실행
  • 실행규칙 변경
Set-ExecutionPolicy RemoteSigned
  • 다음과 같이 나오면 Y 또는 A 입력
실행 규칙 변경 실행 정책은 신뢰하지 않는 스크립트로부터 사용자를 보호합니다. 실행 정책을 변경하면 about_Execution_Policies 도움말 항목(https://go.microsoft.com/fwlink/?LinkID=135170)에 설명된 보안 위험에 노출될 수 있습니다. 실행 정책을 변경하시겠습니까? [Y] 예(Y) [A] 모두 예(A) [N] 아니요(N) [L] 모두 아니요(L) [S] 일시 중단(S) [?] 도움말 (기본값은 "N"):
  • 스크립트 파일 실행
C:\WSL_portForwarding.ps1
  • 포트포워딩 완료

  ※ PC 재부팅시 포트포워딩 자동화

 

[Window] 윈도우 10 WSL 2 포트포워딩 자동화

※ 윈도우 WSL2 포트포워딩 참고 [Window] 윈도우 10 WSL 2 포트포워딩 ※ 윈도우 WSL2 설치 참고 [Window] 윈도우 10 WSL 2 설치 방법 1. WSL 설치 조건 : Window 11 이상 or Window 10 버전 2004 이상(빌드 19041 이상) Po

junghunnote.tistory.com


참조

  1. https://velog.io/@sangwoong/WSL2-PortForwarding

+ Recent posts