2

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.

Witiko
  • 791

3 Answers3

3

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 (10.105.1.3) 56(84) bytes of data.
64 bytes from cse.iitb.ac.in (10.105.1.3): 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 (10.129.1.1) 56(84) bytes of data.
64 bytes from cygnus.it.iitb.ac.in (10.129.1.1): 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.

muru
  • 72,889
1

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:

69.162.80.52  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.

Sander Steffann
  • 1,496
  • 8
  • 11
  • 1
    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 69.162.80.52. 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
1

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 127.0.0.1 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.
Incnis Mrsi
  • 1,988