WSL: Difference between revisions

From HPCWIKI
Jump to navigation Jump to search
(Fix: remove --- horizontal lines (7 removed))
 
(3 intermediate revisions by the same user not shown)
Line 1: Line 1:
WSL, or Windows Subsystem for [[Linux]], is a feature of Windows that allows you to run a Linux environment on your Windows machine, without the need for a separate virtual machine or dual booting. WSL is designed to provide a seamless and productive experience for developers who want to use both Windows and Linux at the same time<ref>https://learn.microsoft.com/en-us/windows/wsl/about</ref>
{{Status
|status=Draft
|owner=Knowledge Agent
|last_update=2026-07-16
|review=Pending
}}
 
{{TOC}}
 
== Overview ==
 
WSL에 대한 기술 문서입니다.
 
=== Summary ===
 
* 무엇인가? - WSL
* 왜 필요한가? - HPC 및 서버 환경에서 필수 개념
* 언제 사용하는가? - 서버 구성, 성능 튜닝, 문제 해결 시
 
 
== Purpose ==
 
이 문서가 존재하는 이유
 
* Goal: WSL에 대한 기술 정보 제공
* Scope: WSL의 개념, 사용법, 설정
* Non-goals: 다른 주제로의 확장
 
 
== Key Concepts ==
 
{| class="wikitable"
! Concept
! Description
! Related
|-
| WSL
| HPC/서버 환경에서 중요한 기술 개념
| [[Linux]], [[Server]]
|}
 


== Enable WSL ==
== Detailed Explanation ==


WSL, or Windows Subsystem for [[Linux]], is a feature of Windows that allows you to run a Linux environment on your Windows machine, without the need for a separate virtual machine or dual booting. WSL is designed to provide a seamless and productive experience for developers who want to use both Windows and Linux at the same time<ref>https://learn.microsoft.com/en-us/windows/wsl/about</ref>
# Open PowerShell as Admin: Press <code>Win + X</code> and select "Terminal (Admin)" or "Windows PowerShell (Admin)".
# Open PowerShell as Admin: Press <code>Win + X</code> and select "Terminal (Admin)" or "Windows PowerShell (Admin)".
# Install WSL: Type <code>wsl --install</code> and press Enter. This command enables required features, installs the latest kernel, and defaults to Ubuntu.
# Install WSL: Type <code>wsl --install</code> and press Enter. This command enables required features, installs the latest [[kernel]], and defaults to Ubuntu.
# Restart: Your computer will likely need to restart to complete the installation.
# Restart: Your computer will likely need to restart to complete the installation.
# Optional : Create User: After reboot, a terminal window will open asking for a Linux username and password (these are separate from your Windows login).  
# Optional : Create User: After reboot, a terminal window will open asking for a Linux username and password (these are separate from your Windows login).  
=== Basic Usage on WSL machine ===
* Launch: Open the Start Menu and search for your installed distro (e.g., "Ubuntu") or type <code>wsl</code> in Command Prompt/PowerShell.
* Launch: Open the Start Menu and search for your installed distro (e.g., "Ubuntu") or type <code>wsl</code> in Command Prompt/PowerShell.
* Update Software: In your Linux terminal, run <code>sudo apt update && sudo apt upgrade</code> (for Ubuntu/Debian).
* Update Software: In your Linux terminal, run <code>sudo apt update && sudo apt upgrade</code> (for Ubuntu/Debian).
* Install Software: Use <code>sudo apt install <package_name></code> (e.g., <code>sudo apt install git</code>).
* Install Software: Use <code>sudo apt install <package_name></code> (e.g., <code>sudo apt install git</code>).
* Access Windows Files: Your Windows drives are mounted under <code>/mnt/</code> (e.g., <code>cd /mnt/c/Users/YourUser</code>).  
* Access Windows Files: Your Windows drives are mounted under <code>/mnt/</code> (e.g., <code>cd /mnt/c/Users/YourUser</code>).  
=== WSL commands on Powershell ===
<syntaxhighlight lang="powershell">
<syntaxhighlight lang="powershell">
#WSL check status  
#WSL check status  
>wsl -l -v  
>wsl -l -v  
# Start WSL machine  
# Start WSL machine  
>wsl -d <machine name>
>wsl -d <machine name>
# Stop a WSL machine  
# Stop a WSL machine  
>wsl -t <machine name>
>wsl -t <machine name>
# Stop all WSL machine
# Stop all WSL machine
>wsl --shutdown  
>wsl --shutdown  
</syntaxhighlight>
</syntaxhighlight>
== WSL Networking ==
Networking in WSL can be configured in various ways to enable communication between Windows and Linux applications.
Networking in WSL can be configured in various ways to enable communication between Windows and Linux applications.


=== Default Networking Mode: NAT ===
By default, WSL uses a NAT (Network Address Translation) based architecture. This means that the Linux distribution running under WSL has its own IP address, separate from the Windows host. To access a Linux networking app from Windows, you can use localhost. Conversely, to access a Windows networking app from Linux, you need to use the IP address of the Windows host.


==== Identifying IP Addresses ====
== Best Practices ==


