Take the 2-minute tour ×
Super User is a question and answer site for computer enthusiasts and power users. It's 100% free, no registration required.

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
add comment

2 Answers

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:

127.0.0.1 localhost otherhandylocalalias
# specificly needed aliases for this host:
1.2.3.4 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
add comment

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
add comment

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.