2

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?

Jeff Schaller
  • 67,283
  • 35
  • 116
  • 255
  • 5
    Related: http://unix.stackexchange.com/questions/10438/can-i-create-a-user-specific-hosts-file-to-complement-etc-hosts – ilkkachu Oct 23 '16 at 17:17

2 Answers2

1

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

steve
  • 21,892
  • 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
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...

mikejonesey
  • 2,030