Can I create override dns similar to writing in /etc/hosts without root access

I was looking for a way to run a program with modified DNS resolution for testing purposes. For me, the solution was using the HOSTALIASES environment variable:

$ echo "foo www.google.com" >> ~/.hosts
$ HOSTALIASES=~/.hosts wget foo

See hostname(7).

(Side note: In the example the HOSTALIASES environment variable only affects the wget process. Of course, you can export HOSTALIASES to have it take effect for all subprocesses of the current shell.)


You can write a wrapper around the libc function to resolve hostnames and look them up in a different file than /etc/hosts. Then run any application you'd like to use your hosts file with

LD_PRELOAD=wrapper.so firefox

I think the best way to do this would be to set up a SOCKS5 proxy and tell firefox to send the DNS requests over the SOCKS5 proxy (network.proxy.socks_remote_dns). You could set up a socks5 proxy with openssh fairly easily (the -D option) and have a remote host running with a custom /etc/hosts, or something like DNSMasq for more complex DNS settings. Effectively, though, this is pushing the configuration of the DNS settings to a system you can make system-wide changes to.