Skip to content

Latest commit

 

History

History

wsl2

Folders and files

NameName
Last commit message
Last commit date

parent directory

..
 
 

WSL 2 Setup

Prerequisite

For Win10_2004 or earlier Enable the WSL feature with admin pwsh:
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart

or

Enable-WindowsOptionalFeature -Online -FeatureName Microsoft-Windows-Subsystem-Linux

Set wsl2 as default:

WSL --set-default-version 2

For Win10_2004 or later

  • Install dependent packages, set wsl2 as default and install the latest Ubuntu:

    WSL --install
  • To select a specific distro:

    WSL --list --online # list all available distro
    WSL --install -d <DistroName>

Get WSL distro exe file

Go to MSStore and click install, but no need to set your username/password in that distro.

  • Keep the exe file with unregister, rather than uninstall it:

    WSL --list all # show all your distro in your pc
    WSL --unregister <DistroName>

Install on non-system drive

  • Export WSL distro as .tar file:

    WSL --export <DistroName> /path/to/Ubuntu.tar
  • Import it at another drive:

    WSL --import <DistroName> /path/to/distro-system /path/to/Ubuntu.tar

    for example:

    WSL --export WLinux WLinux.tar
    
    WSL --export WLinux WLinux.tar
    
    WSL --import WLinuxDev    .\WLinuxDev    WLinux.tar --version 2
    WSL --import WLinuxGo     .\WLinuxGo     WLinux.tar --version 2
    WSL --import WLinuxDotNet .\WLinuxDotNet WLinux.tar --version 2

File accessing

  • Windows -> WSL 2

    In win explorer, use \\WSL$\<DistroName> to access the files in WSL.

    ※ Noted: Every wsl2 distro's virtual disk space is 256GB. If you exceed 256GB, there'll be error. Check Expand the size of your WSL 2 Virtual Hard Disk to adjust the disk space.

  • WSL 2 -> Windows

    Use /mnt/<DRIVE> to access any files in Windows. For example, C:\Projects in Windows equals /mnt/c/Projects in WSL.

    Also, you can call Windows explorer in WSL:

    cd ~
    explorer.exe .

Web service accessing

  • Windows -> WSL 2

    For any web services running in WSL, just use localhost in Windows' browser to access it.

  • WSL 2 -> Windows

    For any outbound web services running in Windows, you can use the IPv4 address in Ethernet adapter vEthernet (WSL) and access the web services via that ip.

    • 2 ways to get the WSL's ip:

      • ipconfig's IPv4 Address in Windows' command line:

        $ ipconfig
        Ethernet adapter vEthernet (WSL):
        
        Connection-specific DNS Suffix  . :
        Link-local IPv6 Address . . . . . : fe80::99e5:4b5:5d8a:7eeb%39
        IPv4 Address. . . . . . . . . . . : 172.19.239.1
        Subnet Mask . . . . . . . . . . . : 255.255.240.0
        Default Gateway . . . . . . . . . :
      • cat /etc/resolv.conf's nameserver in WSL:

        $ cat /etc/resolv.conf
        # This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/WSL.conf:
        # [network]
        # generateResolvConf = false
        nameserver 172.19.239.1

    ※ Noted: To Windows, this type of accessing is public network accessing. So make sure the Inbound Rules in Windows firewall(wf.msc) isn't blocking public network accessing.

    You can set a $local alias in WSL's ~/.profile to avoid searching for ip every time:

    export local=$(cat /etc/resolv.conf | grep nameserver | cut -d ' ' -f 2)

Some useful WSL command

`wslconfig` Usage:
  /l, /list [/all] - Lists registered distributions.
  /all - Optionally list all distributions, including distributions that
         are currently being installed or uninstalled.
  /s, /setdefault <DistributionName> - Sets the specified distribution as the default.
  /u, /unregister <DistributionName> - Unregisters a distribution.
