Difference between revisions of "Avahi"

From ArchWiki
Jump to: navigation, search
(Airprint from Mobile Devices)
(corrected source link to the fork packaged in archlinux)
 
(82 intermediate revisions by 25 users not shown)
Line 1: Line 1:
 
[[Category:Networking]]
 
[[Category:Networking]]
 
[[fr:Avahi]]
 
[[fr:Avahi]]
[http://avahi.org/ Avahi] is a free [[Wikipedia:Zero Configuration Networking|Zero Configuration Networking]] (Zeroconf) implementation, including a system for multicast DNS/DNS-SD service discovery. It allows programs to publish and discover services and hosts running on a local network with no specific configuration. For example you can plug into a network and instantly find printers to print to, files to look at and people to talk to. It is licensed under the GNU Lesser General Public License (LGPL). (Source: [[Wikipedia:Avahi (software)]])
+
[[ja:Avahi]]
 +
From [[Wikipedia:Avahi (software)]]:
 +
:''"[http://avahi.org/ Avahi] is a free [[Wikipedia:Zero-configuration networking|Zero-configuration networking]] (zeroconf) implementation, including a system for multicast DNS/DNS-SD service discovery. It allows programs to publish and discover services and hosts running on a local network with no specific configuration. For example you can plug into a network and instantly find printers to print to, files to look at and people to talk to. It is licensed under the GNU Lesser General Public License (LGPL)."''
  
 
== Installation ==
 
== Installation ==
[[pacman|Install]] {{Pkg|avahi}} and {{Pkg|nss-mdns}}, available in the [[official repositories]].  
+
[[Install]] the {{Pkg|avahi}} package.
  
{{bc|# pacman -S avahi nss-mdns}}
+
You can manage the Avahi daemon with {{ic|avahi-daemon.service}} [[systemd#Using units|using systemd]].
  
=== Enable Avahi daemon under sysvinit system ===
+
== Using Avahi ==
  
After installing Avahi you will need to restart the {{ic|dbus}} [[daemon]] before you can start {{ic|avahi-daemon}}.
+
=== Hostname resolution ===
 +
Avahi provides local hostname resolution using a "''hostname''.local" naming scheme. To enable it, install the {{Pkg|nss-mdns}} package and start {{ic|avahi-daemon.service}}.
  
{{Note|{{ic|avahi-daemon}} depends on {{ic|dbus}} [[daemon]], so it should be added after {{ic|dbus}} in the {{ic|DAEMONS}} array in [[rc.conf]] file}}
+
Then, edit the file {{ic|/etc/nsswitch.conf}} and change the line:
 +
hosts: files dns myhostname
 +
to:
 +
hosts: files mdns_minimal [NOTFOUND=return] dns myhostname
  
=== Enable Avahi daemon under native systemd system ===
+
{{Note|If you experience slowdowns in resolving {{ic|.local}} hosts try to use {{ic|mdns4_minimal}} instead of {{ic|mdns_minimal}}.}}
You can enable Avahi Daemon at startup with the following command:
+
  
{{bc|# systemctl enable avahi-daemon.service}}
+
==== Additional info about mdns ====
 +
 
 +
The {{ic|mdns_minimal}} module handles queries for the {{ic|.local}} TLD only. Note the {{ic|<nowiki>[NOTFOUND=return]</nowiki>}}, which specifies that if {{ic|mdns_minimal}} cannot find {{ic|*.local}}, it will not continue to search for it in {{ic|dns}}, {{ic|myhostname}}, etc. In case you have configured Avahi to use a different TLD, you should replace {{ic|1=mdns_minimal [NOTFOUND=return]}} with the full {{ic|mdns}} module. There also are IPv4-only and IPv6-only modules {{ic|mdns[46](_minimal)}}.
 +
 
 +
==== Tools ====
 +
 
 +
Avahi includes several utilities which help you discover the services running on a network. For example, run
 +
$ avahi-browse -alr
 +
to discover services in your network.
 +
 
 +
The Avahi Zeroconf Browser ({{ic|avahi-discover}} – note that it needs avahi's optional dependencies {{Pkg|pygtk}} and {{Pkg|python2-dbus}}) shows the various services on your network. You can also browse SSH and VNC Servers using {{ic|bssh}} and {{ic|bvnc}} respectively.
 +
 
 +
There's a good list of software with Avahi support at their website: http://avahi.org/wiki/Avah4users
 +
 
 +
===File sharing===
 +
 
 +
====NFS====
 +
If you have an [[NFS]] share set up, you can use Avahi to be able to automount them in Zeroconf-enabled browsers (such as Konqueror on KDE and Finder on OS X).
 +
 
 +
Create a {{ic|.service}} file in {{ic|/etc/avahi/services}} with the following contents:
 +
{{hc|/etc/avahi/services/nfs_Zephyrus_Music.service|<nowiki>
 +
<?xml version="1.0" standalone='no'?>
 +
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
 +
<service-group>
 +
  <name replace-wildcards="yes">NFS Music Share on %h</name>
 +
  <service>
 +
    <type>_nfs._tcp</type>
 +
    <port>2049</port>
 +
    <txt-record>path=/data/shared/Music</txt-record>
 +
  </service>
 +
</service-group></nowiki>}}
 +
 
 +
The port is correct if you have ''insecure'' as an option in your {{ic|/etc/exports}}; otherwise, it needs to be changed (note that ''insecure'' is needed for OS X clients). The path is the path to your export, or a subdirectory of it. For some reason the automount functionality has been removed from Leopard, however [http://www.macosxhints.com/article.php?story=20071116042238744 a script is available]. This was based upon [http://ubuntuforums.org/showthread.php?p=4387032#post4387032 this post].
 +
 
 +
====Samba====
 +
With the Avahi daemon running on both the server and client, the file manager on the client should automatically find the server.
 +
 
 +
====Vsftpd====
 +
You can also auto-discover regular FTP servers, such as vsftpd. Install the {{Pkg|vsftpd}} package and change the settings of vsftpd according to your own personal preferences (see [http://ubuntuforums.org/showthread.php?t=218630 this thread on ubuntuforums.org] or {{ic|man vsftpd.conf}}).
 +
 
 +
Create a {{ic|.service}} file in {{ic|/etc/avahi/services}} with the following contents:
 +
{{hc|/etc/avahi/services/ftp.service|<nowiki>
 +
<?xml version="1.0" standalone='no'?>
 +
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
 +
<service-group>
 +
  <name>FTP file sharing</name>
 +
  <service>
 +
    <type>_ftp._tcp</type>
 +
    <port>21</port>
 +
  </service>
 +
</service-group>
 +
</nowiki>}}
 +
When you are done, [[restart]] the {{ic|avahi-daemon.service}} and {{ic|vsftpd.service}} services.
 +
 
 +
The FTP server should now be advertised by Avahi. You should now be able to find the FTP server from a file manager on another computer in your network. You might need to enable [[#Hostname resolution]] on the client.
 +
 
 +
===Link-Local (Bonjour/Zeroconf) chat===
 +
 
 +
Avahi can be used for bonjour protocol support under linux. Check [[Wikipedia:Comparison of instant messaging clients]] or [[List of applications#Instant messaging]] for a list of clients supporting the bonjour protocol.
 +
 
 +
===Airprint from Mobile Devices===
 +
Avahi along with CUPS also provides the capability to print to just about any printer from airprint compatible mobile devices. In order to enable print capability from your device, simply create an avahi service file for your printer in /etc/avahi/services and restart avahi. An example of a generic services file for an HP-Laserjet printer would be similar to the following with the {{ic|name}}, {{ic|rp}}, {{ic|ty}}, {{ic|adminurl}} and {{ic|note}} fields changed.
 +
 
 +
{{hc|/etc/avahi/services/youFileName.service|<nowiki>
 +
<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
 +
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
 +
<service-group>
 +
  <name>yourPrnterName</name>
 +
  <service>
 +
    <type>_ipp._tcp</type>
 +
    <subtype>_universal._sub._ipp._tcp</subtype>
 +
    <port>631</port>
 +
    <txt-record>txtver=1</txt-record>
 +
    <txt-record>qtotal=1</txt-record>
 +
    <txt-record>rp=printers/yourPrnterName</txt-record>
 +
    <txt-record>ty=yourPrnterName</txt-record>
 +
    <txt-record>adminurl=http://198.168.7.15:631/printers/yourPrnterName</txt-record>
 +
    <txt-record>note=Office Laserjet 4100n</txt-record>
 +
    <txt-record>priority=0</txt-record>
 +
    <txt-record>product=(GPL Ghostscript)</txt-record>
 +
    <txt-record>printer-state=3</txt-record>
 +
    <txt-record>printer-type=0x801046</txt-record>
 +
    <txt-record>Transparent=T</txt-record>
 +
    <txt-record>Binary=T</txt-record>
 +
    <txt-record>Fax=F</txt-record>
 +
    <txt-record>Color=T</txt-record>
 +
    <txt-record>Duplex=T</txt-record>
 +
    <txt-record>Staple=F</txt-record>
 +
    <txt-record>Copies=T</txt-record>
 +
    <txt-record>Collate=F</txt-record>
 +
    <txt-record>Punch=F</txt-record>
 +
    <txt-record>Bind=F</txt-record>
 +
    <txt-record>Sort=F</txt-record>
 +
    <txt-record>Scan=F</txt-record>
 +
    <txt-record>pdl=application/octet-stream,application/pdf,application/postscript,image/jpeg,image/png,image/urf</txt-record>
 +
    <txt-record>URF=W8,SRGB24,CP1,RS600</txt-record>
 +
  </service>
 +
</service-group>
 +
</nowiki>}}
 +
 
 +
Alternatively, https://raw.github.com/tjfontaine/airprint-generate/master/airprint-generate.py can be used to generate Avahi service files. It depends on python2 and pycups. The script can be run using:
 +
 
 +
# python2 airprint-generate.py -d /etc/avahi/services
 +
 
 +
{{Note|If your printer under http://localhost:631/printers is "Not Shared", this python script won't output any file to /etc/avahi/services; in that case, you'll need to "Modify Printer" under one of the CUPS drop-down menus to turn sharing on.  If that doesn't work, check out the ArchWiki on [[CUPS printer sharing]].}}
 +
 
 +
=== Firewall ===
 +
 
 +
Be sure to open UDP port 5353 if you're using iptables:
 +
  # iptables -A INPUT -p udp -m udp --dport 5353 -j ACCEPT
 +
 
 +
If you're following the more-than-useful [[Simple stateful firewall]] format for your firewall:
 +
  # iptables -A UDP -p udp -m udp --dport 5353 -j ACCEPT
  
== Using Avahi ==
 
 
=== Obtaining IPv4LL IP address ===
 
=== Obtaining IPv4LL IP address ===
By default, if you are getting IP using DHCP, you are using {{Ic|dhcpcd}} package. It can attempt to obtain an IPv4LL address if it failed to get one via DHCP. By default this option is disabled. To enable it, comment noipv4ll string:
+
 
 +
{{Merge|dhcpcd|should be merged into the main page}}
 +
 
 +
By default, if you are getting IP using DHCP, you are using the {{Pkg|dhcpcd}} package. It can attempt to obtain an IPv4LL address if it failed to get one via DHCP. By default this option is disabled. To enable it, comment noipv4ll string:
  
 
{{hc|/etc/dhcpcd.conf|
 
{{hc|/etc/dhcpcd.conf|
Line 28: Line 147:
 
...}}
 
...}}
  
Alternatively, run {{Ic|avahi-autoipd}}, included in {{Ic|avahi}} package:
+
Alternatively, run {{ic|avahi-autoipd}}:
{{bc|# avahi-autoipd -D}}
+
# avahi-autoipd -D
  
=== Hostname resolution ===
+
=== Customizing Avahi ===
Avahi also allows you to access computers using their hostnames. '''Note:''' you must install {{Ic|nss-mdns}} for this to work.
+
==== Adding Services ====
  
Suppose you have machines with names maple, fig and oak, all running avahi. Avahi can be set up so that you do not have to manage a {{Ic|/etc/hosts}} file for each computer. Instead you can simply use {{Ic|maple.local}} to access whatever services {{Ic|maple}} has. However by default, .local querying is disabled in Arch Linux. To enable it edit the file {{Ic|/etc/nsswitch.conf}} and change the line:
+
Avahi advertises the services whose {{ic|*.service}} files are found in {{ic|/etc/avahi/services}}. If you want to advertise a service for which there is no {{ic|*.service}} file, it is very easy to create your own.
hosts: files dns
+
to
+
hosts: files mdns4_minimal [NOTFOUND=return] dns mdns4
+
  
Some users have noticed a slowdown in DNS querying; it was attributed to this change. (Reference: [https://bugs.launchpad.net/ubuntu/+source/avahi/+bug/94940 Ubuntu Bug 94940]). Using the following line instead (omit mdns4) solves the problem.
+
As an example, let's say you wanted to advertise a quote of the day (QOTD) service operating per [http://tools.ietf.org/html/rfc865 RFC865] on TCP port 17 which you are running on your machine
hosts: files mdns4_minimal [NOTFOUND=return] dns
+
  
Avahi includes several utilities which help you discover the services running on a network. For example, run
+
The first thing to do is to determine the {{ic|<type>}}. {{ic|man avahi.service}} indicates that the
avahi-browse -alr
+
type should be "the DNS-SD service type for this service. e.g. '_http._tcp'". Since the [http://www.dns-sd.org/ServiceTypes.html DNS-SD register was merged into the IANA register in 2010], we look for the service name on the [http://www.iana.org/assignments/service-names-port-numbers/service-names-port-numbers.xhtml IANA register] or in {{ic|/etc/services}} file. The service name shown there is {{ic|qotd}}. Since we're running QOTD on tcp, we now know the service is {{ic|_qotd._tcp}} and the port (per IANA and RFC865) is 17.
to discover services in your network.
+
  
The {{Ic|avahi-discover}} (Avahi Zeroconf Browser) shows the various services on your network. You can also browse SSH and VNC Servers using {{Ic|bssh}} and {{Ic|bvnc}} respectively.
+
Our {{ic|qotd.service}} file is thus:
  
There's a good list of software with Avahi support at their website: http://avahi.org/wiki/Avah4users
+
{{bc|<nowiki>
{{Note|{{Ic|avahi-discover}} needs pygtk and python-dbus to be installed.}}
+
<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
 +
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
  
===File sharing===
+
<!--
 +
  This file is part of avahi.
  
====NFS====
+
  avahi is free software; you can redistribute it and/or modify it
If you have an [[NFS]] share set up, you can use Avahi to be able to automount them in Zeroconf-enabled browsers (such as Konqueror on KDE and Finder on Mac OS X). Create a .service file in /etc/avahi/services with the following:
+
  under the terms of the GNU Lesser General Public License as
{{hc|/etc/avahi/services/nfs_Zephyrus_Music.service|<nowiki>
+
  published by the Free Software Foundation; either version 2 of the
<?xml version="1.0" standalone='no'?>
+
  License, or (at your option) any later version.
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
+
<service-group>
+
  <name replace-wildcards="yes">NFS Music Share on %h</name>
+
  <service>
+
    <type>_nfs._tcp</type>
+
    <port>2049</port>
+
    <txt-record>path=/data/shared/Music</txt-record>
+
  </service>
+
</service-group></nowiki>}}
+
  
The port is correct if you have ''insecure'' as an option in your /etc/exports; otherwise, it needs to be changed (note that ''insecure'' is needed for OS X clients). The path is the path to your export, or a subdirectory of it. For some reason the automount functionality has been removed from Leopard, however [http://www.macosxhints.com/article.php?story=20071116042238744 a script is available]. This was based upon [http://ubuntuforums.org/showthread.php?p=4387032#post4387032 this post].
+
  avahi is distributed in the hope that it will be useful, but
 +
  WITHOUT ANY WARRANTY; without even the implied warranty of
 +
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
 +
  General Public License for more details.
  
====Samba====
+
  You should have received a copy of the GNU Lesser General Public
 +
  License along with avahi; if not, write to the Free Software
 +
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 +
  02111-1307 USA.
 +
-->
  
{{Note|{{Pkg|samba}} package from extra repository build without an avahi suuport.}}
+
<!-- See avahi.service(5) for more information about this configuration file -->
{{hc|/etc/avahi/services/smb.service|<nowiki>
+
<?xml version="1.0" standalone='no'?>
+
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
+
<service-group>
+
    <name replace-wildcards="yes">Samba Shares on %h</name>
+
    <service>
+
        <type>_smb._tcp</type>
+
        <port>139</port>
+
    </service>
+
</service-group></nowiki>}}
+
{{Note|If you are trying to connect with OS X Lion the port needs to be 445, not 139.}}
+
  
====GShare====
+
<service-group>
You can grab {{aur|gshare}} from the [[Arch User Repository]] and have shared files between the LAN, with no configuration, no hours in samba hacking, no nothing - it just works.
+
  
====Vsftpd====
+
  <name replace-wildcards="yes">%h</name>
Sourced from [http://ubuntuforums.org/showthread.php?t=218630 ubuntuforums.org].
+
If you would rather use a regular ftp service, install vsftpd and avahi. Change the settings of vsftpd according to what is shown on the ubuntuforums page or according to your own personal preferences (See 'man vsftpd.conf).
+
  
Create a ftp.service file in /etc/avahi/services and paste in that file
+
  <service>
<?xml version="1.0" standalone='no'?>
+
     <type>_qotd._tcp</type>
    <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
+
     <port>17</port>
    <service-group>
+
  </service>
    <name>FTP file sharing</name>
+
    <service>
+
     <type>_ftp._tcp</type>
+
     <port>21</port>
+
    </service>
+
    </service-group>
+
When you are done, (re)start avahi-daemon and vsftpd in your /etc/rc.d directory.
+
/etc/rc.d/avahi-daemon restart
+
/etc/rc.d/vsftpd restart
+
  
After that you should be able to browse through the ftp server from another computer in your network. The steps shown in this section are created so that the ftp server is 'advertised' by avahi to the local Zeroconf network.
+
</service-group>
 +
</nowiki>}}
  
Unless you are using GNOME or KDE, you might not be able to log in to the ftp server directly through your file manager, and so you will have to use a ftp client pointed to the IP address of the server or the hostname of the machine (as shown in [[Avahi#Hostname_resolution|this section]]).
+
For more complicated scenarios, such as advertising services running on a different server, DNS sub-types and so on, consult {{ic|man avahi.service}}.
  
====Giver====
+
==== Modifying the service-types database ====
[http://code.google.com/p/giver/ Giver] is a mono program that allows simple file-sharing between two desktops when both are running Giver. All you need to do is click and drag the file to the name or picture of the person you wish to send the file to.
+
As noted above, avahi comes with tools to browse advertised services. Both {{ic|avahi-browse}} and {{ic|avahi-discover}} use a database file to furnish descriptions of the relevant service. That database contains the names of many, but not all, services.
  
A package is on the [https://aur.archlinux.org/packages.php?ID=17139 AUR].
+
Sadly, it doesn't contain the QOTD service we just created. Thus {{ic|avahi-browse -a}} would show the following ugly entry
  
Note that this depends on gnome-sharp, which has heavy GNOME dependencies.
+
+ wlp2s0 IPv4 MyServer                                        _qotd._tcp local
  
===Link-Local (Bonjour/Zeroconf) chat===
+
===== Getting the Sources =====  
  
Avahi can be used for bonjour protocol support under linux. The following chat clients support link-local chat.
+
First, download the files {{ic|build-db.in}} and {{ic|service-types}} files from the {{ic|service-type-database}} subdirectory in the [https://github.com/lathiat/avahi the avahi github mirror] to a build directory.
  
====Gajim====
+
wget https://raw.githubusercontent.com/lathiat/avahi/master/service-type-database/build-db.in
[http://www.gajim.org Gajim] is a Jabber/XMPP instant messenger client written in PyGTK. In the accounts setup just enable "Local" account.
+
wget https://raw.githubusercontent.com/lathiat/avahi/master/service-type-database/service-types
  
====Pidgin====
+
===== Modify the Sources =====
[http://www.pidgin.im Pidgin] is an instant messaging client that supports quite a few commonly used IM protocols. In addition to these, it supports Bonjour.
+
  
Just select 'Bonjour' as the protocol type when you add an account, and enter a username. The first and last name you enter in the 'Advanced' tab will be what the other person (whom you are chatting with) sees, and 'local alias' under 'User Options' in the 'Basic' tab will be what you see of your own name (you could try putting in something like I, me or myself).
+
Second, create the following script:
  
Once this is done, other Pidgin (iChat) users who are on the local network will see you and be able to chat with you. To implement file-sharing, you just send and receive files like you would do with a regular IM session.
+
#!/bin/bash
 +
sed -e 's,@PYTHON\@,/usr/bin/python2.7,g' \
 +
    -e 's,@DBM\@,gdbm,g' < build-db.in > build-db
 +
chmod +x build-db
  
====Kopete====
+
This mimics what the Makefile would do if one were building all of avahi. It creates a file named build-db.
[http://kopete.kde.org/ Kopete] is the KDE equivalent of Pidgin. It supports the Bonjour/Link-local XMPP protocol. One need to create an account in Kopete, by simply entering the desired name.
+
  
====Telepathy====
+
$./whatever_you_named_the_script.sh
[http://telepathy.freedesktop.org Telpathy] is a communication framework which supports different protocols using plugins. The {{Pkg|telepathy-salut}} plugin provides support for Bonjour/Link-Local XMPP protocol. Empathy is a GNOME front-end to Telepathy. Officially, KDE does not support Telepathy, but work is going on which will eventually replace Kopete. KDE Telepathy is available in extra.
+
$ ls
 +
build-db build-db.in service-types whatever_you_named_the_script.sh
  
===Airprint from Mobile Devices===
+
Third, make the changes needed to add your new QOTD service to the {{ic|service-types}} file. This file has one entry per line, with the entries in the format {{ic|type:Human Readable Description}}. Note that the human readable description can contain spaces.
Avahi along with CUPS also provides the capability to print to just about any printer from airprint compatible mobile devices. In order to enable print capability from your device, simply create an avahi service file for your printer in /etc/avahi/services and restart avahi. An example of a generic services file for an HP-Laserjet printer would be similar to the following with the name, rp, ty, adminurl and note fields changed. Save the file as /etc/avahi/services/youFileName.service:
+
  <?xml version="1.0" standalone='no'?><!--*-nxml-*-->
+
  <!DOCTYPE service-group SYSTEM "avahi-service.dtd">
+
  <service-group>
+
    <name>yourPrnterName</name>
+
    <service>
+
      <type>_ipp._tcp</type>
+
      <subtype>_universal._sub._ipp._tcp</subtype>
+
      <port>631</port>
+
      <txt-record>txtver=1</txt-record>
+
      <txt-record>qtotal=1</txt-record>
+
      <txt-record>rp=printers/yourPrnterName</txt-record>
+
      <txt-record>ty=yourPrnterName</txt-record>
+
      <txt-record>adminurl=http://198.168.7.15:631/printers/yourPrnterName</txt-record>
+
      <txt-record>note=Office Laserjet 4100n</txt-record>
+
      <txt-record>priority=0</txt-record>
+
      <txt-record>product=virtual Printer</txt-record>
+
      <txt-record>printer-state=3</txt-record>
+
      <txt-record>printer-type=0x801046</txt-record>
+
      <txt-record>Transparent=T</txt-record>
+
      <txt-record>Binary=T</txt-record>
+
      <txt-record>Fax=F</txt-record>
+
      <txt-record>Color=T</txt-record>
+
      <txt-record>Duplex=T</txt-record>
+
      <txt-record>Staple=F</txt-record>
+
      <txt-record>Copies=T</txt-record>
+
      <txt-record>Collate=F</txt-record>
+
      <txt-record>Punch=F</txt-record>
+
      <txt-record>Bind=F</txt-record>
+
      <txt-record>Sort=F</txt-record>
+
      <txt-record>Scan=F</txt-record>
+
      <txt-record>pdl=application/octet-stream,application/pdf,application/postscript,image/jpeg,image/png,image/urf</txt-record>
+
      <txt-record>URF=W8,SRGB24,CP1,RS600</txt-record>
+
    </service>
+
  </service-group>
+
  
Alternatively, https://raw.github.com/tjfontaine/airprint-generate/master/airprint-generate.py can be used to generate Avahi service files. It depends on python2 and pycups. The script can be run using: {{bc|# python2 airprint-generate.py -d /etc/avahi/services}}
+
In our example, we add the following entry to the end of the file:
  
=== Firewall ===
+
_qotd._tcp:Quote of the Day (QOTD) Server
  
Be sure to open UDP port 5353 if you're using iptables:
+
===== Build and Install the New Database =====
  # iptables -A INPUT -p udp -m udp --dport 5353 -j ACCEPT
+
  
If you're following the more-than-useful [[Simple Stateful Firewall]] format for your firewall:
+
Now run the {{ic|build-db}} python script (be sure to use python2 not python3). This will build the {{ic|service-types.db}} file. Check to make sure it's been built and use {{ic|gdbmtools}} to make sure the new database is loadable and contains the new entry:
  # iptables -A UDP -p udp -m udp --dport 5353 -j ACCEPT
+
 
 +
$/usr/bin/python2.7 build-db
 +
$ls
 +
build-db build-db.in service-types service-types.db whatever_you_named_the_script.sh
 +
$gdbmtool service-types.db
 +
 +
Welcome to the gdbm tool.  Type ? for help.
 +
 +
gdbmtool>fetch _qotd._tcp
 +
Quote of the Day (QOTD) Server
 +
gdbmtool>quit
 +
 
 +
Now copy the old database to a backup location, move the new database to the live directory and use {{ic|avahi-browse}} database dump command to make sure avahi sees the new entry:
 +
 
 +
$cp /usr/lib/avahi/service-types.db /backup-directory
 +
$sudo cp /build-directory/service-types.db /usr/lib/avahi/service-types.db
 +
$avahi-browse -b | grep QOTD
 +
Quote of the Day (QOTD) Server
 +
 
 +
The entry in {{ic|avahi-browse}} should now be:
 +
 
 +
+ wlp2s0 IPv4 MyServer                                        Quote of the Day (QOTD) Server local
  
 
==See also==
 
==See also==
 
* [http://avahi.org/ Avahi] - Official project website
 
* [http://avahi.org/ Avahi] - Official project website
* [http://en.wikipedia.org/wiki/Avahi_%28software%29 Wikipedia entry]
+
* [[wikipedia:Avahi (software)|Wikipedia entry]]
 
* [http://www.apple.com/support/downloads/bonjourforwindows.html Bonjour for Windows] - Enable Zeroconf on Windows
 
* [http://www.apple.com/support/downloads/bonjourforwindows.html Bonjour for Windows] - Enable Zeroconf on Windows
 
* http://www.zeroconf.org/
 
* http://www.zeroconf.org/

Latest revision as of 23:33, 17 May 2016

From Wikipedia:Avahi (software):

"Avahi is a free Zero-configuration networking (zeroconf) implementation, including a system for multicast DNS/DNS-SD service discovery. It allows programs to publish and discover services and hosts running on a local network with no specific configuration. For example you can plug into a network and instantly find printers to print to, files to look at and people to talk to. It is licensed under the GNU Lesser General Public License (LGPL)."

Installation

Install the avahi package.

You can manage the Avahi daemon with avahi-daemon.service using systemd.

Using Avahi

Hostname resolution

Avahi provides local hostname resolution using a "hostname.local" naming scheme. To enable it, install the nss-mdns package and start avahi-daemon.service.

Then, edit the file /etc/nsswitch.conf and change the line:

hosts: files dns myhostname

to:

hosts: files mdns_minimal [NOTFOUND=return] dns myhostname
Note: If you experience slowdowns in resolving .local hosts try to use mdns4_minimal instead of mdns_minimal.

Additional info about mdns

The mdns_minimal module handles queries for the .local TLD only. Note the [NOTFOUND=return], which specifies that if mdns_minimal cannot find *.local, it will not continue to search for it in dns, myhostname, etc. In case you have configured Avahi to use a different TLD, you should replace mdns_minimal [NOTFOUND=return] with the full mdns module. There also are IPv4-only and IPv6-only modules mdns[46](_minimal).

Tools

Avahi includes several utilities which help you discover the services running on a network. For example, run

$ avahi-browse -alr

to discover services in your network.

The Avahi Zeroconf Browser (avahi-discover – note that it needs avahi's optional dependencies pygtk and python2-dbus) shows the various services on your network. You can also browse SSH and VNC Servers using bssh and bvnc respectively.

There's a good list of software with Avahi support at their website: http://avahi.org/wiki/Avah4users

File sharing

NFS

If you have an NFS share set up, you can use Avahi to be able to automount them in Zeroconf-enabled browsers (such as Konqueror on KDE and Finder on OS X).

Create a .service file in /etc/avahi/services with the following contents:

/etc/avahi/services/nfs_Zephyrus_Music.service
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name replace-wildcards="yes">NFS Music Share on %h</name>
  <service>
    <type>_nfs._tcp</type>
    <port>2049</port>
    <txt-record>path=/data/shared/Music</txt-record>
  </service>
</service-group>

The port is correct if you have insecure as an option in your /etc/exports; otherwise, it needs to be changed (note that insecure is needed for OS X clients). The path is the path to your export, or a subdirectory of it. For some reason the automount functionality has been removed from Leopard, however a script is available. This was based upon this post.

Samba

With the Avahi daemon running on both the server and client, the file manager on the client should automatically find the server.

Vsftpd

You can also auto-discover regular FTP servers, such as vsftpd. Install the vsftpd package and change the settings of vsftpd according to your own personal preferences (see this thread on ubuntuforums.org or man vsftpd.conf).

Create a .service file in /etc/avahi/services with the following contents:

/etc/avahi/services/ftp.service
<?xml version="1.0" standalone='no'?>
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name>FTP file sharing</name>
  <service>
    <type>_ftp._tcp</type>
    <port>21</port>
  </service>
</service-group>

When you are done, restart the avahi-daemon.service and vsftpd.service services.

The FTP server should now be advertised by Avahi. You should now be able to find the FTP server from a file manager on another computer in your network. You might need to enable #Hostname resolution on the client.

Link-Local (Bonjour/Zeroconf) chat

Avahi can be used for bonjour protocol support under linux. Check Wikipedia:Comparison of instant messaging clients or List of applications#Instant messaging for a list of clients supporting the bonjour protocol.

Airprint from Mobile Devices

Avahi along with CUPS also provides the capability to print to just about any printer from airprint compatible mobile devices. In order to enable print capability from your device, simply create an avahi service file for your printer in /etc/avahi/services and restart avahi. An example of a generic services file for an HP-Laserjet printer would be similar to the following with the name, rp, ty, adminurl and note fields changed.

/etc/avahi/services/youFileName.service
<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">
<service-group>
  <name>yourPrnterName</name>
  <service>
    <type>_ipp._tcp</type>
    <subtype>_universal._sub._ipp._tcp</subtype>
    <port>631</port>
    <txt-record>txtver=1</txt-record>
    <txt-record>qtotal=1</txt-record>
    <txt-record>rp=printers/yourPrnterName</txt-record>
    <txt-record>ty=yourPrnterName</txt-record>
    <txt-record>adminurl=http://198.168.7.15:631/printers/yourPrnterName</txt-record>
    <txt-record>note=Office Laserjet 4100n</txt-record>
    <txt-record>priority=0</txt-record>
    <txt-record>product=(GPL Ghostscript)</txt-record>
    <txt-record>printer-state=3</txt-record>
    <txt-record>printer-type=0x801046</txt-record>
    <txt-record>Transparent=T</txt-record>
    <txt-record>Binary=T</txt-record>
    <txt-record>Fax=F</txt-record>
    <txt-record>Color=T</txt-record>
    <txt-record>Duplex=T</txt-record>
    <txt-record>Staple=F</txt-record>
    <txt-record>Copies=T</txt-record>
    <txt-record>Collate=F</txt-record>
    <txt-record>Punch=F</txt-record>
    <txt-record>Bind=F</txt-record>
    <txt-record>Sort=F</txt-record>
    <txt-record>Scan=F</txt-record>
    <txt-record>pdl=application/octet-stream,application/pdf,application/postscript,image/jpeg,image/png,image/urf</txt-record>
    <txt-record>URF=W8,SRGB24,CP1,RS600</txt-record>
  </service>
</service-group>

Alternatively, https://raw.github.com/tjfontaine/airprint-generate/master/airprint-generate.py can be used to generate Avahi service files. It depends on python2 and pycups. The script can be run using:

# python2 airprint-generate.py -d /etc/avahi/services
Note: If your printer under http://localhost:631/printers is "Not Shared", this python script won't output any file to /etc/avahi/services; in that case, you'll need to "Modify Printer" under one of the CUPS drop-down menus to turn sharing on. If that doesn't work, check out the ArchWiki on CUPS printer sharing.

Firewall

Be sure to open UDP port 5353 if you're using iptables:

 # iptables -A INPUT -p udp -m udp --dport 5353 -j ACCEPT

If you're following the more-than-useful Simple stateful firewall format for your firewall:

 # iptables -A UDP -p udp -m udp --dport 5353 -j ACCEPT

Obtaining IPv4LL IP address

Merge-arrows-2.pngThis article or section is a candidate for merging with dhcpcd.Merge-arrows-2.png

Notes: should be merged into the main page (Discuss in Talk:Avahi#)

By default, if you are getting IP using DHCP, you are using the dhcpcd package. It can attempt to obtain an IPv4LL address if it failed to get one via DHCP. By default this option is disabled. To enable it, comment noipv4ll string:

/etc/dhcpcd.conf
...
#noipv4ll
...

Alternatively, run avahi-autoipd:

# avahi-autoipd -D

Customizing Avahi

Adding Services

Avahi advertises the services whose *.service files are found in /etc/avahi/services. If you want to advertise a service for which there is no *.service file, it is very easy to create your own.

As an example, let's say you wanted to advertise a quote of the day (QOTD) service operating per RFC865 on TCP port 17 which you are running on your machine

The first thing to do is to determine the <type>. man avahi.service indicates that the type should be "the DNS-SD service type for this service. e.g. '_http._tcp'". Since the DNS-SD register was merged into the IANA register in 2010, we look for the service name on the IANA register or in /etc/services file. The service name shown there is qotd. Since we're running QOTD on tcp, we now know the service is _qotd._tcp and the port (per IANA and RFC865) is 17.

Our qotd.service file is thus:

<?xml version="1.0" standalone='no'?><!--*-nxml-*-->
<!DOCTYPE service-group SYSTEM "avahi-service.dtd">

<!--
  This file is part of avahi.

  avahi is free software; you can redistribute it and/or modify it
  under the terms of the GNU Lesser General Public License as
  published by the Free Software Foundation; either version 2 of the
  License, or (at your option) any later version.

  avahi is distributed in the hope that it will be useful, but
  WITHOUT ANY WARRANTY; without even the implied warranty of
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  General Public License for more details.

  You should have received a copy of the GNU Lesser General Public
  License along with avahi; if not, write to the Free Software
  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  02111-1307 USA.
-->

<!-- See avahi.service(5) for more information about this configuration file -->

<service-group>

  <name replace-wildcards="yes">%h</name>

  <service>
    <type>_qotd._tcp</type>
    <port>17</port>
  </service>

</service-group>

For more complicated scenarios, such as advertising services running on a different server, DNS sub-types and so on, consult man avahi.service.

Modifying the service-types database

As noted above, avahi comes with tools to browse advertised services. Both avahi-browse and avahi-discover use a database file to furnish descriptions of the relevant service. That database contains the names of many, but not all, services.

Sadly, it doesn't contain the QOTD service we just created. Thus avahi-browse -a would show the following ugly entry

+ wlp2s0 IPv4 MyServer                                        _qotd._tcp local
Getting the Sources

First, download the files build-db.in and service-types files from the service-type-database subdirectory in the the avahi github mirror to a build directory.

wget https://raw.githubusercontent.com/lathiat/avahi/master/service-type-database/build-db.in
wget https://raw.githubusercontent.com/lathiat/avahi/master/service-type-database/service-types
Modify the Sources

Second, create the following script:

#!/bin/bash
sed -e 's,@PYTHON\@,/usr/bin/python2.7,g' \
    -e 's,@DBM\@,gdbm,g' < build-db.in > build-db
chmod +x build-db

This mimics what the Makefile would do if one were building all of avahi. It creates a file named build-db.

$./whatever_you_named_the_script.sh
$ ls
build-db build-db.in service-types whatever_you_named_the_script.sh

Third, make the changes needed to add your new QOTD service to the service-types file. This file has one entry per line, with the entries in the format type:Human Readable Description. Note that the human readable description can contain spaces.

In our example, we add the following entry to the end of the file:

_qotd._tcp:Quote of the Day (QOTD) Server
Build and Install the New Database

Now run the build-db python script (be sure to use python2 not python3). This will build the service-types.db file. Check to make sure it's been built and use gdbmtools to make sure the new database is loadable and contains the new entry:

$/usr/bin/python2.7 build-db
$ls
build-db build-db.in service-types service-types.db whatever_you_named_the_script.sh
$gdbmtool service-types.db

Welcome to the gdbm tool.  Type ? for help.

gdbmtool>fetch _qotd._tcp
Quote of the Day (QOTD) Server
gdbmtool>quit

Now copy the old database to a backup location, move the new database to the live directory and use avahi-browse database dump command to make sure avahi sees the new entry:

$cp /usr/lib/avahi/service-types.db /backup-directory
$sudo cp /build-directory/service-types.db /usr/lib/avahi/service-types.db
$avahi-browse -b | grep QOTD
Quote of the Day (QOTD) Server

The entry in avahi-browse should now be:

+ wlp2s0 IPv4 MyServer                                        Quote of the Day (QOTD) Server local

See also