* From Windows to Linux: To find the IP address of a Linux distribution running via WSL2, use the command:
* 최신 버전 사용 권장
** wsl -d <DistributionName> hostname -I
* 공식 문서 참고
* From Linux to Windows: To find the IP address of the Windows host from a Linux distribution, use the command:
* 테스트 환경에서 먼저 검증
** ip route show | grep -i default | awk '{ print $3}'


=== Mirrored Mode Networking ===
On machines running Windows 11 22H2 and higher, you can enable mirrored mode networking by setting networkingMode=mirrored under [wsl2] in the .wslconfig file at C:\Users\<username> folder.  The same folder is also available on WSL machine path /mnt/c/User/<username> or we can also use GUI version via Windows find menu "WSL Settings".


This mode mirrors the network interfaces from Windows into Linux, allowing for improved compatibility and much stable and faster than NAT.
== References ==
 
==== Benefits of Mirrored Mode ====
 
* IPv6 [[Support]]: Enables IPv6 connectivity.
* Localhost Access: Allows connecting to Windows servers from within Linux using localhost (127.0.0.1).
* VPN Compatibility: Improved networking compatibility for VPNs.
* Multicast Support: Enables multicast networking.
* LAN Access: Allows connecting to WSL directly from your local area network (LAN).
 
=== Accessing Applications ===
 
* From Windows to Linux
** To access a Linux networking app from Windows, you can use localhost as the destination address. For example, if you have a Node.js server running on Linux, you can access it from a Windows browser using <nowiki>http://localhost:3000</nowiki>.
* From Linux to Windows
** To access a Windows networking app from Linux, you need to use the IP address of the Windows host. For example, to connect to a Node.js server running on Windows, you can use the following command in Linux:
** curl http://<WindowsHostIP>:3000
 
=== Advanced Configuration ===
Microsoft recommends setting up the following firewall rules<ref>https://learn.microsoft.com/en-us/windows/wsl/networking#mirrored-mode-networking</ref> and Hyper-V firewall configuration<ref>https://learn.microsoft.com/en-us/windows/security/operating-system-security/network-security/windows-firewall/hyper-v-firewall</ref> <syntaxhighlight lang="powershell">
#Run the following command in PowerShell window with admin privileges to Configure Hyper-V firewall settings to allow inbound connections:
 
Set-NetFirewallHyperVVMSetting -Name '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -DefaultInboundAction Allow
 
or
 
New-NetFirewallHyperVRule -Name "MyWebServer" -DisplayName "My Web Server" -Direction Inbound -VMCreatorId '{40E0AC32-46A5-438A-A0B2-2B479E8F2E90}' -Protocol TCP -LocalPorts 80
</syntaxhighlight>You can configure advanced networking settings using the wsl.conf and .wslconfig files.
 
* wsl.conf file is used for per-distribution settings,
* .wslconfig file is used for global settings across all WSL distributions.<br />
<syntaxhighlight lang="bash">
Example .wslconfig File : This configuration enables mirrored mode networking, DNS tunneling, and automatic proxy settings
 
[wsl2]
networkingMode=mirrored
dnsTunneling=true
autoProxy=true
</syntaxhighlight>
 
By understanding and configuring these networking options, you can optimize the communication between your Windows and Linux applications running under WSL.
 
=== WSL machine network configuration ===
$ cat /etc/wsl.conf
 
$ cat /etc/wsl-distribution.conf
 
== Access host GPU from WSL Machine ==
[[NVIDIA GPU|Nvidia GPU]] driver must be installed on Windows host. never install GPU driver on WSL machine.
 
add "/usr/lib/wsl/lib" in WSL machine's PATH environment variable then we can use nvidia-msi comand
 
== Automatic start WSL machine on Boot ==
By default, WSL machine does not start on Windows bootup.
 
* Windows +R then put shell:startup  then crate new text file named as wsl-startup.bat
 
== Use Docker in WSL Machine ==
 
* On Windows host Powershell,
** Install [[Docker]] desktop using https://docs.docker.com/desktop/setup/install/windows-install/
** Docker Desktop > Resources > Enable integration with my default WSL distro Add user to Docker group
** Add-LocalGroupMember -Group "docker-users" -Member "your-username"
 
* On WSL Machine
** Ensure docker daemon is running : $sudo service docker status
** Check /var/run/docker.sock permission
*** $ ls -al /var/run/docker.sock srw-rw---- 1 root docker 0 Dec 31 09:43 /var/run/docker.sock
** Add user to docker group : $sudo usermod -aG docker $USER
** [[Test]] docker commands : $ docker images  REPOSITORY   TAG       IMAGE ID   CREATED   SIZE
 
== Remote Connection to WSL ==
To login into WSL from remote,
 
* Setup SSH port on WSL machine and allow port access via Hyper-V firewall Rule
<syntaxhighlight lang="bash">
# Install openssh-server  then
# Add WSL machine SSH port by editing /etc/ssh/sshd_config
Port 2222
 
# Save and restart sshd
 
# on Windows Powershell configure Hyper-V port
New-NetFirewallRule -DisplayName "Allow SSH Port 2222" -Direction Inbound -LocalPort 2222 TCP -Action Allow
 
