3

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?

1

2 Answers 2

2

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

1
  • 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! Commented Oct 24, 2016 at 21:02
1

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...

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.