# [Record] Install Linux on Windows with WSL ###### tags: `docker`, `WSL` ,`WSL2` [toc] ## System info Edition: Windows 11 Home Version: 21H2 OS build: 22000.1335 ## Procedure 1. Run Windows PowerShell as administrator 2. Install and enable hyper-v by running the script Install Linux on Windows with WSL {%gist anderson-tomas/e9127aea7388c3a978d7c5415a156eb6 %} 2. [Download](https://desktop.docker.com/win/main/amd64/Docker%20Desktop%20Installer.exe) and install Docker Desktop for Windows 3. Install linux subsystem (WSL) by run "Windows PowerShell" as administrator ``` Windows PowerShell Copyright (C) Microsoft Corporation. All rights reserved. Install the latest PowerShell for new features and improvements! https://aka.ms/PSWindows PS C:\Windows\system32> wsl --install ``` 4. [Download](https://wslstorestorage.blob.core.windows.net/wslblob/wsl_update_x64.msi) WSL2 and install it ## Share folders We can access all files in ubuntu from windows. Just open the file explorer (hotkey: win+e) in wondows with below path. ```bash \\wsl$\Ubuntu\home\username\ ``` ## Enable systemd ```shell sudo -b unshare --pid --fork --mount-proc /lib/systemd/systemd --system-unit=basic.target sudo -E nsenter --all -t $(pgrep -xo systemd) runuser -P -l $USER -c "exec $SHELL" ``` ## Enable SSH We got an error message after modifying /etc/ssh/sshd_config and restarting ssh. ### Error ```shell sudo service ssh restart sshd: no hostkeys available ``` ### Solution ```shell sudo ssh-keygen -t dsa -f /etc/ssh/ssh_host_dsa_key sudo ssh-keygen -t rsa -f /etc/ssh/ssh_host_rsa_key chmod 600 ssh_host_dsa_key chmod 600 ssh_host_rsa_key sudo service ssh restart ``` ## Move WSL to other drive - The Windows Subsystem for Linux only runs on your system drive (usually this is your C: drive). Make sure that distributions are stored on your system drive: 1. Windows 10 open Settings -> System -> Storage -> More Storage Settings: Change where new content is saved ![](https://hackmd.io/_uploads/rJHzOTui2.png) 2. On Windows 11 open Settings -> System -> Storage -> Advanced storage settings -> Where new content is saved ![](https://hackmd.io/_uploads/HkImdpus2.png) - To move WSL from C drive to D drive On Windows 11 open Settings -> Apps -> Apps & features -> Ubuntu on Windows -> Move ![](https://hackmd.io/_uploads/Hkixqpuon.png) And you will see the error ![](https://hackmd.io/_uploads/HJPh96Oon.png) ### If distribution is not installed Run this command in Powershell which opens the install location of that Windows Store package. ``` Explorer.exe (Get-AppxPackage *Ubuntu*).InstallLocation ``` Replace Ubuntu with your installed distribution name. For Ubuntu 18.04 the folder path is: ``` C:\Program Files\WindowsApps\CanonicalGroupLimited.Ubuntu18.04onWindows_1804.2019.522.0_x64__79rhkp1fndgsc ``` Copy the install.tar.gz and ubuntu1804.exe (or other name) where you want to install. Run ubuntu1804.exe which will install the distribution. This may take some time. After successful installation, there will be a rootfs and temp folder. ### If distribution is installed: This method only works in Windows 10 Build 18305 or above. Close any running instances of that WSL distribution. List the installed distribution to get distribution name: ``` wsl.exe --list --all ``` Export the required distribution to a tar.gz file: ``` wsl.exe --export Ubuntu-18.04 D:\MyFolder\MyFile.tar.gz ``` Import that same file but with different path and different name: ``` wsl.exe --import MyDistro D:\MyFolder D:\MyFolder\MyFile.tar.gz ``` Launch the distribution: ``` wsl.exe --distribution MyDistro ``` ## Usefull commands in Windows PowerShell ### List available Linux distributions available for download through the online store ``` wsl --list --online wsl -l -o ``` ### Install WSL command ``` wsl --install -d <Distribution Name> ``` ### List all Linux distros installed on WSL ``` wsl -l –running wsl --list --running wsl --list --all ``` ### Unregister or uninstall a Linux distribution ``` wsl --unregister <DistributionName> ``` ### Change the default user for a distribution ``` ubuntu config --default-user tomas ``` ### Set default Linux distribution ``` wsl --set-default <Distribution Name> ``` ## Logs in Windows PowerShell ```bash PS C:\Users\tomas> wsl --list --online Use 'wsl --install -d <Distro>' to install NAME FRIENDLY NAME Ubuntu Ubuntu Debian Debian GNU/Linux kali-linux Kali Linux Rolling Ubuntu-18.04 Ubuntu 18.04 LTS Ubuntu-20.04 Ubuntu 20.04 LTS Ubuntu-22.04 Ubuntu 22.04 LTS OracleLinux_7_9 Oracle Linux 7.9 OracleLinux_8_7 Oracle Linux 8.7 OracleLinux_9_1 Oracle Linux 9.1 openSUSE-Leap-15.5 openSUSE Leap 15.5 SUSE-Linux-Enterprise-Server-15-SP4 SUSE Linux Enterprise Server 15 SP4 SUSE-Linux-Enterprise-15-SP5 SUSE Linux Enterprise 15 SP5 openSUSE-Tumbleweed openSUSE Tumbleweed PS C:\Windows\system32> wsl --version WSL version: 1.0.3.0 Kernel version: 5.15.79.1 WSLg version: 1.0.47 MSRDC version: 1.2.3575 Direct3D version: 1.606.4 DXCore version: 10.0.25131.1002-220531-1700.rs-onecore-base2-hyp Windows version: 10.0.22000.1335 PS C:\Users\tomas> wsl -l -v NAME STATE VERSION * Ubuntu Running 2 PS C:\Users\tomas> wsl --list --running Windows Subsystem Linux Distributions: Ubuntu (Default) PS C:\Users\tomas> wsl --list --all Windows Subsystem Linux Distributions: Ubuntu (Default) PS C:\Users\tomas> wsl --unregister Ubuntu ``` ## Reference https://learn.microsoft.com/en-us/windows/wsl/install https://blog.hungwin.com.tw/windows-home-hyper-v/ https://docs.docker.com/desktop/install/windows-install/ https://learn.microsoft.com/zh-tw/windows/wsl/install https://hackmd.io/@Kailyn/H1N5OPKlF https://zhuanlan.zhihu.com/p/552307629 https://www.ruyut.com/2022/09/windows-11-install-docker.html https://askubuntu.com/questions/1379425/system-has-not-been-booted-with-systemd-as-init-system-pid-1-cant-operate https://github.com/MicrosoftDocs/wsl/blob/main/WSL/basic-commands.md https://learn.microsoft.com/en-us/windows/wsl/troubleshooting#installation-issues https://superuser.com/questions/1443995/how-to-move-ubuntu-on-windows-to-drive-d-or-other-drive https://pureinfotech.com/remove-linux-distro-wsl/ https://learn.microsoft.com/en-us/windows/wsl/basic-commands