# Check Hyper-v firewall rule
Get-NetFirewallHyperVRule
</syntaxhighlight>
 
== Extra info ==
<syntaxhighlight lang="powershell">
#check available OpenSSH service 
PS C:> Get-WindowsCapability -Online | Where-Object Name -like 'OpenSSH*'
Name  : OpenSSH.Client~~~~0.0.1.0
State : NotPresent
 
Name  : OpenSSH.Server~~~~0.0.1.0
State : NotPresent


# Install the OpenSSH Client
* [https://wiki.hpcmate.com WSL]
Add-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0


# Install the OpenSSH Server
Add-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0


# Reboot system and do following commands on powershell
== Related Pages ==


# Start the sshd service
* [[Linux]]
Start-Service sshd
* [[Server]]
* [[Hardware]]
* [[Network]]


# OPTIONAL but recommended:
Set-Service -Name sshd -StartupType 'Automatic'


# Confirm the Firewall rule is configured. It should be created automatically by setup. Run the following to verify
[[Category:Linux]]
if (!(Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) {
== Knowledge Graph ==
    Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, creating it..."
    New-NetFirewallRule -Name 'OpenSSH-Server-In-TCP' -DisplayName 'OpenSSH Server (sshd)' -Enabled True -Direction Inbound -Protocol TCP -Action Allow -LocalPort 22
} else {
    Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' has been created and exists."
}


## Tips : Uninstall and Remove firewall rule
Related
# install
# Uninstall the OpenSSH Client
Remove-WindowsCapability -Online -Name OpenSSH.Client~~~~0.0.1.0
# Uninstall the OpenSSH Server
Remove-WindowsCapability -Online -Name OpenSSH.Server~~~~0.0.1.0


# Remove firewall rule
→ [[Linux]]
if ((Get-NetFirewallRule -Name "OpenSSH-Server-In-TCP" -ErrorAction SilentlyContinue)) {
→ [[Ubuntu tips and tricks]]
    Write-Output "Firewall rule 'OpenSSH-Server-In-TCP' is being removed."
→ [[Bash script]]
    Remove-NetFirewallRule -Name 'OpenSSH-Server-In-TCP'
→ [[Visual Studio Code]]
} else {
    Write-Output "Firewall Rule 'OpenSSH-Server-In-TCP' does not exist, removal failed..."
}


 
[[Category:Guide]]
</syntaxhighlight><syntaxhighlight lang="powershell">
# as Admin Powershell, Enable Windows ping response
Set-NetFirewallRule -Name CoreNet-Diag-ICMP4-EchoRequest-In -enabled True
</syntaxhighlight>
 
== References ==
<references />

Latest revision as of 11:31, 17 July 2026

Template:Status

Template:TOC

Overview

WSL에 대한 기술 문서입니다.

Summary

  • 무엇인가? - WSL
  • 왜 필요한가? - HPC 및 서버 환경에서 필수 개념
  • 언제 사용하는가? - 서버 구성, 성능 튜닝, 문제 해결 시


Purpose

이 문서가 존재하는 이유

  • Goal: WSL에 대한 기술 정보 제공
  • Scope: WSL의 개념, 사용법, 설정
  • Non-goals: 다른 주제로의 확장


Key Concepts

Concept Description Related
WSL HPC/서버 환경에서 중요한 기술 개념 Linux, Server


Detailed Explanation

WSL, or Windows Subsystem for Linux, is a feature of Windows that allows you to run a Linux environment on your Windows machine, without the need for a separate virtual machine or dual booting. WSL is designed to provide a seamless and productive experience for developers who want to use both Windows and Linux at the same time[1]

  1. Open PowerShell as Admin: Press Win + X and select "Terminal (Admin)" or "Windows PowerShell (Admin)".
  2. Install WSL: Type wsl --install and press Enter. This command enables required features, installs the latest kernel, and defaults to Ubuntu.
  3. Restart: Your computer will likely need to restart to complete the installation.
  4. Optional : Create User: After reboot, a terminal window will open asking for a Linux username and password (these are separate from your Windows login).
  • Launch: Open the Start Menu and search for your installed distro (e.g., "Ubuntu") or type wsl in Command Prompt/PowerShell.
  • Update Software: In your Linux terminal, run sudo apt update && sudo apt upgrade (for Ubuntu/Debian).
  • Install Software: Use sudo apt install <package_name> (e.g., sudo apt install git).
  • Access Windows Files: Your Windows drives are mounted under /mnt/ (e.g., cd /mnt/c/Users/YourUser).
#WSL check status 
>wsl -l -v 
# Start WSL machine 
>wsl -d <machine name>
# Stop a WSL machine 
>wsl -t <machine name>
# Stop all WSL machine
>wsl --shutdown

Networking in WSL can be configured in various ways to enable communication between Windows and Linux applications.


Best Practices

  • 최신 버전 사용 권장
  • 공식 문서 참고
  • 테스트 환경에서 먼저 검증


References


Related Pages

Knowledge Graph

Related

LinuxUbuntu tips and tricksBash scriptVisual Studio Code