1

This question already has an answer here:

To modify a host's resolved address we can change /etc/hosts
I want to have that functionality when running a single script without actually modifying /etc/hosts
Something

$HOST=foo.bar:10.0.1.256 ping foo.bar

would be the same as having an entry in /etc/hosts 10.0.1.256 foo.bar
and $ ping foo.bar

Is this doable?

marked as duplicate by Jeff Schaller, HalosGhost, John1024, techraf, Gilles command-line Oct 23 '16 at 22:47

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

0

There are no user host files, a complex bind setup would work hosting the application behind a different internal ip... however... the application should be able to skip DNS...

If you are looking to do this for an application that is making http/https calls, you can skip the DNS entirely. When making a http request the domain is copied to the headers, you can skip this step by overriding the header...

for example;

wget --header="Host: www.example.com" "http://127.0.0.1/bob/is/cool"

This will send a http request to localhost, however the http request would be for www.example.com, not 127.0.0.1...

(bypassed dns and made a valid http call for the correct domain).

If for some reason it's an alternate tcp service that has no identifier like a host on the request, then a crazy bind setup would work, (deny the src ip of the application, it'll resolve on a different dns service).

Another solution would be using kernel firewall...

split the application out to a different lan or maybe loop back address, then you will be able to DNAT to a different IP based off the source packet address...

0

Duplicate of Can I create a user-specific hosts file to complement /etc/hosts?, where it explains use of HOSTALIASES.

Essentially, you set a HOSTALIASES environment variable which points at your very own hosts file.

More information at hostname(7) man page

  • HOSTALIASES, does what it says on the tin, "host alias", it allows you to map say mikejonesey to www.mikejonesey,co.uk, but not a host to an IP. But quite right a duplicate question, I like the libnss_files.so. trick... brilliant! – mikejonesey Oct 24 '16 at 21:02

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