WSL : Windows Subsystem for Linux

Install

PS> wsl --install

Install a distro either from Microsoft Store, or direcly from PS or CMD command line using wsl utility:

# List all distros available for installation
wsl --list --online 
# Install a distro
wsl --install openSUSE-Leap-15.5
# List installed distros
wsl --list --verbose
# Set a default distro
wsl --set-default Ubuntu-22.04
# Launch default distro
wsl

Configure a WSL2 distro

  1. Edit /etc/wsl.conf to configure mount points as /<DRIVE> instead of /mnt/<DRIVE> .

    [boot]
    systemd=true
    
    [automount]
    root = /
    options = "metadata,umask=22,fmask=11"
    
    • To take effect, must close WSL terminal, then run wsl --shutdown from CMD or PS. Then okay.
    • Verify using df -hT that mount points at new WSL terminal are, e.g., /c instead of /mnt/c
      • If that doesn't work, use the /etc/fstab method
  2. Edit /etc/passwd to change user's home dir to our common $HOME, e.g., /c/HOME

    • Requires effects of prior step, which require WSL restart, with "wsl --shutdown" being the first step.
    • Also mod root user's home dir entry at /etc/passwd, setting it to that common $HOME, so "sudo su" invokes the same bash config scripts.

Configuration | Advanced Settings

UPDATE: No longer necessary. Configure only /etc/wsl.conf . See below.

WSL mounts some drvfs as type 9p, which indicates a Windows server using 9P protocol. This is seen at output of mount command.

WSL 2

Distros

Installed

wsl -l -v

Available online

wsl --list --online

Import ANY (custom) Linux Distro per Tarball

WSL2 Host IP is NOT localhost

See:

$ cat /etc/resolv.conf

LAN IP v. WSL2 Host IP

$ ip -4 addr show dev eth0
5: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc mq state UP group default qlen 1000
    inet 172.29.148.53/20 brd 172.29.159.255 scope global eth0
       valid_lft forever preferred_lft forever

$ ip route
default via 172.29.144.1 dev eth0
172.29.144.0/20 dev eth0 proto kernel scope link src 172.29.148.53

Fix GUI programs @ WSL 2

On errors of display or clipboard/copy, e.g., ... couldn't connect to display:0, set the DISPLAY environment variable to : WSL_HOST_IP:0.0

export DISPLAY=$(grep nameserver /etc/resolv.conf |awk '{print $2}'):0.0

Commands

WSL 2 Issues: (OBSOLETE)

  1. Filesystem (FS); very slow FS transfers between Win and Linux.
    • Transfer/synch everything to the (linux) FS equivalent (per distro?).
  2. Networking; no localhost; IP Addresses only.
    • localhost:3000 would be, e.g., 192.168.28.2:3000

Reset/Uninstall a distro

wsl.exe --unregister DISTRO_NAME

/etc/fstab Mod(s)

This may not be necessary, depending on Windows 10/11 update status.

Want mount points /<DRIVE> instead of /mnt/<DRIVE>

Persistently mount the desired HOME to /home/$USER per /etc/fstab entry.

sudo vim /etc/fstab
/c/HOME /home/x1 drvfs binary,noacl 0 0

Prior method:

vim /etc/passwd             # 2. Edit ...
# ... change home dir of user (NOT root) to, e.g., /c/HOME
# ... save (ZZ)
vim /root/.bashrc           # 3. Edit/Add ...
    [[ -d '/mnt/c/HOME' ]] && export HOME='/mnt/c/HOME'
    [[ -d '/c/HOME' ]] && export HOME='/c/HOME'
    [[ "$HOME" != '/root' ]] && source $HOME/.bashrc
# ... save (ZZ)

@ ONCE @ our global $HOME

vim ~/.bash_profile     # 4. Edit/Add ...
# Bash on Windows improperly sets `umask` to 0000; should be 0022.
# https://www.turek.dev/post/fix-wsl-file-permissions/ 
# Only @ WSL @ ConEMU; @ WSLtty, `umask` is 0022
[[ "$(umask)" == "0000" ]] && umask 0022
# ... save (ZZ)

Pkgs

# Ubuntu/Debian
sudo apt update
sudo apt-get install vim screenfetch

Fix GPG Key fail @ sudo apt update

sudo apt update  # ... fails with ERR msg: 
# "signatures couldn't be verified because the public key is not available: NO_PUBKEY 5523BAEEB01FA116"

# Note & set the pubkey (5523BAEEB01FA116) ...
_PUBKEY='5523BAEEB01FA116'