# Find out all the WSL installations in your Windows system:
$ wslconfig /l
Windows Subsystem for Linux Distributions:
Ubuntu-22.04 (Default)
# Set default distribution:
$ wslconfig /setdefault Ubuntu-22.04
# To uninstall a distribution:
$ wslconfig /u <DistroName>
# Shutdown specific WSL Distro
$ wsl -t <DistroName>
# Shutdown all running WSL instances
$ wsl --shutdown
# Login to specific Distro
$ wsl -d <DistroName>
# Login to the Distro in specific username, password free.
$ wsl -d <DistroName> -u root
# List installed WSL distro
$ wsl -l
# List installed WSL distro, it's status and WSL version
$ wsl -l -v
  NAME                   STATE           VERSION
  WLinux                 Stopped         2
  docker-desktop         Stopped         2
  docker-desktop-data    Stopped         2
* Ubuntu-20.04           Running         1
  Ubuntu-18.04           Stopped         1
# List all downloaded distro, including those unregistered one.
$ wsl -l --all
# List all running WSL instances.
wsl -l --running
# remove all files in the Distro and uninstall the Distro.
# Be careful! You can't undo this action.
$ wsl --unregister <DistroName>

Bring systemd back to WSL!

By this installation, systemd is enabled in your WSL 2 distro.

  1. Download and run the latest installer script.

    curl -L -O "https://raw.githubusercontent.com/nullpo-head/WSL-distrod/main/install.sh"
    chmod +x install.sh
    sudo ./install.sh install

    This script installs distrod, but doesn't enable it yet.

  2. Enable distrod in your distro

    You have two options. If you want to automatically start your distro on Windows startup, enable distrod by the following command

    sudo /opt/distrod/bin/distrod enable --start-on-windows-boot

    Otherwise,

    /opt/distrod/bin/distrod enable

    You can run enable with --start-on-Windows-boot again if you want to enable autostart later.

  3. Restart your distro

    Close your WSL's terminal. Open a new Command Prompt window, and run the following command.

    wsl --terminate Distrod

    After re-opening a new WSL window, your shell runs in a systemd session.

Update Distrod

  1. Inside a Distrod session, download and run the latest installer script.

    curl -L -O "https://raw.githubusercontent.com/nullpo-head/WSL-distrod/main/install.sh"
    chmod +x install.sh
    sudo ./install.sh update

Some useful setup

Git

git config --global user.name Charles
git config --global user.email [email protected]
git config --global core.editor nvim
git config --global core.autocrlf false
git config --global core.quotepath false
git config --global help.autocorrect 30
git config --global color.diff auto
git config --global color.status auto
git config --global color.branch auto

Editor

In .bashrc or .profile

# set vim as default editor
sudo update-alternatives --set editor /usr/bin/vim.basic
export EDITOR=vim

ssh-key

ssh-keygen -C "[email protected]"
touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

password-free sudo

echo "<USER> ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/<USER>

localhost alias

In .bashrc or .profile

export mylocalhost=$(cat /etc/resolv.conf | grep nameserver | cut -d ' ' -f 2)

View images in terminal (kitty+ranger)

kitty-on-windows-with-wsl2

Enter $HOME dir

If you want to be at the $HOME dir when entering WSL.

if [ "${PWD:-}" = "/mnt/c/Users/$USER" ]; then
  cd ~
fi

Safer rm

sudo apt install trash-cli
# add alias to .bashrc
alias rm='trash'
# use trash-list & restore-trash to restore file

Bugs

  • Warning: Missing charsets in String to FontSet conversion, ref

    Set this in .bashrc:

    export LANG=C

Reference

  1. Windows Subsystem for Linux Documentation
    1. Working across Windows and Linux file systems
    2. Advanced settings configuration in WSL)
    3. File Permissions for WSL
    4. Accessing network applications with WSL
    5. Windows Server Installation Guide
    6. Release Notes for Windows Subsystem for Linux
  2. Set up your development environment on Windows
  3. Export and Import WSL Linux Distro in Windows 10
  4. microsoft/WSL github repo
  5. MicrosoftDocs/WSL
  6. wsl-distrod
  7. Generating a new SSH key
  8. Adding your SSH key to the ssh-agent
  9. 使用 WSL 2 打造優質的多重 Linux 開發環境