Score:1

Editing /etc/hosts using WSL vs. using Notepad as an administrator

de flag

Why am I not able to modify my /etc/hosts file in Ubuntu through WSL? But if I try to run Notepad as an administrator and name the file as c:\Windows\System32\drivers\etc\hosts, I am able to modify the /etc/host file.

Is this the limitation of WSL, since I am running it in my Windows machine?

David avatar
cn flag
What is the EXACT command you used to try and modify the hosts file? Moving a file from Ubuntu to Windows will strip off the Ubuntu file permissions as Windows does not use or support them.
Artur Meinild avatar
vn flag
You do realize that `c:\Windows\System32\drivers\etc\hosts` is the Windows hosts file - this itself has nothing to do with Ubuntu or WSL. But WSL uses this file to map onto the `/etc/hosts` file in Ubuntu, as AFAIK.
Score:1
vn flag

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.

I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.