{"id":363,"date":"2023-04-26T17:04:40","date_gmt":"2023-04-26T21:04:40","guid":{"rendered":"https:\/\/blog.signalsguru.net\/?p=363"},"modified":"2023-05-24T19:22:06","modified_gmt":"2023-05-24T23:22:06","slug":"wsl-setup-tips","status":"publish","type":"post","link":"http:\/\/blog.signalsguru.net\/archives\/363","title":{"rendered":"WSL Setup Tips"},"content":{"rendered":"\n
When scripting VM or container images, it’s easy to inadvertently rm -rf \/<\/mark>, so it’s desirable to mount host drives read only. It’s also nice to have a static IP for accessing your machine. The default WSL2 configuration uses a dynamic IP. It was actually relatively easy, but it did take me a day or so of work. I’m using the official distro.<\/p>\n\n\n\n Create a \/etc\/wsl.conf<\/a> that uses \/etc\/fstab. This will allow us to custom mount drives however we want. The appendWindowsPath <\/mark>part automatically adds the paths for powershell, ssh, and other standard Windows executables. For this to work, you need to mount drive C. To set a static IP, set generateHosts <\/mark>to false so Windows doesn’t overwrite the hosts file. I’ll get to resolv.conf below.<\/p>\n\n\n\n Now create \/etc\/fstab with the directories you want. Because we enabled automount, it will mount C automatically, but it won’t be read only. The second entry in fstab makes it read only. Note that you can still modify your host machine by running powershell.exe “command”<\/mark>, but it’s a lot harder to damage by accident.<\/p>\n\n\n\n To use a static IP, we’ll need a virtual network adapter<\/a>. Do the following as admin. The first command will only work once WSL is up and running. You’ll have to add a rule to the firewall to allow traffic coming from WSL<\/a>. Without this you won’t even be able to ping the gateway. You may need to kill and restart sshd<\/a>. Initially I removed the dynamic IP windows attached to the adapter. Don’t do this. See below.<\/p>\n\n\n\n To temporarily set an IP, execute the following.<\/p>\n\n\n\n To set it permanently, edit \/etc\/systemd\/network\/10-eth0.network and run systemctl enable systemd-networkd<\/mark>.<\/p>\n\n\n\n I ran into an issue at this point with contacting the default nameserver. To fix this, you have to set generateResolvConf<\/mark> to false and add the following to \/etc\/systemd\/resolved.conf. After reboot, check the result with resolvectl<\/mark>. Initially I tried this with the resolvconf package. But I had to run resolvconf -u<\/mark> after each boot to get it to generate resolv.conf.<\/p>\n\n\n\n You need to link \/etc\/resolve.conf to systemd’s version.<\/p>\n\n\n\n Now update \/etc\/hosts with the new IP.<\/p>\n\n\n\n Let yourself log in without a password. You can log in with a different user with the -u<\/mark> flag (e.g. wsl -d ubuntu -u myuser<\/mark>).<\/p>\n\n\n\n Unlike VirtualBox virtual hard disks, containers grow automatically. If they get too big, you can shrink them with diskpart<\/a>. You will need to locate the container image<\/a>.<\/p>\n\n\n\n If you see error messages like the following, something went wrong mapping the drives, and there’s probably other issues as well. It may crash after a short while. Usually this happens if I shutdown from within the container instead of using wsl –shutdown<\/mark>. The only thing to do is restart.<\/p>\n\n\n\n If you get error messages about insufficient memory, you can try adding the following to C:\\Users\\myuser.wslconfig.<\/p>\n\n\n\n I’ve create an ansible script to do this for Rocky Linux 9. Download Rocky-9-container-Base<\/a>. Place the files below in a directory on the host and follow the steps at the top of the YAML file, making sure to change IPs, user names, paths, etc.<\/p>\n\n\n\n playbook-wsl.yml<\/a>Distributor ID: Ubuntu\nDescription: Ubuntu 22.04.2 LTS\nRelease: 22.04\nCodename: jammy<\/mark><\/code><\/pre>\n\n\n\n
[automount]\nenabled = true\nroot = \/mnt\noptions = \"metadata\"\nmountFsTab = true\n\n[network]\ngenerateResolvConf = false\ngenerateHosts = false\nhostname = ubuntu\n\n[interop]\nappendWindowsPath = true\n\n[user]\ndefault = myuser\n\n[boot]\nsystemd = true<\/mark><\/code><\/pre>\n\n\n\n
# UNCONFIGURED FSTAB FOR BASE SYSTEM\nC:\/mystuff \/home\/mystuff drvfs defaults,ro 0 0\nC: \/mnt\/c drvfs defaults,ro 0 0<\/mark><\/code><\/pre>\n\n\n\n
netsh.exe interface ip add address \"vEthernet (WSL)\" 192.168.2.1 255.255.255.0\nNew-NetFirewallRule -Name 'WSL' -DisplayName 'WSL' -InterfaceAlias 'vEthernet (WSL)' -Direction Inbound -Action Allow<\/mark><\/code><\/pre>\n\n\n\n
ip addr add 192.168.2.5\/24 broadcast 192.168.2.255 dev eth0 label eth0:1<\/mark><\/code><\/pre>\n\n\n\n
[Match]\nName=eth0\n\n[Network]\nAddress=192.168.2.5\/24\nGateway=192.168.2.1\nDHCP=no<\/mark><\/code><\/pre>\n\n\n\n
[Resolve]\n# Google's public DNS\nDNS=8.8.8.8 8.8.4.4<\/mark><\/code><\/pre>\n\n\n\n
ln -s \/run\/systemd\/resolve\/resolv.conf \/etc\/resolv.conf<\/mark><\/code><\/pre>\n\n\n\n
192.168.2.5 ubuntu<\/mark><\/code><\/pre>\n\n\n\n
echo \"myuser ALL=(ALL) NOPASSWD:ALL\" | sudo tee \/etc\/sudoers.d\/myuser<\/mark><\/code><\/pre>\n\n\n\n
<3>WSL (819) ERROR: UtilTranslatePathList:2803: Failed to translate C:\\Windows\\system32<\/mark><\/code><\/pre>\n\n\n\n
[wsl2]\nmemory=4GB\nprocessors=4<\/mark><\/code><\/pre>\n\n\n\n
Rocky<\/h2>\n\n\n\n
fstab.j2<\/a>
wsl.conf.j2<\/a>
update-ip.sh.j2<\/a><\/p>\n\n\n\nAdd WSL Network Adapter<\/h3>\n\n\n\n