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

$HOST=foo.bar: ping foo.bar

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

Is this doable?


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

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

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


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.