microsoft / microsoft/WSL

Autotomatically add Windows host IP address to /etc/hosts

Open
#8,799 0 comments 4 reactions 0 assignees View on GitHub
feature
Dominant language
C++
Stars
33.7k
Forks
1.8k
Avg merge
3d 17h
Merged PRs (30d)
116

Description

**Is your feature request related to a problem? Please describe.**
According to the documentation [https://docs.microsoft.com/en-us/windows/wsl/networking](url) to connect to a server app on the Windows host from WSL use the IP address of the nameserver entry in /etc/resolv.conf. This works as documented but since the address changes periodically it needs to be checked each time.

**Describe the solution you'd like**
I think it would be a great convenience if you could add an entry for the Windows host each time you generate /etc/hosts. Possibly called winhost or some concatenation of the hostname and some other indicator.

**Describe alternatives you've considered**
Both of these work
a) Running sudo from user shell
~> sudo sh -c $'cat /etc/resolv.conf | grep nameserver | awk \'{ printf "\\\n# Windows host\\\n%s hostname.windows\\\n", $2 }\' >> /etc/hosts;'

b) Creating a script and running from [boot] in /etc/wsl.conf
~> cat /usr/local/sbin/winhost.sh
#!/bin/sh
cat /etc/resolv.conf | grep nameserver | awk '{ printf "\n# Windows host\n%s hostname.windows\n", $2 }' >> /etc/hosts

in /etc/wsl.conf (needs full pathname to work)
[boot]
command = /usr/local/sbin/winhost.sh

**Additional context**
Windows 11
WSL 2
openSUSE-Leap-15.4

Contributor guide

Open the contributing guide

Research direction

Start by reviewing how WSL generates /etc/hosts and how the nameserver entry is written to /etc/resolv.conf, using the linked WSL networking documentation for context. Done means each generated /etc/hosts includes a current Windows host IP and a stable hostname mapping without requiring a user script.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
operating-systems
Issue type
Feature
Difficulty
4/5
Estimated time
3-5 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.