.local hostname resolution is slow on 20.04

Solution 1:

Looks like the solution might not depend on nss-mdns at all. I was unfamiliar with systemd-resolved, which appears to have mdns resolution capability. It appears to outperform nss-mdns. However, mdns in systemd-resolved appears to be off by default. Here's how I activated it and deactivated nss-mdns.

  1. Change /etc/nsswitch.conf:

    hosts:          files dns myhostname
    
  2. Edit /etc/systemd/resolved.conf:

    MulticastDNS=yes
    
  3. Create a new /etc/NetworkManager/conf.d/mdns.conf containing:

    [connection]
    connection.mdns=2
    
  4. Restart NetworkManager:

    sudo service NetworkManager restart
    
  5. Might also need to restart systemd-resolved:

    sudo service systemd-resolved restart
    

Test results with:

resolvectl mdns

When it's working, you should get:

Global: yes
Link 2 (enp0s5): yes

Now:

time ping -c1 invasion.local

completes in under 150ms.

Solution 2:

In addition to Bill Barton's excellent answer which helped me quite a bit, I also had to disable IPV6 in my configuration. Machines that had IPv6 enabled would still resolve quickly. If I tried to ping a machine that didn't have IPv6 enabled, like my Nas4Free server, it would take around 5~10 seconds before the ping would start responding. NSLookup was the same.

I followed the instructions on How to disable IPv6 address on Ubuntu 20.04 LTS Focal Fossa - LinuxConfig.org to disable IPv6 on my Ubuntu 20.04 machine.