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
-
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>
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>
-
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
-
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 .
-
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
'sIPv4 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
'snameserver
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 theInbound 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)
-
`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.
-
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.
-
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. -
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.
-
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
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
In .bashrc
or .profile
# set vim as default editor
sudo update-alternatives --set editor /usr/bin/vim.basic
export EDITOR=vim
ssh-keygen -C "[email protected]"
touch ~/.ssh/authorized_keys
chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys
echo "<USER> ALL=(ALL) NOPASSWD:ALL" | sudo tee /etc/sudoers.d/<USER>
In .bashrc
or .profile
export mylocalhost=$(cat /etc/resolv.conf | grep nameserver | cut -d ' ' -f 2)
If you want to be at the $HOME
dir when entering WSL.
if [ "${PWD:-}" = "/mnt/c/Users/$USER" ]; then
cd ~
fi
sudo apt install trash-cli
# add alias to .bashrc
alias rm='trash'
# use trash-list & restore-trash to restore file
-
Warning: Missing charsets in String to FontSet conversion
, refSet this in
.bashrc
:export LANG=C