How can I get the /etc/hosts file to refer to another configuration file for it's list of hosts?

Example /etc/hosts:

 ## My Hosts
 127.0.0.1   localhost
 255.255.255.255 broadcasthost

 #Other Configurations
 <Link to /myPath/to/MyConfig/ConfigFile.txt>

 #Other Addresses
 3.3.3.3 MyAwesomeDomain.com
 4.4.4.4 SomeplaceIWantToGoTo.com

ConfigFile.txt

##My additional Hosts
1.1.1.1 SomeLocation.com
2.2.2.2 AnotherLocation.com

How do I add a link/Reference to /etc/hosts file such that ConfigFile.txt will be loaded?

share|improve this question
up vote 27 down vote accepted

You can't. The format for /etc/hosts is quite simple, and doesn't support including extra files.

There are a couple approaches you could use instead:

  • Set up a (possibly local-only) DNS server. Some of these give a lot of flexibility, and you can definitely spread your host files over multiple files, or even machines. Others (such as dnsmasq) offer less (but still sufficient) flexibility, but are easy to set up. If you're trying to include the same list of hosts on a bunch of machines, then DNS is probably the right answer.

  • Set up some other name service (NIS, LDAP, etc.). Check the glibc NSS docs for what is supported. Personally, I think you should use DNS in most all cases.

  • Make yourself an /etc/hosts.d directory or similar, and write some scripts to concatenate them all together (most trivial: cat /etc/hosts.d/*.conf > /etc/hosts, though you'll probably want a little better to e.g., sort), and run that script at boot, or from cron, or manually whenever you update the files.

Personally, at both home and work, to have machine names resolvable from every device, I run BIND 9. That does involve a few hours to learn, though.

share|improve this answer
    
Thanks! I was wondering why each attempt was failing. It would be so much easier if it were possible, but I'll use the /etc/hosts.d – DogEatDog Jan 7 '13 at 16:53
5  
@DogEatDog I recommend running a local DNS server. Dnsmasq is very easy to set up; on user-friendly distributions, you install the package and it just works. – Gilles Jan 7 '13 at 19:17
1  
+1 for recommending a local DNS Server for that. – Bonsi Scott Jan 7 '13 at 20:56
    
@Gilles has it right - dnsmasq likely does exactly what you want on a home network. It lets you assign static IPs to all your LAN resources by MAC address, grant them hostnames, and resolve requests for them - all while letting unknown hosts pass through to an upstream DNS server. – moodboom Nov 11 '16 at 17:46
    
@moodboom & Gilles Thank you for suggesting that, I've noted it in the answer. Not sure if I was using dnsmasq back in 2013, but I certainly am now. – derobert Nov 11 '16 at 18:05

Similar to this Can I create a user-specific hosts file to complement /etc/hosts?. AFAIK it is not possible to get external file associated with /etc/hosts file.

share|improve this answer

I’ve had a similar requirement for me to work seamlessly from different locations with dynamic IPs, especially since I use virtual machines. My simple solution to this is as follows:

  1. Create a script to update host entries for each entity. E.g., if I have a host called ‘myhost.net’, with sub-domains ‘app.myhost.net’, I would invoke that script and supply an IP address that will be written for each domain and saved in a host file called ‘my host’. The same script has a flag to delete or update a single host file.

  2. Create another script to merge those files into /etc/hosts. This script will find all the host files you’ve created and merge them into the single hosts file at /etc/hosts.

This has worked for me perfectly. I work remotely and sometimes need to move between locations for any reason or another, and would be burdened by the demand to edit that one file so many times. Instead I just run ONE script. Yes, one script. The first script automatically runs the other one to do the merging automatically.

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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