FOREST_CHOI's BLOG

Mac에서 Window WSL 접속하기 본문

운영/Linux

Mac에서 Window WSL 접속하기

Forest_Choi 2023. 2. 6. 10:58
728x90

일단 wsl에서 openssh-server를 재설치 해줘야한다.

 

sudo apt remove openssh-server

sudo apt update -y && sudo apt upgrade -y

sudo pat install openssh-server

 

그다음 sshd_config 파일을 수정해주어야한다.(Sudo로 들어가서 수정해 주어야한다.)

 

 

sudo vi /etc/ssh/sshd_config

Port 22

PasswordAuthentication yes

 

위 두가지 줄을 주석 해제한다.

 

이후 ssh 서버를 재시작한다.

 

sudo service ssh --full-restart

 

 

다음 윈도우에서 

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 {
  Write-Output "The Script Exited, the ip address of WSL 2 cannot be found";
  exit;
}

$ports = @(3000, 3001, 5000, 5500, 19000, 19002, 19006, 22);

Invoke-Expression "netsh interface portproxy reset";

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

Invoke-Expression "netsh interface portproxy show v4tov4";

위에 스크립트를 ps1 이라는 확장자로 저장한 후 실행한다.

ex) port_wsl.ps1 -> .\port_wsl.ps1

 

--> 실행이 안될떄가 있는데 이떄 Set-ExecutionPolicy RemoteSigned 를 수행하오 모두 예를 선택해준다.

 

그리고 맥에서

ssh { wsl id } @ { wsl ip } -p {port}

를 실행하면 접속이 된다.

 

** 원도우를 껐다켜면 ssh를 수동으로 실행해줘야하는것 같다. -> 자동화할수있을듯?

728x90
Comments