반응형
[ 다음은 다른 글 참조 해서 필요 사항만 정리 한 글입니다. ]
윈도우 WSL에 자동 할 당된 아이피와 포트를 윈도우에 할당된 아이피와 포트로 서로 연결 시켜 주어 외부 PC에서 해당 WSL의 프로그램에 접속 하게 할 때 다음과 같이 매번 수동으로 사용 할 수 있다.
PowerShell 프로그램을 관리자 권한으로 실행 후 파워셀 스크립트 파일 확장자 .ps1 으로 저장 후에 실행 하면 된다.
$my_wsl_address = 172.xxx.xxx.xxx
$port = 10000
netsh interface portproxy add v4tov4 listenport=$port listenaddress='0.0.0.0' connectport=$port connectaddress=$my_wsl_address
WSL 의 아이피는 윈도우가 부팅 할 때마다 바뀔 수 있는데 이부분으로 자동으로 검색 해서 원하는 포트로 연결 해주는 스크립트를 사용 하면 된다. ( 인터넷에 누군가 올려 놓은것을 대부분이 이 스크립트를 사용 하는 듯. )
다음과 같이 확장자만 .ps1으로 명명해서 실행 해 주면 된다.
혹은 윈도우 부팅 후에 wsl을 자동 실행 하고 아래 스크립트를 자동 실행 하게도 해서 사용 한다.
wsl-port-forward.ps1
: 중간의 $ports=@(80,443, XXXX); 이부분에 XXXX에 원하는 포트로 바꾸거나 추가 하고 사용. (80,443:은 http , https관련 포트 )
$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);
#[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";
}
파워쉘로 wsl-port-forward.ps1을 실행 시에
.\wsl_ip_forward.ps1
+ ~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : 보안 오류: (:) [], PSSecurityException
+ FullyQualifiedErrorId : UnauthorizedAccess
이런 에러가 발생하면
PowerShell.exe -ExecutionPolicy Bypass -File .\wsl-port-forward.ps1
이와 같이 Execution Policy를 설정해서 실행 해야 한다고 합니다.
참조 블로그)
https://codeac.tistory.com/118
https://www.sysnet.pe.kr/2/0/12347
https://www.sysnet.pe.kr/2/0/13280
반응형
'Programming' 카테고리의 다른 글
시그널 SIGFPE SIGILL SIGSEGV SIGBUS SIGABRT SIGHUP SIGINT SIGQUIT SIGTERM SIGKILL (0) | 2024.03.15 |
---|---|
C++ Visual Studio Error 해결 | strcpy fopen fscanf chdir freopen | _CRT_SECURE_NO_WARNINGS (0) | 2023.01.09 |
C# 정적 생성자 | Static Constructor (0) | 2022.09.01 |
visual c++/clr 프로그램 컴파일 에러 (0) | 2022.08.25 |
소스트리 로그인 실패 해결 | logon failed use ctrl c to cancel basic credential prompt (0) | 2022.08.22 |