-
Notifications
You must be signed in to change notification settings - Fork 855
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
/etc/resolv.conf and secondary DNX suffixes #701
Comments
updated w/ ipconfig /all content, instead of just ipconfig. |
Thanks for providing this info, our networking wizard is getting back from vacation on Monday and I'll sync up with him to see if he can figure out what's going on. |
I think normal way to work around this would be to put additional search domains into the /etc/resolvconf/resolv.conf.d/post file and they should be appended to /etc/resolv.conf when it is generated, but I don't think this is working either. |
I think on Ubuntu the additional search domains are added into /etc/resolvconf/resolv.conf.d/tail (see man resolvconf), but that doesn't work for me either. |
As per the current design, WSL tries to keep |
In Linux, search domains are declared by adding the "search [,<domain]*" entries into resolv.conf, e.g.:
However. by default, WSL will auto-generate resolv.conf, overwriting any changes you make. But as @sunilmut points out above, a recent Insider build adds the ability to turn-off auto-generation by removing the comment at the top of the file.
Also, the WSL resolv.conf auto-generator doesn't add search suffixes configured on Windows network connections. We have an internal task tracking this scenario and will update this thread if/when it pops off the backlog ;) |
For anyone who reads the above and thinks removing the comment in resolv.conf will fix this - it won't. That method appears to only work while there is an active WSL session, i.e. if you close all open WSL windows and then open a new one, your /etc/resolv.conf is regenerated even if you've previously removed the comment line. This is true at least in the Fall Creator's Update (build 16299.19) as of today. I also experienced this with the first Creator's Update in the spring. I assume it's always been this way. I didn't see anything in User Voice for this as suggested here, so I created a new idea here if people want to go vote on it. Finally, as a workaround, I set up a file ~/resolv.conf.add with my desired suffix search list in it. Then I modified my .bashrc to copy the generated /etc/resolv.conf minus the last line (which is the search line), cat my file with the result, then copy back to /etc. Content of ~/resolv.conf.add: Relevant content of ~/.bashrc:
This will make you enter your WSL password every time you open a WSL window. There are various ways around this, but all of them (that I can think of) to get rid of the prompt completely involve changing /etc/sudoers to allow running that mv command without entering a password. Probably the 'safest' would be to locate all the bashrc commands above into a separate script, which is itself allowed to run for your user with NOPASSWD in /etc/sudoers. Then call that separate script from your .bashrc. It would be really great if the WSL /etc/resolv.conf autogeneration/autoupdate handling were changed to include the entire configured DNS suffix search list from Windows, or to allow a more standard way of overriding the generated list that persists between WSL sessions. For most home users the existing behavior is fine, but workplace users are likely to have a need for more complicated DNS suffix search lists and the WSL behavior here is not ideal. |
I regard the current behavior as a bug: if WSL generates the |
I agree the current behavior is a bug. Secondary search suffixes from windows should be added to the search list in resolv.conf. Any other workaround, such as populating resolv.conf as part of your profile, or login routine, is simply a hack because the original product was wrong. |
At least now, for me, the following procedure seems to prevent WSL from overwriting resolv.conf:
To check that it doesn't get overwritten any more, you can force a "shutdown" of the Linux system by running this command in an elevated cmd.exe: |
Any update on this? I too would like to see all domains added to the one search line in /etc/resolv.conf search domain1 domain2 ... The current behavior appears to be incorrectly setting "search" to the Primary Dns Suffix. "search" should be set to a space separated list from the "DNS Suffix Search List" |
The problem with unlinking I would suggest an option in
or even something more generic:
|
Any updates on this? Been open for 3.5 years now... |
I recently made the move from WSLv1 to WSLv2, and this ranks as one of my two greatest pain points with v2. |
I also ran aground on this issue during my WSL1 -> WSL2 migration. My workaround is to disable WSL's |
Same for me, moving to WSL2 and the dns uffixes are missing ... |
same for me w/ WSL2, dns search domains are not added at all which means local systems are only reachable using the FQDN |
Any update on this? Ideally, it should just take the "DNS Suffix Search List" and transfer it to /etc/resolv.conf. This is disrupting, because not only are things not working out of the box, it is difficult to create something static that doesn't break when network config changes (e.g. due to switching location, or restarting). |
would love to see this fixed as well... |
I'm going to put my "me too" in here as well. Overriding the resolv.conf file with my own org's settings is fine for a PC that doesn't go anywhere, but I have a laptop. My connection suffix can change on a regular basis and I would like that change to be reflected in the search directive. |
This is a problem for me as well. My resolv.conf does not pick up the primary dns suffix and I have to add it manually to all hostnames. How can i make WSL2 pick up the primary dns suffix automatically? |
Since the migration from WSL to WSL2 I ran into the issue as well. 😠 ...
I cannot even do this, as I have hosts/resolv.conf pseudo-domains to my local IPs and the WSL2 networking IPs are always auto-generated on startup. Something like this would work:
|
This is harder given the common work-from-home. I need one set of entries in resolv.conf while off VPN and a different set while on VPN. #5806 bugs with AUTHORITY/ADDITIONAL records mishandled by the WSL local DNS cache makes this even harder. |
I used # in ~/.bashrc, WSL2 Ubuntu18.04
refresh_resolvconf() {
# Get the IP Address (without any carriage return added by running a Windows command)
NAMESERVER=`/mnt/c/WINDOWS/system32/netsh.exe interface ip show config name="vEthernet (WSL)" \
| grep "IP Address" \
| cut -d: -f2 \
| sed "s/\r//"`
# Replace the nameserver line - line 6 in my config
sudo sed -i "6s/.*/nameserver $NAMESERVER/" /etc/resolv.conf
} Idk if it was because of the 2 weeks between or what in which time I forgot that nameserver had always worked before, but it was only after doing all this that I realized it would just have been easier to let the resolv.conf be generated and just add my search domains in 😂 So now I'm just doing sudo bash -c "echo search company.com >> /etc/resolv.conf" ... that was fun ... Hopefully this helps someone. |
@ntextreme3 Thanks! Following that line of thought, I've added to .bashrc
|
Thank you @mheyman and @dklawson for the starting point in solving this annoying issue. However the script seems overly complicated for me and more importantly, only works on en-US Windows installations because of the language-specific grep-ing used. On my system, the DNS nameservers are correctly added and updated by WSL itself - including when I'm on my companies VPN. This is because the VPN client is configured to add the corporate DNS servers to the local NICs as well on connect (as opposed to only having them set on the virtual VPN interface). This is generally the preferred setup because it makes DNS queries much faster: rather than Windows waiting for your public resolvers to time out for internal DNS queries before it tries another DNS server, all DNS queries go to the corporate DNS which resolves both internal and external names quickly. But whether this works on your machine will depend on whether your VPN client sets the DNS servers on all interfaces or not. Anyway, that's just the nameservers, the DNS search suffix was still missing every time for me and I wanted a robust, language-independent solution. /opt/add_dns_search_suffix.sh #!/bin/bash
set -euo pipefail
# Gets the global DNS domain of the computer as well as the DNS search suffix domains of all
# individual network interfaces, deduplicates any entries and formats them in a single line
DNSSEARCH=$(/mnt/c/windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoLogo -NoProfile -Command \
"[System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().DomainName; \
[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces().GetIPProperties().DnsSuffix" \
| tr -d '\r' | uniq | tr -s '\n' ' ')
echo "Will set search domains: ${DNSSEARCH}"
echo "search ${DNSSEARCH}" | sudo tee -a /etc/resolv.conf > /dev/null Setting permissions on the shell script sudo chown root.root /opt/add_dns_search_suffix.sh
sudo chmod 0744 /opt/add_dns_search_suffix.sh /etc/sudoers.d/add_dns_search_suffix
At the end of my ~/.bashrc echo "UPDATING WSL DNS SEARCH SUFFIXES ..."
sudo /opt/add_dns_search_suffix.sh
echo "UPDATING WSL DNS SEARCH SUFFIXES DONE" This works great on Windows 10 Version 21H2 (Build 19044.x) using Ubuntu 20.04 and 22.04 in WSL1. On this setup at least, I have not needed to pass/preserve the Hope this helps! |
Thanks @jantari! I updated my answer to include the simpler powershell command to get the search line. |
I found I needed to use: |
|
I don't know if it's just me missing something or not knowing enough, but using the above scripts to write an updated I made it update rather than just skip in case my network changes I can just rerun the script and it will update if needed.
|
I just found this issue while googling for this problem. It boggles my mind that this issue was opened 6 years ago and still I need to do some kludgy shell script to add the windows search domains to resolv.conf in my WSL distro... |
Hello, |
I took some inspiration from @saxonww , thanks for that. I share my fix bellow (even though it is still just a hack ).
And the solution: Apparently, at this point it seems that even though I cannot
The above snippet speaks for itself. I generate an extended resolv.conf (w/ desired changes) in /tmp and move that over my /etc/resolv.conf. Seems to work for the time being, even after a Drawback of this solution is that this only changes the |
Something like:
will run the echo as root, but the redirection still runs as you. Use
|
I'm using this to add my DNS suffix (using wsl.exe to run it as root, so it doesn't require any sudo prompts or changes). Added to ~/.profile:
|
Thank you to everyone who has presented solutions to this problem. It would be great to get an actual solution. I have taken @mheyman solution above and adjusted the search domain list to come from the following: Get-DnsClientGlobalSetting | Select-Object -ExpandProperty SuffixSearchList And the solution seems to work ok. |
It has been 7 years since this bug was reported. The available workarounds are cumbersome and fragile.
@benhillis, have they returned from vacation yet? |
Bye, Microsoft - I am a lucky MacOS user now. |
Just ran into this issue also and surprised there is no official solution. If I install systemd under WSL does systemd take over generation of resolv.conf from WSL, and can I fix it there? |
systemd-resolved can be optionally configured to manage /etc/resolv.conf. Obviously you would need to tell WSL to not manage /etc/resolv.conf itself first. Though I think doing this and adding domains to /etc/systemd/resolved.conf is probably just a more complicated way of adding the same domains to /etc/resolv.conf directly; systemd-resolved will use /etc/resolv.conf contents when not managing the conffile itself. |
I want to avoid hardcoding the DNS server in resolv.conf as this is fragile. What I'm hoping is that I can override the search domain in one of the /etc/systemd/resolved.conf.d/*.conf files, use the systemd stub DNS and have systemd forward the DNS queries to the nameserver. I don't understand how systemd works inside WSL though. Normally systemd-resolved would get the nameserver from DHCP, does that work the same in WSL? |
It looks like 2.0.0 might finally fix this:
|
Please try enabling "dnsTunneling" and let us know if it fixes the issue. thanks! you can find more details at https://devblogs.microsoft.com/commandline/windows-subsystem-for-linux-september-2023-update/ |
We have added the following documentation about how DNS suffixes are handled in WSL - https://github.com/MicrosoftDocs/WSL/blob/main/WSL/troubleshooting.md#dns-suffixes-in-wsl At the moment, to get secondary Windows DNS suffixes configured in WSL you need to have one of the following 2 configs in your wslconfig file |
Unfortunately, these options are only available on Windows 11, as per:
This is what I get in Windows 10: Can you make this available for those of us in enterprises that still only support Windows 10? Thanks! |
For now I've added this to my ~/.bashrc (based on above tips):
|
hello @ahsodex, thanks for reaching out Mirrored mode and DNS tunneling are Windows 11 only features, and we are currently not investigating backporting them to Windows 10 |
For connection-specific suffixes (set by DHCP), one can use this PowerShell command instead: (Get-DnsClient | ForEach-Object {$_.ConnectionSpecificSuffix}) -join " " |
Hello @CatalinFetoiu . I'm on Windows 11 Pro (23H2). I tried the following, but still do not get the DNS suffixes in WSL. What am I doing wrong ?
Also tried with this content, and no more luck.
|
@abenoit-reeliant did you look at the docs? It doesn't go under |
Thanks @jantari I did look at the doc, but scrolled too fast and read the wrong header. However if I read correctly this time, it should be in After analyzing a bit more, I see that the suffixes are defined on one specific interface (they are set up when I connect to a VPN), so that might complicate things more. The solution by xieve works for me when I use the |
There's been a bunch of good solutions posted here already, but the thread's gotten so long and scripts have been iteratively improved on it's not obvious what the best solution is. For any situation where #!/bin/bash
# https://github.com/microsoft/WSL/issues/701#issuecomment-1162887704
# https://github.com/microsoft/WSL/issues/701#issuecomment-1428917142
set -euo pipefail
# Gets the global DNS domain of the computer as well as the DNS search suffix domains of all
# individual network interfaces, deduplicates any entries and formats them in a single line
DNSSEARCH=$(/mnt/c/windows/System32/WindowsPowerShell/v1.0/powershell.exe -NoLogo -NoProfile -Command \
'@([System.Net.NetworkInformation.IPGlobalProperties]::GetIPGlobalProperties().DomainName;
[System.Net.NetworkInformation.NetworkInterface]::GetAllNetworkInterfaces().GetIPProperties().DnsSuffix;
(Get-DnsClientGlobalSetting).SuffixSearchLIst).Where({ $_ })' | tr -d '\r' | awk '!seen[$0]++,ORS=" "')
echo "Will set search domains: ${DNSSEARCH}"
# Replacing or appending 'search ...' config line in resolv.conf
wsl.exe -d "${WSL_DISTRO_NAME}" -u root -e /usr/bin/sed -i \
-e '/^\(search[[:blank:]]\).*/{s//\1'"${DNSSEARCH}"'/;:a;n;ba;q}' \
-e '$asearch '"${DNSSEARCH}" \
/etc/resolv.conf I've been using this for years now, from WSL1 to WSL2. No need to sudo and should cover all scenarios including VPNs without issues. |
Here are the TCP/IP settings:
![dns suffixes](https://cloud.githubusercontent.com/assets/115950/17255834/bedbbc10-556f-11e6-81dc-6b27838c0f68.png)
And here is ipconfig /all output:
Actual results (with terminal output if applicable)
Your Windows build number
14372
The text was updated successfully, but these errors were encountered: