M1 Macbook으로 WSL에 원격 접속하기

sshinohs·2023년 2월 23일
0

1. 기기 IP 확인

2. wsl 환경 준비

openssh-server 재설치

sudo apt remove openssh-server
sudo apt update
sudo apt install openssh-server

sshd_config 파일 수정

sudo vi /etc/ssh/sshd_config
Port 22 # 주석처리 되어 있으면 주석 해제, 기본 SSH Port 번호는 22, 보안을 위해서 다른 번호로 변경 추천
PasswordAuthentication no를
PasswordAuthentication yes로 변경
sudo service ssh --full-restart

ssh service를 비밀번호 없이 시작할 수 있도록 설정

sudo visudo
# 열린 파일 가장 아래에 구문 추가하고 저장
%sudo ALL=NOPASSWD: /usr/sbin/service

3. ssh service를 windows 시작 시 자동으로 시작하도록 등록

  • 메모장으로 아래 내용을 입력하고, sshd.bat으로 저장
@echo off
"C:\Users\SHIN\AppData\Local\Microsoft\WindowsApps\ubuntu2204.exe" -c "sudo service ssh start"
  • windows + r, shell:startup 실행 후 뜨는 시작프로그램 폴더에 sshd.bat 삽입

4. Port-Forwarding Script 작성 (to WSL IP:Port)

$remoteport = ubuntu2204.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,5000,22);

#[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";
  • ubuntu2204.exe 부분은 본인의 wsl 실행파일에 맞게 변경
  • 위 내용 중, 아래의 22를 임의로 설정한 ssh port 번호로 변경
# from
$ports=@(80,443,5000,22);
# to
$ports=@(80,443,5000,23456);
  • '임의로지정한파일명.ps1'으로 저장

5. 작업 스케줄러 및 ExecutionPolicy 지정

https://jackcokebb.tistory.com/18

6. Script 직접 돌려보기

https://jackcokebb.tistory.com/18

7. 접속 테스트

https://jackcokebb.tistory.com/18

8. Font 설정

  • MesloLGF NF 폰트 다운로드 하여 설치(regular만 설치)
  • VSCode의 terminal integrated font family에 아래 입력
MesloLGS NF

9. warning: setlocale: LC_CTYPE: cannot change locale (UTF-8)

** 윈도우 부팅 시 4.가 자동으로 작동되지 않음, 왜 그럴까?

References

profile
sshinohs

0개의 댓글