2021년 1월 12일 화요일

WSL2를 외부에서 연결하기 위한 포트포워딩 Powershell script



WSL-Windows Subsystem for Linux 로 윈도우 내에 리눅스 커널을 내장시켜 리눅스를 돌린다.

WSL 버전 1과 2의 차이점

버전 1 - 윈도우에서 리눅스 커널을 실행.

버전 2 - Hyper-V 기반으로 리눅스가 가상환경에서 동작한다.




WSL2는 별도의 가상 NIC를 가지고 있고 WSL내부에서 사용하는 가상 IP를 가지고 있다.

-> WSL내부에서 사용하는 가상 IP는 재부팅시마다 IP값이 변경된다.

-> 별도의 포트포워딩 작업을 거쳐야 외부에서 접속가능하다.







https://dev.to/vishnumohanrk/wsl-port-forwarding-2e22

이곳의 내용을 참고하였습니다. 능력자분 감사감사 ㅋ_ㅋ


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=@(3000, 3001, 3002, 5500, 19000, 19001);

iex "netsh interface portproxy reset";
for( $i = 0; $i -lt $ports.length; $i++ ){
  $port = $ports[$i];
  iex "netsh interface portproxy add v4tov4 listenport=$port connectport=$port connectaddress=$remoteport";
}
iex "netsh interface portproxy show v4tov4";



메모장으로 위 내용을 복사 붙여넣기 한후


$ports=@(3000, 3001, 3002, 5500, 19000, 19001);


이 부분을 원하는 포트로 적어주면 된다.

원하는 포트가 2222,3333 일 경우

$ports=@(2222,3333);


원하는곳에 저장, 파일이름도 원하는대로 저장. 확장자는 ps1 로 저장.

예 (C:\wsl_port.ps1)





관리자 권한으로 Windows PowerShell 실행

C:\wsl_port.ps1

실행.

정상작동 확인.



댓글 없음:

댓글 쓰기

jfif 로 저장되는 문제

 크롬에서만 ? 비발디에서도 register \HKEY_CLASSES_ROOT\MIME\Database\Content Type\image/jpeg Extension 항목이 .jfif 로 설정 수정 -> jpg