WSL Ubuntu 부팅 시 SSH 자동 시작 + 바로가기 아이콘 만들기

오늘은 내 PC에서 아이콘을 클릭하여 WSL의 우분투를 실행하고, 자동으로 SSH 서버를 시작하도록 설정하는 방법을 단계별로 안내드릴게요. 전체 흐름은 다음과 같습니다:

  1. WSL 우분투에서 SSH 서버 설치 및 설정
  2. WSL 우분투 실행 시 자동으로 SSH 서버가 켜지도록 설정
  3. 바탕화면이나 시작 메뉴에 아이콘 생성

1. WSL 우분투에서 SSH 서버 설치 및 설정

지난 시간에 우리는 우분투에 SSH를 설치하고 원격에서 접속하는 방법을 구성하였습니다.
아래 포스팅을 참고해주세요.


2. WSL 실행 시 SSH 자동 실행 설정

WSL을 실행하고 “su” 명령어를 통해 root 권한을 획득하고 SSH 서비스를 활성화합니다.

#ssh 실행여부 확인
systemctl status ssh

#ssh 서비스 활성화
systemctl enable ssh.service

이제 WSL이 실행되면 자동으로 SSH 데몬이 활성화 됩니다.


3. 바탕화면에 아이콘으로 WSL 실행하도록 설정

내가 WSL 프로그램을 실행하고 싶을 때, 아이콘 하나만을 클릭해서 자동으로 실행하도록 설정해보도록 하겠습니다.

먼저 지난번에 설정했던 PowerShellScript 에 내용을 추가해서 IP 동기화와 동시에 WSL이 실행되는 결과를 출력하겠습니다.

PowerShellScript 실행:

#현재 설치된 배포판 확인
wsl -l -v

  NAME            STATE           VERSION
* Ubuntu-24.04    Running         2

지난 번 작성했던 PowerShellScript 파일 마지막 라인에 다음 코드 추가:

# 우분투 실행
wsl -d Ubuntu-24.04

전체 코드 (script.ps1)

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 = @(2222);

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";

# 우분투 실행
wsl -d Ubuntu-24.04

바탕화면에 바로가기 아이콘 추가:

  1. 바탕화면 마우스 우클릭 > 새로 만들기 > 바로 가기

항목 위치를 다음과 같이 입력:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "실행할 스크립트 파일의 경로"

예시)
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -ExecutionPolicy Bypass -File "C:\PowerShellScript\script.ps1"

마무리하며

지금까지 WSL 우분투에서 SSH 서버를 자동으로 실행하고, 바탕화면 아이콘으로 간편하게 실행하는 방법을 알아봤습니다.
작은 설정 하나로도 개발 환경이 훨씬 편리해질 수 있죠.
궁금한 점이나 더 다뤄줬으면 하는 내용이 있다면 댓글로 남겨주세요! 😊
읽어주셔서 감사합니다.

Leave a Comment