Well, I wouldn't call it a "limitation" of WSL personally -- Really more of an intentional feature. If you examine /etc/hosts
in Ubuntu, you should see that the first few lines are:
# This file was automatically generated by WSL. To stop automatic generation of this file, add the following entry to /etc/wsl.conf:
# [network]
# generateHosts = false
Yes, WSL autogenerates this file during its /init
process. It combines three different sources into the auto-generated file (at least in the latest WSL release):
The "normal" Linux /etc/hosts
entries such as:
- A
localhost
entry for 127.0.0.1
- A hostname based entry on
127.0.1.1
(is often inserted by the installer in a typical Linux system, at least Debian).
- Entries for typical useful IPv6 networks
The Windows host file, since its assumed that you would like to have access to those same hostnames whether in PowerShell, Ubuntu, CMD, etc.
As noted, you can easily disable this auto-generation and have full control over your own file if you'd like. Just:
sudo -e /etc/wsl.conf
Side-note: Use sudo update-alternatives --config editor
to set the default editor used by sudo -e
And add:
[network]
generateHosts = false
Then exit Ubuntu.
From PowerShell:
wsl -l -v
# Confirm the distribution name is "Ubuntu" and adjust the following command if needed
wsl --terminate Ubuntu
After restarting Ubuntu, you'll still the auto-generated file (since it was there when you terminated), but if you edit it now, the changes will stick.