# Fetch and install it ...
curl -sL "http://keyserver.ubuntu.com/pks/lookup?op=get&search=0x${_PUBKEY}" \
    | sudo apt-key add
    
sudo apt udpate  # ... should work now

Fixing GPG Keys in Ubuntu (2011)

sudo apt-key adv --keyserver ha.pool.sks-keyservers.net \
    --recv-keys ${_PUBKEY}
$ screenfetch
         _,met$$$$$gg.           f06y@XPC
      ,g$$$$$$$$$$$$$$$P.        OS: Debian
    ,g$$P""       """Y$$.".      Kernel: x86_64 Linux 4.4.0-17134-Microsoft
   ,$$P'              `$$$.      Uptime: 4m
  ',$$P       ,ggs.     `$$b:    Packages: 333
  `d$$'     ,$P"'   .    $$$     Shell: bash l -i
   $$P      d$'     ,    $$P     WM: Not Found
   $$:      $$.   -    ,d$$'     CPU: Intel Core i5-7400T CPU @ 2.4GHz
   $$\;      Y$b._   _,d$P'      RAM: 8433MiB / 15319MiB
   ...

Usage

Distro Management

wslconfig.exe

Manage any installed Linux distro(s)

wslconfig /list [/all]  
wslconfig /unregister <DistributionNames> 
wslconfig /setdefault Ubuntu       

WSL Config | Advanced Settings

/etc/wsl.conf

Configure certain functionality per options, per distro. E.g., map default /mnt to /, and allow chmod/chown. Default behavior, sans wsl.conf, automounts all fixed drives (NTFS, etal), and prepends /mnt. E.g., /mnt/c .

Create the file per distro:

[automount]
root = /
#options = "metadata"
options = "metadata,umask=22,fmask=11"
#options = "metadata,uid=1000,gid=1000,umask=022,fmask=111"

By default, WSL processes /etc/fstab (mountFsTab option; defaults to true).

WSL Filesystem Config | Advanced Settings

Per /etc/fstab

Use this only for settings that are particular to the specified drive. For common settings across all drives, use wsl.conf or .wslconf .

E.g., add SMB drive mount

sudo vim /etc/fstab
...

//RT-AC66U/etc /media/share drvfs binary,noacl 0 0

Per mount(8)

Mount a drive manually, sans any such declaration at either /tec/wsl.conf or /etc/fstab:

# Unmount/Mount
sudo umount /mnt/c
sudo mount -t drvfs C: /mnt/c -o 'metadata'

Mount (router's) thumb drive.

# Mount Windows SMB drive. 
sudo mkdir /media/share
sudo mount -t drvfs //RT-AC66U/etc /media/share

# Verify
ls /media/share

Show all mounts

☩ mount
rootfs on / type lxfs (rw,noatime)
none on /dev type tmpfs (rw,noatime,mode=755)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,noatime)
proc on /proc type proc (rw,nosuid,nodev,noexec,noatime)
devpts on /dev/pts type devpts (rw,nosuid,noexec,noatime,gid=5,mode=620)
none on /run type tmpfs (rw,nosuid,noexec,noatime,mode=755)
...
C:\ on /c type drvfs (rw,noatime,uid=1000,gid=1000,umask=22,fmask=11,metadata,case=off)
D:\ on /d type drvfs (rw,noatime,uid=1000,gid=1000,umask=22,fmask=11,metadata,case=off)
...
\\SMB\etc\ on /smb type drvfs (rw,relatime,case=off)

Integration

Setup a WSL Development Environment

WSL References

@ Debian

$ cat /etc/debian_version       # 9.3; version number 
$ cat /etc/os-release           # OS Info  

$ sudo su
# GNU Dev Tools
$ apt-get update                   # pkgs version-info update      
$ apt-get install build-essential  # gcc, make, ...
$ apt-get install dh-autoreconf    # autoreconf 
# @ Kali
$ apt-get dist-upgrade            
$ apt-get install metasploit-framework  # turn off anti-virus
# Apps
$ apt-get install libssl-dev -y      # openssl crypto lib
$ apt-get install openssh-client -y  # ssh client
$ apt-get install openssh-server -y  # ssh server
$ apt-get install man-db -y   # man pages
$ apt-get install vim -y      # vim editor 
$ apt-get install strace -y   # debugger
$ apt-get install rsync -y    # rsync

# NOT needed; ntfs volumes mounted, per WSL default, @ /mnt/... 
$ apt-get install ntfs-3g -y  # Debian|Ubuntu
$ yum install ntfs-3g     -y  # RHEL 

$ ls -la '/mnt/c/Users/X1'      # fixed-drives automount by default