MDNS: Difference between revisions

From Alpine Linux
(Add setup instructions for NetworkManager and dnsmasq)
m (fix mistakes)
 
(10 intermediate revisions by 4 users not shown)
Line 1: Line 1:
{{DISPLAYTITLE:mDNS}}
{{DISPLAYTITLE:mDNS}}
[https://en.wikipedia.org/wiki/Multicast_DNS Multicast DNS] is a protocol that is normally used for the discovery of printers. It is implemented by Avahi, but more setup is needed for the regular name resolution to see the results.
[https://en.wikipedia.org/wiki/Multicast_DNS Multicast DNS] is a protocol that is normally used for the discovery of printers. Avahi is a popular implementation by but more setup is needed for the regular name resolution to see the results.


== Setup avahi ==
== Setup avahi ==


Install, enable and start avahi with:
Install, enable and start avahi with:{{cmd|$ doas apk add {{pkg|avahi}}
 
$ doas rc-update add avahi-daemon
{{cmd|doas apk add {{pkg|avahi}}
$ doas rc-service avahi-daemon start}}
doas rc-update add avahi-daemon
doas rc-service avahi-daemon start}}


It should now be possible to browse results. To look for a printer, use:
It should now be possible to browse results. To look for a printer, use:


{{cmd|doas apk add {{pkg|avahi-tools}}
{{cmd|$ doas apk add {{pkg|avahi-tools}}
avahi-browse --resolve --terminate  _ipp._tcp}}
$ avahi-browse --resolve --terminate  _ipp._tcp}}


Make note of the hostname, as we will use it afterwards.
Make note of the hostname, as we will use it afterwards.
Line 19: Line 17:
== Setup avahi2dns ==
== Setup avahi2dns ==


Name resolution is implemented by musl, and it only supports DNS, so we have to map the avahi results to a regular DNS server. This is done by avahi2dns.
Name resolution is implemented by musl, and it only supports DNS, so we have to map the avahi results to a regular DNS server. This is done by [https://github.com/LouisBrunner/avahi2dns avahi2dns]. As of now, {{pkg|avahi2dns}} package is available in the [[Repositories#Testing|testing]] repository. It can be safely installed by following the [[Repositories#Using_testing_repository|guidelines]].


{{cmd|doas apk add {{pkg|avahi2dns}}}}
{{cmd|$ doas apk add avahi2dns@testing}}


since we will want a full DNS server running at port 53, we need to configure avahi2dns to use another port. This is done by default with {{path|/etc/conf.d/avahi2dns}} containing:
since we will want a full DNS server running at port 53, we need to configure avahi2dns to use another port. This is done by default with {{path|/etc/conf.d/avahi2dns}} containing:
Line 27: Line 25:
   command_args="-p 5354"
   command_args="-p 5354"


Enable and start avahi2dns with
Enable and start avahi2dns with the command:{{cmd|$ doas rc-update add avahi2dns
$ doas rc-service avahi2dns start}}


{{cmd|doas rc-update add avahi2dns
It should now be possible to use DNS to query the address of the printer:{{cmd|$ drill -p 5354 @127.0.0.1 <printer_name>.local}}
doas rc-service avahi2dns start}}
 
It should now be possible to use DNS to query the address of the printer.
 
{{cmd|drill -p 5354 @127.0.0.1 <printer_name>.local}}


Where printer_name is the hostname given by avahi-browse.
Where printer_name is the hostname given by avahi-browse.
Line 40: Line 34:
== Setup Networkmanager ==
== Setup Networkmanager ==


If you are already using networkmanager, you can leverage {{pkg|dnsmasq}} as your DNS server which can easily forward mDNS requests to another server.
If you are already using [[NetworkManager|Networkmanager]], you can leverage {{pkg|dnsmasq}} as your DNS server which can easily forward mDNS requests to another server.
 
Install the {{pkg|networkmanager-dnsmasq}} package:
 
{{cmd|doas apk add {{pkg|networkmanager-dnsmasq}}}}
 
Configure networkmanager to use dnsmasq as it's dns server by editing {{path|/etc/NetworkManager/NetworkManager.conf}}


  [main]
Install the {{pkg|networkmanager-dnsmasq}} package: {{cmd|$ doas apk add {{pkg|networkmanager-dnsmasq}}}}
  dhcp=internal
  dns=dnsmasq


Then we need to tell dnsmasq to forward all mDNS queries to avahidns {{path|/etc/NetworkManager/dnsmasq.d/mdns.conf}}:
Configure networkmanager to use dnsmasq as it's dns server by editing {{path|/etc/NetworkManager/NetworkManager.conf}} as follows {{Cat|/etc/NetworkManager/NetworkManager.conf|<nowiki>[main]
dhcp=internal
dns=dnsmasq</nowiki>}}


  # Forward queries for the "local" domain to 127.0.0.1 port 5354
Then we need to tell dnsmasq to forward all mDNS queries to avahidns by editing the file {{path|/etc/NetworkManager/dnsmasq.d/mdns.conf}} as follows {{Cat|/etc/NetworkManager/dnsmasq.d/mdns.conf|# Forward queries for the "local" domain to 127.0.0.1 port 5354
  server=/local/127.0.0.1#5354
server{{=}}/local/127.0.0.1#5354}}


Restart networkmanager:
Restart networkmanager: {{cmd|$ doas rc-service networkmanager restart}}
 
{{cmd|doas rc-service networkmanager restart}}


== Without NetworkManager ==
== Without NetworkManager ==


=== Setup DNS resolver ===
If you are not using [[NetworkManager|Networkmanager]], you will need to setup a DNS resolver that will forward request of .local domain to avahi2dns and handle other requests normally. There is more than one way to do it.
 
If you are not using NetworkManager, you will need to setup a DNS resolver that will forward request of .local domain to avahi2dns and handle other requests normally. There is more than one way to do it, but we document an option that is probably most convenient for a laptop: using the DHCP provided server for the regular DNS requests. We will use unbound as the server and resolvconf to inform unbound about the DHCP results.
 
Install the programs:
 
{{cmd|doas apk add {{pkg|openresolv}} {{pkg|unbound}}}}


Create {{path|/etc/resolvconf.conf}}:
=== Setup unbound as DNS resolver ===


  name_servers=127.0.0.1
We will use [[Setting up unbound DNS server|unbound]] as the server and {{ic|resolvconf}} tool to inform unbound about the DHCP results. This option is probably most convenient for a laptop: using the DHCP provided by the server for the regular DNS requests.
  unbound_conf=/etc/unbound/unbound.conf.d/resolvconf.conf


This tells resolveconf to use a local nameserver and pass the DHCP provided DNS server to unbound
Install the programs: {{cmd|$ doas apk add {{pkg|openresolv}} {{pkg|unbound}}}}


Create {{path|/etc/unbound/unbound.conf.d/avahi-local.conf}}:
Create {{path|/etc/resolvconf.conf}} as follows:{{Cat|/etc/resolvconf.conf|<nowiki>name_servers=127.0.0.1
unbound_conf=/etc/unbound/unbound.conf.d/resolvconf.conf</nowiki>}}


This tells resolveconf to use a local nameserver and pass the DHCP provided DNS server to unbound. Create {{path|/etc/unbound/unbound.conf.d/avahi-local.conf}} as follows {{Cat|/etc/unbound/unbound.conf.d/avahi-local.conf|
   forward-zone:
   forward-zone:
         name: "local"
         name: "local"
Line 86: Line 67:
         do-not-query-localhost: no
         do-not-query-localhost: no
         domain-insecure: "local"
         domain-insecure: "local"
 
}}
This reads the information provided by resolvconf, but forwards .local requests to avahi2dns. We also need to disable dnssec for .local and tell unbound that it is OK to query localhost.
This reads the information provided by resolvconf, but forwards .local requests to avahi2dns. We also need to disable dnssec for .local and tell unbound that it is OK to query localhost.


Enable and start unbound
Enable and start unbound. {{cmd|$ doas rc-update add unbound
$ doas rc-service unbound start}}


{{cmd|doas rc-update add unbound
=== Using existing bind server as DNS resolver ===
doas rc-service unbound start}}


=== Setup DHCP client ===
It is assumed that a bind(named) server is already installed and running, if not, then you can use the instructions [[Small-Time_DNS_with_BIND9]]


How send the DHCP provided DNS to resolvconf depends on the DHCP client being used.
Once the bind(named) server is up and running, then the solution is to set an access "zone" for postfix addresses.local


==== udhcpc ====
To do this, add the following lines to the bind configuration file {{path|/etc/bind/named.conf}} as follows {{Cat|/etc/bind/named.conf|


This is the DHCP client in busybox, and will work for both wired and wireless interfaces.  
zone "local" {
  type forward; # type requests
  forward only; # rule to use only forwards
  forwarders {
    127.0.0.1 port 5354; # Avahi2dns address and port
  };
};
}}
Additionally, you may need to disable dnssec verification for the .local postfix. To do this, add the following lines inside the options configuration {{Cat|/etc/bind/named.conf|...


Create {{path|/etc/udhcpc/udhcpc.conf}}:
options {
...
  validate-except {
    "local";
  };
...
};
}}
After adding the configuration, double-check that you have a .local zone. To do this, use the command: {{Cmd|$ named-checkconf -l # OUTPUT: local IN _default forward}}


  RESOLV_CONF="/etc/udhcpc-resolv.conf"
If there were no errors, you can reload the bind(named) service:{{Cmd|$ doas rc-service named reload}}


Create {{path|/etc/udhcpc/post-bound/resolvconf}}:
=== Setup DHCP client ===


  #!/bin/sh
How send the DHCP provided DNS to resolvconf depends on the DHCP client being used.
  cat /etc/udhcpc-resolv.conf | resolvconf -a $interface


and make it executable
==== udhcpc ====


{{cmd|chmod 755 /etc/udhcpc/post-bound/resolvconf}}
This is the DHCP client in busybox, and will work for both wired and wireless interfaces. Create {{path|/etc/udhcpc/udhcpc.conf}} as follows {{path||/etc/udhcpc/udhcpc.conf|<nowiki>
RESOLV_CONF="/etc/udhcpc-resolv.conf"</nowiki>}}


An inconvenience of this setup is that udhcpc will not reconfigure the interface when connecting to other wifi networks. For that to happen one has to run
Create the file {{path|/etc/udhcpc/post-bound/resolvconf}} as follows {{Cat|/etc/udhcpc/post-bound/resolvconf|
 
#!/bin/sh
{{cmd|iwctl station wlan0 connect <network_name>
cat /etc/udhcpc-resolv.conf | resolvconf -a $interface
doas kill -USR2 $(cat /run/udhcpc.wlan0.pid)
}}
doas kill -USR1 $(cat  /run/udhcpc.wlan0.pid)}}
and make it executable {{cmd|$ doas chmod 755 /etc/udhcpc/post-bound/resolvconf}}


==== iwd ====
==== iwd ====


To avoid having to manually reconfigure the wifi interface, we can configure iwd to use DHCP internally and forward DNS server info to resolveconf. To do that create {{path|/etc/iwd/main.conf}}:
When using [[iwd]] an inconvenience of this setup is that udhcpc will not reconfigure the interface when connecting to other wifi networks. For that to happen one has to run


  [General]
{{cmd|$ iwctl station wlan0 connect <network_name>
  EnableNetworkConfiguration=True
$ doas kill -USR2 $(cat  /run/udhcpc.wlan0.pid)
$ doas kill -USR1 $(cat  /run/udhcpc.wlan0.pid)}}


  [Network]
To avoid having to manually reconfigure the wifi interface, we can configure [[iwd]] to use DHCP internally and forward DNS server info to resolveconf. To do that create the file {{path|/etc/iwd/main.conf}} as follows {{Cat|/etc/iwd/main.conf|<nowiki>[General]
  NameResolvingService=resolvconf
EnableNetworkConfiguration=True


== Test the setup ==
[Network]
NameResolvingService=resolvconf</nowiki>}}


You should now be able to query for both the printer address and regular addresses with
== Troubleshooting ==


{{cmd|drill @127.0.0.1 <printer_name>.local
=== Test the setup ===
drill @127.0.0.1 alpinelinux.org}}


Your {{path|/etc/resolv.conf}} should also contain
You should be able to query for both the printer address and regular addresses with: {{cmd|$ drill @127.0.0.1 <printer_name>.local
$ drill @127.0.0.1 alpinelinux.org}}


  nameserver 127.0.0.1
Your {{path|/etc/resolv.conf}} should also contain {{Cat|/etc/resolv.conf|nameserver 127.0.0.1}}


Printer discovery should now be working.
Printer discovery should now be working.
=== Failure in parallel query of A and AAAA requsts ===
An issue with parallel query of A and AAAA requsts was reported to [https://github.com/LouisBrunner/avahi2dns/issues/21 upstream] related to ipv4/ipv6 dual-setups and since been resolved in Alpine Linux {{MR|82560}} by adding a timeout 2.5s.


[[Category:Networking]]
[[Category:Networking]]
[[Category:Printers]]

Latest revision as of 07:20, 4 June 2025

Multicast DNS is a protocol that is normally used for the discovery of printers. Avahi is a popular implementation by but more setup is needed for the regular name resolution to see the results.

Setup avahi

Install, enable and start avahi with:

$ doas apk add avahi $ doas rc-update add avahi-daemon $ doas rc-service avahi-daemon start

It should now be possible to browse results. To look for a printer, use:

$ doas apk add avahi-tools $ avahi-browse --resolve --terminate _ipp._tcp

Make note of the hostname, as we will use it afterwards.

Setup avahi2dns

Name resolution is implemented by musl, and it only supports DNS, so we have to map the avahi results to a regular DNS server. This is done by avahi2dns. As of now, avahi2dns package is available in the testing repository. It can be safely installed by following the guidelines.

$ doas apk add avahi2dns@testing

since we will want a full DNS server running at port 53, we need to configure avahi2dns to use another port. This is done by default with /etc/conf.d/avahi2dns containing:

 command_args="-p 5354"

Enable and start avahi2dns with the command:

$ doas rc-update add avahi2dns $ doas rc-service avahi2dns start

It should now be possible to use DNS to query the address of the printer:

$ drill -p 5354 @127.0.0.1 <printer_name>.local

Where printer_name is the hostname given by avahi-browse.

Setup Networkmanager

If you are already using Networkmanager, you can leverage dnsmasq as your DNS server which can easily forward mDNS requests to another server.

Install the networkmanager-dnsmasq package:

$ doas apk add networkmanager-dnsmasq

Configure networkmanager to use dnsmasq as it's dns server by editing /etc/NetworkManager/NetworkManager.conf as follows

Contents of /etc/NetworkManager/NetworkManager.conf

[main] dhcp=internal dns=dnsmasq

Then we need to tell dnsmasq to forward all mDNS queries to avahidns by editing the file /etc/NetworkManager/dnsmasq.d/mdns.conf as follows

Contents of /etc/NetworkManager/dnsmasq.d/mdns.conf

# Forward queries for the "local" domain to 127.0.0.1 port 5354 server=/local/127.0.0.1#5354

Restart networkmanager:

$ doas rc-service networkmanager restart

Without NetworkManager

If you are not using Networkmanager, you will need to setup a DNS resolver that will forward request of .local domain to avahi2dns and handle other requests normally. There is more than one way to do it.

Setup unbound as DNS resolver

We will use unbound as the server and resolvconf tool to inform unbound about the DHCP results. This option is probably most convenient for a laptop: using the DHCP provided by the server for the regular DNS requests.

Install the programs:

$ doas apk add openresolv unbound

Create /etc/resolvconf.conf as follows:

Contents of /etc/resolvconf.conf

name_servers=127.0.0.1 unbound_conf=/etc/unbound/unbound.conf.d/resolvconf.conf

This tells resolveconf to use a local nameserver and pass the DHCP provided DNS server to unbound. Create /etc/unbound/unbound.conf.d/avahi-local.conf as follows

Contents of /etc/unbound/unbound.conf.d/avahi-local.conf

forward-zone: name: "local" forward-addr: 127.0.0.1@5354 server: do-not-query-localhost: no domain-insecure: "local"

This reads the information provided by resolvconf, but forwards .local requests to avahi2dns. We also need to disable dnssec for .local and tell unbound that it is OK to query localhost.

Enable and start unbound.

$ doas rc-update add unbound $ doas rc-service unbound start

Using existing bind server as DNS resolver

It is assumed that a bind(named) server is already installed and running, if not, then you can use the instructions Small-Time_DNS_with_BIND9

Once the bind(named) server is up and running, then the solution is to set an access "zone" for postfix addresses.local

To do this, add the following lines to the bind configuration file /etc/bind/named.conf as follows

Contents of /etc/bind/named.conf

zone "local" { type forward; # type requests forward only; # rule to use only forwards forwarders { 127.0.0.1 port 5354; # Avahi2dns address and port }; };

Additionally, you may need to disable dnssec verification for the .local postfix. To do this, add the following lines inside the options configuration

Contents of /etc/bind/named.conf

... options { ... validate-except { "local"; }; ... };

After adding the configuration, double-check that you have a .local zone. To do this, use the command:

$ named-checkconf -l # OUTPUT: local IN _default forward

If there were no errors, you can reload the bind(named) service:

$ doas rc-service named reload

Setup DHCP client

How send the DHCP provided DNS to resolvconf depends on the DHCP client being used.

udhcpc

This is the DHCP client in busybox, and will work for both wired and wireless interfaces. Create /etc/udhcpc/udhcpc.conf as follows

Create the file /etc/udhcpc/post-bound/resolvconf as follows

Contents of /etc/udhcpc/post-bound/resolvconf

#!/bin/sh cat /etc/udhcpc-resolv.conf

and make it executable

$ doas chmod 755 /etc/udhcpc/post-bound/resolvconf

iwd

When using iwd an inconvenience of this setup is that udhcpc will not reconfigure the interface when connecting to other wifi networks. For that to happen one has to run

$ iwctl station wlan0 connect <network_name> $ doas kill -USR2 $(cat /run/udhcpc.wlan0.pid) $ doas kill -USR1 $(cat /run/udhcpc.wlan0.pid)

To avoid having to manually reconfigure the wifi interface, we can configure iwd to use DHCP internally and forward DNS server info to resolveconf. To do that create the file /etc/iwd/main.conf as follows

Contents of /etc/iwd/main.conf

[General] EnableNetworkConfiguration=True [Network] NameResolvingService=resolvconf

Troubleshooting

Test the setup

You should be able to query for both the printer address and regular addresses with:

$ drill @127.0.0.1 <printer_name>.local $ drill @127.0.0.1 alpinelinux.org

Your /etc/resolv.conf should also contain

Contents of /etc/resolv.conf

nameserver 127.0.0.1

Printer discovery should now be working.

Failure in parallel query of A and AAAA requsts

An issue with parallel query of A and AAAA requsts was reported to upstream related to ipv4/ipv6 dual-setups and since been resolved in Alpine Linux !82560 by adding a timeout 2.5s.