How can I control which interface host.local resolves to?

I've got a server on my network (we'll called the host name server) and I can access the machine on the LAN using the host name server.local which resolves to its local address (let's call it 192.168.1.50). Most devices talk to this machine via WiFi, but for added performance I recently connected another machine directly to it via an Ethernet cable. The server's IP address on this interface is 10.0.0.1, and on the machine that connects to it (client1) it's 10.0.0.2. I noticed after a while I could ping server.local from client1 and it was resolving server.local as 10.0.0.1 instead of 192.168.1.50.

This is great because this means that I don't have to explicitly specify the IP address (this is good for web pages forget their state when the IP address changes), but periodically I see slow performance and when I ping server.local I see it has switched back to 192.168.1.50. After a while it goes back to 10.0.0.1 again.

How can I pin server.local to resolving to the ethernet interface's IP address, at least for the connected machine? It's fine for now if it has to resolve to the same thing for all machines on the network, as currently the directly connected one is the only one using it.


Solution 1:

The .local domain is resolved (translated to an IP address) by dnsmasq (see man dnsmasq systemd-dnsmasq), and "local" has different meanings on different systems.

Rather than try to trick .local, simply add the hostnames and IP addresses to /etc/hosts:

sudoedit /etc/hosts
servereth   10.0.0.1
serverwifi  192.168.1.50

Read man sudoedit hosts.

The connection of IP addresses with actual interface names is done via "packet routing", see man ip ip-route, do ip route, and bear in mind that almost all packets use the "default" route.