port forwarding WSL2
TL;DR
netsh interface portproxy add v4tov4 listenport=<listenport> listenaddress=0.0.0.0 connectport=<connectport> connectaddress=$(wsl -d <distro-name> hostname -I)
ssh into WSL2 & vs-code์ ๋ค๋ฅธ์ ์ด๋ผ๊ณ ๋งํ๋ค๋ฉด Power Shell ๊ด๋ฆฌ์ ๋ชจ๋์์ netsh
๋ช
๋ น์ด๋ฅผ ์ง์ ์ณ์ผ ํ๋ค๋ ๊ฒ์ด๋ค. SSH์ ๊ฒฝ์ฐ 22๋ฒ ํฌํธ๋ฅผ ์ด์ด์ฃผ์ด์ผ ํ๋๋ฐ, ์๋์ฐ์์ 22๋ฒ ํฌํธ๋ฅผ ์ด์ด์ฃผ๋ฉด VSCode์์ ์์์ ํฌํธ ํฌ์๋ฉ์ ํด ์ฃผ๋ ๊ฒ ๊ฐ์๋ค.
์๋ ์คํฌ๋ฆฝํธ์ ๋ฐ๋ฅด๋ฉด, ํฌํธ๋ฅผ ์ถ๊ฐํ ๋๋ง๋ค ๋ณต์กํ๊ฒ ์ ๊ธด ๋ช
๋ น์ด๋ฅผ ์น ํ์ ์์ด 1. WSL ip์ฃผ์๋ฅผ ์์๋ด๊ณ , 2. $ports
์ ์ ํ ํฌํธ๋ฒํธ๋ค์ 3. ์๋์ฐ ๋ฐฉํ๋ฒฝ์ WSL2 Firewall Unlock
์ด๋ ์ด๋ฆ์ผ๋ก ์ธ๋ฐ์ด๋ ๊ท์น์ ์ถ๊ฐํ๋ค.
$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";
}
Try
week06 {swjungle}{proxy-lab}์ค์ต์ ํ๋ ค๋๋ฐ ์์์ ํฌํธ๋ฅผ ํ๋ ์ด์ด์ ๊ทธ์ชฝ์ผ๋ก ๋ค์ด๊ฐ๊ธฐ ์ํด ๋ค์๊ณผ ๊ฐ์ ์์ ์ ์ํํ๋ค
- ๊ณต์ ๊ธฐ ์ค์ ์ ํฌํธํฌ์๋ฉ์ ์ค์ ํด ์ฃผ์๋ค. ์ด์ ๋๋ฉ์ธ ๋ค์ ๋ค์ ํฌํธ๋ฒํธ๋ฅผ ๋ถ์ฌ์ฃผ๋ฉด ์๋์ผ๋ก ๋ด ์ปดํจํฐ๋ก ๋ฆฌ๋๋ ์ ๋๋ค.
- ์ปดํจํฐ ์์์ ๊ตฌ์ฒด์ ์ธ ํ๋ก์ธ์ค๋ฅผ ์ฐพ์๊ฐ์ผ ํ๊ธฐ ๋๋ฌธ์ ms ๊ณต์๋ฌธ์ ๋ฅผ ์ฐธ์กฐํด WSL์ด ์ฌ์ฉํ๊ณ ์๋ ๋ด๋ถ IP๋ก ํฌ์๋ฉ์ ํด ์ฃผ์๋ค. โ ์ฌ๊ธฐ์์ ๋ฐ์ํ ๋ฌธ์ ์์
- tiny web server๋ฅผ ์ฌ์ ์ ํฌ์๋ฉํ ํฌํธ๋ก ์คํ์์ผฐ๊ณ , ๋จผ์ RDP๋ก ์ฐ๊ฒฐํด ๋ค์ด๊ฐ ์น ๋ธ๋ผ์ฐ์ ์์
localhost:portnum
์ผ๋ก ์ณ์ ๋ค์ด๊ฐ๋ ์ฑ๊ณต์ ์ผ๋ก ํ๋ฉด์ด ์ถ๋ ฅ๋๋ค. - ์ด์ ์๊ฒฉ์ง(๋์ )์์ ๋ณธ๊ฐ ์ฃผ์๋ฅผ ์ ๋ ฅํ๊ณ ํฌํธ๋ฅผ ํจ๊ป ๋ฃ์ด์ฃผ์๋๋ฐ... connection timeout์ด ๋์๊ณ , ํฐ๋ฏธ๋์๋ ์๋ฌด๊ฒ๋ ๋จ์ง ์์๋ค.
Catch
๋ณด๋๊น, ๋ด๊ฐ ๊ฐ์ง๊ณ ์๋ WSL ์ธ์คํด์ค๊ฐ ํ ๊ฐ๊ฐ ์๋์๋ ๊ฒ์ด๋ค. ๋ด๊ฐ ์ฌ์ฉํ๊ณ ์๋๊ฑด ์ต๊ทผ์ ๊ณผ์ ์๊ตฌ์ฌํญ์ ๋ง์ถ๊ธฐ ์ํด ์๋ก ํ๋ ์ค์นํ Ubuntu-22.04
์๊ณ , MS ๊ณต์๋ฌธ์๊ฐ ์๋ดํด์ค ๋ช
๋ น์ด wsl hostname -i
์ ๊ฒฐ๊ณผ๊ฐ๊ณผ Ubuntu-22.04
์ ํ ๋น๋ IP์ ๊ฐ์ด ์๋ก ๋ฌ๋ผ ์๋ฑํ ๊ณณ์ผ๋ก ํฌ์๋ฉ์ ํด์ค ๊ฒ์ด์๋ค.
PS>wsl -l -v
NAME STATE VERSION
* Ubuntu-22.04 Running 2
docker-desktop Stopped 2
Ubuntu Stopped 2
docker-desktop-data Stopped 2
๊ทธ๋์ Stack Exchange ๋ํ๋ฅผ ์ฐธ์กฐํด ๊ตฌ์ฒด์ ์ธ wsl ๋์คํธ๋ก๋ฅผ ๋ช
์ํ IP ์ฃผ์๋ฅผ ๋ฌ๋ผ๊ณ ํ๋ ๊ทธ์ ์์ผ ์ ๋๋ก ๋ ๊ฐ์ด ๋์๊ณ , ์ค์ ๋ก wsl ์์์ ip addr show | grep eth0
์ผ๋ก ๋์จ ์ฃผ์๊ฐ๊ณผ ์ผ์นํ๋ค.
Easy Way
๊ทธ๋ฅ VScode์์ Port๋ฅผ ์ด์ด๋ฒ๋ฆฌ๋๊น localhost์์ ๊ทธ๋ฅ ์ด๋ฆฌ๋ค... ๐ฆ