These are recommended steps for setting up your WSL
This will allow users to set the owner and group of files using chmod/chown and modify read/write/execute permissions in WSL.
- unmount drvfs
sudo umount /mnt/c
- remount it with the "metadata" flag
sudo mount -t drvfs C: /mnt/c -o metadata
- mount automatically with "metadata" enabled
sudo vi /etc/wsl.conf
and add the following lines,
# mount with options
[automount]
options = "metadata"
[boot]
systemd=true
It would be very convenient to keep home in sync among Windows and different WSL distros.
- change home path
sudo vi /etc/passwd
and find the line defines your username. It will look like this,
david:x:1000:1000:,,,:/home/david:/bin/bash
Change it to,
david:x:1000:1000:,,,:/mnt/c/users/bindai:/bin/bash
- change permission
Exit WSL and re-open it. Home is now changed to new path. Then set home directory permission.
chown david ~
chgrp david ~
chmod 755 ~
- install openssh server
sudo apt update
sudo apt install openssh-server
- generate hostkeys
sudo dpkg-reconfigure openssh-server
- edit the
/etc/ssh/sshd_config
configuration file to allow password authentication
PasswordAuthentication yes
- restart the ssh server
sudo service ssh --full-restart
It is convenient to use the same DNS server with the Window host.
sudo vi /etc/wsl.conf
and add the following lines,
[network]
generateResolvConf = false
restart wsl2: wsl --terminate $WSL_DISTRO_NAME
.
- get the name servers and optional the search domain
ipconfig /all | grep "DNS Servers" | awk '{print "nameserver " $NF}'
ipconfig /all | grep -Po "DNS Suffix .* : \K([^\s]+)" | sort | uniq
sudo vi /etc/resolv.conf
and put the above nameservers.
restart wsl2: wsl --terminate $WSL_DISTRO_NAME
.
If you directly run ping
, you will probably get ping: socket: Operation not permitted
. This usually happens in WSL1. Below command will fix
sudo chmod u+s `which ping`
-
Create an external switch in Hyper-v, say
Bridge-WSL
. -
Create a file named
.wslconfig
under%USERPROFILE%
in Windows.
[wsl2]
networkingMode=bridged
vmSwitch=Bridge-WSL # change this to the bridge name just created.
ipv6=true
- restart wsl2:
wsl --terminate $WSL_DISTRO_NAME
.
Check ip a
to see if it actually works.