Take the 2-minute tour ×

I have 2 linux server A and B which share common directory. I have to create symbolic link for /etc/hosts file for each server with one common file . when ever I update that common file which is shared by both server they are able to know what changes in file and update /etc/hosts automatically . Is it possible using symbolic link or any other option ?

share|improve this question

2 Answers 2

Instead of having a symlink to a (remote) hosts file, better to update the local copy with the remote one's information. That way in case of network outage, or the "master" host is down, you still have the local /etc/hosts file.

You should, in each local /etc/hosts file, a "structure" to separate local and global informations.

exemple of local /etc/hosts file :

# local informations: localhost otherhandylocalalias
# specificly needed aliases for this host: some_specific.host.domain

# before:local after:global informations (copied from: ...........)


And you could replace the rest of the file (everyuthing after the "# before: local after:global ...." line) with the "global" information, taken either from a remote file dedicated to host the "validated" global information (ex: remotehost:/etc/hosts_global_informations.master) file, or retrieved ONLY from from the "global part" of the master /etc/hosts file (ex: remotehost:/etc/hosts) .

The specific to retrieve those infos are various, depending on what's available to you.

An example: in your shell you could do it manually as such: (this assume that all the /etc/hosts file contains the same separator, which is a line matching the "zesep" regexp:

export zesep='# before:local after:global .*'
   #this allows you to complete the line in each /etc/hosts with non-global informations!

awk "/${zesep}/,0 { continue } 1 { print }" </etc/hosts >/etc/newlocalhosts 
ssh user@remotehost 'cat /etc/hosts' | awk "/${zesep}/,0 { print } " >> /etc/newlocalhosts

And then replace /etc/hosts with /etc/newlocalhosts ONCE YOU ARE SURE that the new one is correct (beware: check that the ssh was ok, for example, as it could fail if the network fails!). If it is correct you could:

cat /etc/newlocalhosts > /etc/hosts  #this will keep the settings (mode, etc) of /etc/hosts but replace its content. 
share|improve this answer

Using traditional symbolic links, this isn't possible. You're looking for some sort of configuration management software like Puppet or cfengine to push out /etc/hosts when you update it.

share|improve this answer

Your Answer


