{"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

Ubuntu<\/h2>\n\n\n\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

Distributor ID: Ubuntu\nDescription: Ubuntu 22.04.2 LTS\nRelease: 22.04\nCodename: jammy<\/mark><\/code><\/pre>\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

[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

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

# 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

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

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

To temporarily set an IP, execute the following.<\/p>\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

To set it permanently, edit \/etc\/systemd\/network\/10-eth0.network and run systemctl enable systemd-networkd<\/mark>.<\/p>\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

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

[Resolve]\n# Google's public DNS\nDNS=8.8.8.8 8.8.4.4<\/mark><\/code><\/pre>\n\n\n\n

You need to link \/etc\/resolve.conf to systemd’s version.<\/p>\n\n\n\n

ln -s \/run\/systemd\/resolve\/resolv.conf \/etc\/resolv.conf<\/mark><\/code><\/pre>\n\n\n\n

Now update \/etc\/hosts with the new IP.<\/p>\n\n\n\n

192.168.2.5 ubuntu<\/mark><\/code><\/pre>\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

echo \"myuser ALL=(ALL) NOPASSWD:ALL\" | sudo tee \/etc\/sudoers.d\/myuser<\/mark><\/code><\/pre>\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

<3>WSL (819) ERROR: UtilTranslatePathList:2803: Failed to translate C:\\Windows\\system32<\/mark><\/code><\/pre>\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

[wsl2]\nmemory=4GB\nprocessors=4<\/mark><\/code><\/pre>\n\n\n\n

Rocky<\/h2>\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>
fstab.j2<\/a>
wsl.conf.j2<\/a>
update-ip.sh.j2<\/a><\/p>\n\n\n\n

Add WSL Network Adapter<\/h3>\n\n\n\n

The vEthernet (WSL)<\/mark> adapter does not get created until you start WSL after each reboot. There are a couple of ways to automate this.<\/p>\n\n\n\n

Method 1: Startup<\/h6>\n\n\n\n

Put the create_wsl_network.bat<\/a> file in a convenient place like your user home. Create a shortcut to the batch file in the same directory. Right click the shortcut, select Properties -> Advanced<\/strong>, and check Run as administrator<\/strong>. Type windows key + R<\/strong> and enter shell:startup<\/strong>. Create a batch file with a single line that has the full path to the shortcut (e.g. C:\\Users\\myuser\\WSL_Network.lnk). Don’t ask me why this has to be so complicated. The only problem with doing it this way is that it will ask you for permission to run the batch file every time you log in.<\/p>\n\n\n\n

Method 2: Task Scheduler<\/h6>\n\n\n\n

Start the task scheduler and select Create Task<\/strong>. Check Run with highest privileges<\/strong> on the General <\/strong>tab and enter a name for the task at the top. Under the Triggers <\/strong>tab, click New<\/strong>, and select At log on<\/strong> from the drop down at the top. On the Actions <\/strong>tab, click New<\/strong>, and browse to create_wsl_network.bat<\/mark>. Save.<\/p>\n\n\n\n

Windows Terminal<\/h3>\n\n\n\n

If you’re using Windows Terminal, you can open a tab directly to a WSL instance. However, there are a lot of profiles in the dropdown. You can hide a profile by going to Settings -> Profiles -> Specific Profile -> Hide profile from dropdown<\/strong>. If you want to save your terminal settings, you can find the settings.json<\/mark> in C:\\Users\\username\\AppData\\Local\\Packages\\Microsoft.WindowsTerminal_8wekyb3d8bbwe\\LocalState.<\/p>\n\n\n\n

Your WSL instances are mounted as network drives. To access an instance called rocky <\/strong>from Powershell, for example, cd \\\\wsl.localhost\\rocky<\/mark>.<\/p>\n\n\n\n

Hyper-V Service<\/h3>\n\n\n\n

You may also want to set the Hyper-V Host Compute Service<\/strong> to start automatically. It isn’t necessary though since it will start when you start WSL.<\/p>\n\n\n\n

Shortcut to WSL2 GUI Application<\/h3>\n\n\n\n

Create a new shortcut, e.g.:<\/p>\n\n\n\n

C:\\Windows\\System32\\wsl.exe -d rocky -e bash -ic \"nohup ~\/run-emacs.sh &\"<\/mark><\/code><\/pre>\n","protected":false},"excerpt":{"rendered":"

Ubuntu When scripting VM or container images, it’s easy to inadvertently rm -rf \/, 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 …<\/p>\n

WSL Setup Tips<\/span> Read More »<\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"site-sidebar-layout":"default","site-content-layout":"default","ast-global-header-display":"","ast-main-header-display":"","ast-hfb-above-header-display":"","ast-hfb-below-header-display":"","ast-hfb-mobile-header-display":"","site-post-title":"","ast-breadcrumbs-content":"","ast-featured-img":"","footer-sml-layout":"","theme-transparent-header-meta":"default","adv-header-id-meta":"","stick-header-meta":"","header-above-stick-meta":"","header-main-stick-meta":"","header-below-stick-meta":"","footnotes":""},"categories":[12],"tags":[],"_links":{"self":[{"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/posts\/363"}],"collection":[{"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/comments?post=363"}],"version-history":[{"count":8,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/posts\/363\/revisions"}],"predecessor-version":[{"id":397,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/posts\/363\/revisions\/397"}],"wp:attachment":[{"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/media?parent=363"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/categories?post=363"},{"taxonomy":"post_tag","embeddable":true,"href":"http:\/\/blog.signalsguru.net\/wp-json\/wp\/v2\/tags?post=363"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}