I'd like to add shorthands for various FQDNs into the /etc/hosts file, such as:

pyrrha.compsci.university.org. pyrrha

Is there a way to force the local DNS resolver to process the /etc/hosts entries recursively?

Why? Inserting full FQDNs into every config file feels redundant. Creating an alias in a central location solves this and also adds a level of abstraction, which allows for a simple update of the FQDN behind the alias, should it ever change.

  • Two loosely related questions bundled into one thread isn’t StackExchange’s way. – Incnis Mrsi Sep 12 '15 at 17:55
  • Which is why I removed the second question at August the 11th? – Witiko Sep 12 '15 at 22:20
  • Oops… now see it. Sorry for inconvenience. – Incnis Mrsi Sep 12 '15 at 22:29

resolv.conf allows you to specify searchdomains. An entry like the following:

search cse.iitb.ac.in it.iitb.ac.in iitb.ac.in

Allows me to:

$ ping -c1 www 
PING www.cse.iitb.ac.in ( 56(84) bytes of data.
64 bytes from cse.iitb.ac.in ( icmp_seq=1 ttl=64 time=0.803 ms

--- www.cse.iitb.ac.in ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.803/0.803/0.803/0.000 ms
$ ping -c1 cygnus
PING cygnus.it.iitb.ac.in ( 56(84) bytes of data.
64 bytes from cygnus.it.iitb.ac.in ( icmp_seq=1 ttl=61 time=0.688 ms

--- cygnus.it.iitb.ac.in ping statistics ---
1 packets transmitted, 1 received, 0% packet loss, time 0ms
rtt min/avg/max/mdev = 0.688/0.688/0.688/0.000 ms

resolv.conf doesn't apply on entries in /etc/hosts, but if your name is being resolved using a DNS server, then searchdomain might be what you're looking for.

  • This seems to be the next best thing, although I still long for recursive aliases. – Witiko Aug 11 '15 at 20:17
  • @Witiko one comment is enough. :) – muru Aug 11 '15 at 20:17

The first part of your question asks for recursive definitions in /etc/hosts. This is not necessary as it is already possible to specify multiple hostnames there:  pyrrha.compsci.university.org pyrrha

Please read the manual page (man 5 hosts) for the syntax of the hosts file.

The second part of your question is a duplicate of IPv6 Zone ID in /etc/hosts so please look there for answers.

  • Regarding the first part: Suppose that the IP addresses of the FQDNs aren't static and should be retrieved via DNS lookup. I would just like to be able to create aliases on top of that, so that the resolver first translates pyrrha into pyrrha.compsci.university.org and then it consults the DNS cache / an external DNS server to translate pyrrha.compsci.university.org into As for the second part: I removed the second part of the question. The wrapper provided in the accepted answer is useful; hopefully the functionality will soon be a part of glibc. – Witiko Aug 11 '15 at 17:23
  • Configuring aliases is not what /etc/hosts is for, so that won't work. – Sander Steffann Aug 11 '15 at 17:58

Original poster’s problem seemingly isn’t about /etc/hosts, but about a new software layer: a locally managed list of aliases of network hosts.

Yes, there is a ready-to-deploy solution.

  1. Install a nameserver (e.g. BIND).
  2. Make it serve a new (private) zone .host (zone "host" {… ), with a master zone file.
  3. Make search host (or possibly even domain host) the first line of /etc/resolv.conf and nameserver its second line. If several machines are about to use the system, then use IP address of the nameserver on machines other than it.
  4. Place aliases to the zone file in the DNS records’ semantic (namely, IN CNAME for an alias proper and IN A(IN AAAA) for a hosts-style explicit IPv4(v6) address).
  5. Reload zones.

All applications started with the new resolv.conf can access your aliases without .host appended. Note about the point 4.: CNAMEing to a domain name in another zone requires .-terminated FQDN syntax for the target. For example:

irc     IN  CNAME   h.mixtery.name.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.