6

I've followed the last example in this thread:

https://github.com/Microsoft/WSL/issues/384

In other words:

~$ sudo service dbus start
 * Starting system message bus dbus                                                                                                                    [ OK ]
~$ sudo service avahi-daemon start
 * Starting Avahi mDNS/DNS-SD Daemon avahi-daemon                                                                                                      [ OK ]
~$ avahi-resolve --name rpi.local
Failed to resolve host name 'rpi.local': Timeout reached

This was done after installing the packages listed in this answer.

rpi.localcan be resolved by Windows.

Windows 10 Version 10.0.16299 build 16299

~$ lsb_release -a

No LSB modules are available.
Distributor ID: Ubuntu
Description:    Ubuntu 16.04.3 LTS
Release:        16.04
Codename:       xenial
6

1 Answer 1

0

Not a solution, but a workaround (for WSL2).

It's not just that MDNS isn't working, the real problem is that WSL2 is on its own virtual network NAT-ed to your physical network, and MDNS doesn't get through. Is is actually working in the latest versions, but only for the host machine's name. I've been cursing this too.

I included all mDNS names I frequently need in my WSL's /etc/hosts, and created this script to update them using Windows name resolution.

#!/bin/bash
grep -oP "[0-9.]*\s\K[A-Za-z0-9.-]+\.local" /etc/hosts | while read HOSTNAME
do
    echo -n "Pinging $HOSTNAME... "
    # Executing windows command without subshell breaks DO loop.
    IP=`echo "/mnt/c/Windows/System32/PING.EXE -4 -n 1 $HOSTNAME | grep -oP \"Reply from \K[0-9.-]+\"" | bash`
    echo $IP
    [ -z $IP ] || sudo sed -i "s/.*$HOSTNAME.*/$IP $HOSTNAME/" /etc/hosts
done

echo ====== Result in /etc/hosts
grep -P "[0-9.]*\s[A-Za-z0-9.-]+\.local" /etc/hosts
  1. Executing PING.EXE directly in the WHILE loop broke that loop for me, it stopped after the first iteration if I didn't do it in a subshell. Commenting out the IP= line stopped that behavior, so it's definitely that line that broke the loop. I don't know if it was caused by executing a Windows executable, or by putting the command between backquotes instead of using $(command), or something else I haven't thought of yet.
  2. Use one line per IP/host. Two names with the same IP on one line will not work.
  3. /etc/hosts lines not containing a "*.local" name will remain untouched, of course :)
  4. Lines for hosts that don't reply are left unchanged.
  5. sudo in the sed line, so you need that right to execute it.

-- Edit: removed space that got between - and oP in grep command

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .