installing wsl and setting

322 阅读2分钟

操作指南

enable-feature

# need admin right
dism.exe /online /enable-feature /featurename:Microsoft-Windows-Subsystem-Linux /all /norestart
dism.exe /online /enable-feature /featurename:VirtualMachinePlatform /all /norestart

# reboot machine
Restart-Computer 

update linux package

# update linux package
wsl --update

set wsl version

# set wsl version
wsl --set-default-version 2

list linux distribution online

# list linux distribution 
# only you know how to access the extranet in China, can it show correct result with next line: 
wsl --list --online

# today, you can choose one of them:
# Debian                                 Debian GNU/Linux
# Ubuntu-18.04                           Ubuntu 18.04 LTS
# Ubuntu-20.04                           Ubuntu 20.04 LTS
# Ubuntu-22.04
# ...

install linux distribution

# install 
# wsl --install -d <Distribution Name>
# Run as administrator
# will ask for user and password
# $linux_distibution="Ubuntu-20.04"
$linux_distibution="Ubuntu-22.04"
wsl --install -d $linux_distibution

# exit wsl
# exit

get wsl info you are running

# get wsl info you are running
wsl -l -v

set wsl memory and others

# this is optional.
# set wsl memory and others
$text=@"
[wsl2]
processors=8
memory=8GB
swap=8GB
localhostForwarding=true
"
set-conent -Path ~/.wslconfig -Value $text

to check

set wsl installed position to custom diretory

# task:s:change installed position
# shutdown -> export -> unregister -> import

$linux_distibution="Ubuntu-22.04"
$wsl_ms_name="Ubuntu_2204"
$wsl2_dir="D:\10_WSL2"

# make a std new name
$wsl_ms_name=$linux_distibution.replace("-","_").replace('.','').tolower()

# make dirs
$null=mkdir -p "${wsl2_dir}\${linux_distibution}" -ErrorAction SilentlyContinue

# cache ms dir in var
# $wsl_ms_dir=${wsl2_dir}
$wsl_ms_dir="${wsl2_dir}\${linux_distibution}"

# shutdown
wsl --shutdown

# export
wsl --export $linux_distibution "${wsl2_dir}\${linux_distibution}.tar"
# wait some min

# unregister
wsl --unregister $linux_distibution

# import
wsl --import $wsl_ms_name ${wsl_ms_dir}\ "${wsl2_dir}\${linux_distibution}.tar" --version 2
# wait some min

# task:e:change installed position

set default wsl machine

# switch wsl
# wslconfig /setdefault <系统名称>
# wslconfig /setdefault <DistributionName>
# wslconfig /setdefault ubuntu_2204
wslconfig /setdefault $wsl_ms_name

delete wsl machine

# delete
# wsl --unregister <DistributionName>
# wsl --unregister Ubuntu-22.04
# wsl --unregister Ubuntu_2204
wsl --unregister $wsl_ms_name

start wsl machine

# start and login with default user and default machine name
wsl

# start and login with spec user and default machine name
# wsl -u root
# wsl -u zero

# start and login with spec user and spec machine name
# wsl -u zero -d ubuntu_2204

set line-ending between win and wsl ( when using git)

# in win
# scoop install git
git config --global core.autocrlf input
# in wsl
# set line eof in between win and wsl for git
# sudo apt install git -y
git config --global core.autocrlf input

all in one on window

git config --global core.autocrlf input

# window run cmd on remote wsl
wsl git config --global core.autocrlf input

sharing git credential between win and wsl ( when using git)

git config --global credential.helper wincred

# scoop prefix git
# share git credential of window to wsl
# apt-get install git
# git config --global user.name "Your Name"
# git config --global user.email "youremail@domain.com"
# git config --global credential.helper "/mnt/c/Program\ Files/Git/mingw64/libexec/git-core/git-credential-manager.exe"

# git config --global credential.helper "/mnt/d/CustomSoftware/windows/Scoop/apps/git/current/mingw64/libexec/git-core/git-credential-manager.exe"

to check

set systemd hacks in wsl

# if current wsl do no support systemd.
# enable systemd support
sudo apt install git -y
git clone https://github.com/DamionGans/ubuntu-wsl2-systemd-script.git
cd ubuntu-wsl2-systemd-script/
bash ubuntu-wsl2-systemd-script.sh

set wsl with static ip

give wsl a static ip (todo)

set wsl hosts file

edit-hosts-file (todo)

give wsl a custom domain (todo)

design custom domain in my developing enviroenment (todo)

access localhost ports from Windows

  • have wsl.local automatically resolve to the WSL2 VM
# install it
$ghproxy="https://ghproxy.com/"
$url="${ghproxy}https://github.com/shayne/go-wsl2-host/releases/download/v0.3.5/wsl2host.exe"
$output = ".\wsl2host.exe"

$start_time = Get-Date
(New-Object System.Net.WebClient).DownloadFile($url, $output)
Write-Output "Time taken: $((Get-Date).Subtract($start_time).Seconds) second(s)"
# admin right
.\wsl2host.exe install
# Windows Username: <username-you-use-to-login-to-windows>
# Windows Password: <password-for-this-user>

  • To make these dev servers / ports accessible you can run the following commands, or add them to the /etc/rc.local if you have systemd running:
# /etc/rc.local runs as root by default
# if you run these yourself add 'sudo' to the beginning of each command

sysctl -w net.ipv4.conf.all.route_localnet=1
iptables -t nat -I PREROUTING -p tcp -j DNAT --to-destination 127.0.0.1 

to check

set wsl ubuntu host name

changing wsl ubuntu host name

USER_NAME=wsl;

# download -> add-x-right -> run-sh
todir=./tool/wslubt-use-host-name; 
tof="$todir/index.sh";
gcproxy="https://ghproxy.com/";
uri="${gcproxy}https://raw.githubusercontent.com/ymc-github/wslubt-use-host-name/main/index.sh";  
mkdir -p "$todir"; curl -o "$tof" -s $uri; chmod +x "$tof"; "$tof" $USER_NAME; 

# del-sh:
# rm -rf "$todir";

add user on wsl (ubuntu)

# useradd -m zero -s /bin/bash

# window run remote cmd on wsl machine
wsl -d ubuntu_2004 useradd -m zero -s /bin/bash

use no-root user to do sth.

# install wsl -> add custom user -> login with custom user -> do sth. with custom user

# do sth.
# eg. run docker with no root user
# eg. use podman replace with docker
# ...

参考文献

install wsl

WSL2 Ubuntu+K8s+Docker

wsl usage exp

replacing-strings-in-powershell

shayne/wsl2-hacks: Useful snippets / tools for using WSL2 as a development environment (github.com)

下篇阅读

developing in k8s in wsl