Longshot; Conditional '/etc/hosts' depending on an iface's subnet?

Eric Battersby gyre-Ja3L+HSX0kI at public.gmane.org
Mon Nov 30 23:19:39 UTC 2009


On Mon, 30 Nov 2009, Madison Kelly wrote:

>  I really doubt this is possible, but is there a way to have entries in 
> '/etc/hosts' return an IP depending on what a given interface's subnet is?
>
>  The reason I ask is that when I am at work, I want a given name to resolve 
> the internal IP, but when I am out I need it to resolve to it's external IP. 
> I know I could do this with an internal DNS, but I am hoping there might be 
> an easier way to do it. Currently I just use two different names.

Are these unqualified hostnames?
I would recommend not putting fully qualified hostnames in
'/etc/hosts' in case the real IP address changes.

(1) Why not create two '/etc/hosts' files: '/etc/hosts.work',
and '/etc/hosts.home'? Then copy the appropriate one into
'/etc/hosts' when you bring the network up.

(2) You could also periodically update '/etc/hosts' with a
simple update script, that changes the IP address in question.
For example:
    cd /etc
    update_hosts <hosts >hosts.new
    cmp -s hosts hosts.new || mv hosts.new hosts

I use this technique to allow dynamic hostnames access my
server programs (eg: sshd) while running TCP Wrappers by
creating unqualified hostnames that correspond to the fully
qualified hostnames.

TCP Wrappers will fail if the reverse IP address look up
fails, but it won't fail if the IP address is in '/etc/hosts'.
Perhaps surprising, the TCP Wrappers file, '/etc/hosts.allow',
can still use the fully-qualified hostnames.

--
Eric Battersby.

--
The Toronto Linux Users Group.      Meetings: http://gtalug.org/
TLUG requests: Linux topics, No HTML, wrap text below 80 columns
How to UNSUBSCRIBE: http://gtalug.org/wiki/Mailing_lists





More information about the Legacy mailing list