56

I want to set a dns record that my browser will use, but I don't have root access, so I can't modify /etc/hosts. I need to do this for testing vhosts with apache, whose dns hasn't yet been set up. I have access to firefox, and chrome, so if there's a plugin that could facilitate it; or other options are helpful.

update: the alternative to overriding the dns is probably modifying the HTTP headers, if the correct ones are sent to apache, the correct content should be returned.

xenoterracide
  • 59,188
  • 74
  • 187
  • 252

9 Answers9

75

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

11

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
chris
  • 1,142
6

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.

jsbillings
  • 24,406
5

Check out following question at superuser:

https://superuser.com/questions/184643/override-dns-in-firefox

If the discussed options and the SO link are not viable solutions then check out:

https://superuser.com/questions/100239/hostname-override-in-firefox

Especially check out:

https://addons.mozilla.org/en-US/firefox/addon/redirector/

It sounds like this addon could help - but I depends on its actual implementation.

maxschlepzig
  • 57,532
  • unfortunately much like the op in that question none of these seem viable. – xenoterracide Feb 04 '11 at 10:28
  • @xenoterracide - I updated my answer - perhaps the redirector add-on is an option? – maxschlepzig Feb 04 '11 at 19:38
  • I don't think so from the way it's worded.. what I really need to do is modify the HTTP headers that are sent... as an alternative option to hacking around using hosts – xenoterracide Feb 06 '11 at 05:14
  • I poked around the SO solution (TamperData) but I didn't see how to modify the host in that. I wonder if the live http headers thing really does allow me to modify headers, it says it's a viewer. – xenoterracide Feb 06 '11 at 05:18
  • I think the Modify Headers extension listed down a bit in the SO one works... if you use Host hostname... in it... not yet tested... outside of breaking sites using it. – xenoterracide Feb 06 '11 at 05:32
2

To test vhosts, you might set the Apache server as the proxy in Firefox. The name you entered will be forwarded to Apache. This will break browsing other sites so set the proxy back when you are done.

BillThor
  • 8,965
0

overlayfs + chroot : )

problem: mount requires root access

#!/bin/sh

D=/tmp/my-overlay

mkdir -p $D/{upper,work,merged} mount -t overlay overlay -o lowerdir=/,upperdir=$D/upper,workdir=$D/work $D/merged

echo '1.2.3.4 myhost' >>$D/merged/etc/hosts

chroot $D/merged wget http://myhost/

milahu
  • 208
0

It is possible to override dns resolution in google chrome in windows, mac and linux without root/admin access. All we need to do is to start google chrome process with a switch and supply the required DNS entries there.

https://devopslife.io/resolve-dns-locally-using-google-chrome/

-1

I needed something similar for ssh, scp etc. for a server which keeps changing IP every couple weeks (and our IT has a couple weeks delay in setting the host). I solved it using a special variable for this server: export FOO='131.227.aaa.bbb and then I can do just ssh bar@$FOO and I change the IP in my ~/.bashrc any time it changes...

BIOStheZerg
  • 181
  • 4
-2

Unlucky not, you cannot, except you write your own internet browser.

If you have to do some tests you need a test machine, whatever is a virtual machine or a real one, so you have to ask your Unix admin (or hosting provider) how you can put in place a development environment.

You can also install a VM on your PC, install a Linux distribution, Apache and test your changes (it's not as hard as it sounds)

Update

To better explain, each application is written using the standard libraries, this way nobody has to rewrite the low level library and functions like the gethostbyname().

These functions normally are set to use file (/etc/hosts) and DNS, so, unlucky, if you need that your browser will resolve a name than the one is set in the /etc/hosts you don't have too many alternatives.

  1. You can set up your own DNS server and ask who has root rights to change the /etc/resolve.conf
  2. You can install a proxy server like DeleGate and set some rules for your VirtualHost
  3. Ask sudo rights on that customer host
  4. Change the behavior of the application you are using to test the connection (I guess the browser), for example https://addons.mozilla.org/en-US/firefox/addon/switchhosts/
tmow
  • 1,255
  • 4
    -1 worded poorly and patronizing. I AM the hosting provider, and it's the damn customers site on our server that I need to test. We have a network boot linux that I do not have admin access to, and I don't believe I can boot a VM on it. – xenoterracide Feb 04 '11 at 10:01
  • @xenoterracide there is not too much to say. It's really not feasible what you are asking for, except you change the way a SOCKET is created. – tmow Feb 04 '11 at 13:01
  • 1
    @xenoterracide BTW, there wasn't any intention to offend. – tmow Feb 04 '11 at 13:08
  • well as the hosting provider... I have "sudo rights" actually, I have real root on every box, and full access to everything... except my desktop (eyeroll) but if they haven't wanted to change their nameservers yet.. I can't help that... – xenoterracide Feb 06 '11 at 05:02
  • 1
    with LD_PRELOAD you can 'override' specific 'low level' functions and do what you want, eg. telling the program about your own '/etc/hosts' on open() and any other low level call .. as long as dynamic loading of code is invovled (.so) – akira Feb 06 '11 at 05:34