9

I've a website running on my computer, and I want to test it from my nexus 7 tablet. I.e. over the local wifi LAN. If I type in the IP address it connects, but shows the wrong web site, because I'm using named virtual hosting.

So, what I want to do is be able to type "mysite.local" in to Chrome/Firefox on the Nexus, have them convert that to 10.1.2.3 (or whatever the local IP address is), then call the 10.1.2.3 server giving mysite.local as the Host to request.

My Question: Does an Android 4.x tablet have an /etc/hosts files, or equivalent, that I can edit? (Without having to root the tablet, or anything like that.)

On my linux computers I do this by adding an entry in /etc/hosts:

10.1.2.3  mysite.local

(Or Windows\System32\drivers\etc\hosts on a Windows machine does the same.)

BTW, this is a semi-duplicate of Manually set a hostname for IP address (i.e. /etc/hosts equiv.?), but that question was for Android 2.2, where apparently it was not possible. (One of the alternative ideas suggested there was configure DNS at the local router, but as far as I can tell my router does not do DNS locally so does not have that option. I could set up my own DNS server to do this, but that feels like a Big Job Solution.)

  • That's rather a question for Server Fault or Super User. And you should setup those host names with your local server (which is presented by your DHCP server as DNS node). – Izzy Jul 29 '14 at 11:00
  • 1
    @Izzy Thanks Izzy, but I know how to configure /etc/hosts on servers. What I don't know how to do is find/edit the equivalent on an Android 4.x tablet (or even if there is an equivalent without rooting it). (My DHCP server, i.e. the wifi router, appears to offer no options to add custom names: I do have the option to specify a custom DNS server but that is a muddy pool I didn't really want to put my hands in to.) – Darren Cook Jul 29 '14 at 11:06
  • You got me wrong, Darren: I didn't suggest editing /etc/hosts, but configuring your "name server" (e.g. DNSMasq, or whatever you use). Your router might give you a place for that. /etc/hosts needs to be edited on each machine otherwise – and the corresponding file on Android can only be edited with "root powers" (I don't know its exact location out-of-my-head, but it's somewhere inside /data/system AFAIR). See: How to edit etc/hosts file – Izzy Jul 29 '14 at 11:15
6

You cannot simply edit the hosts file on Android, as it resides on a read-only file system: /system/etc/hosts, see:

Alternatives are:

  • use a DNS server like DNSMasq in your local network to take care for that "centrally"
  • use "root powers" to force-edit the system file as described above
  • install a "local DNS server" on your Android device to use that, e.g. DNS Server
  • hey Izzy, I tried to use DNS Server to handle this as I don't have root access to my phone; I added a simple rule that redirects the hostname to the ip in question.. but it doesn't work.. what am I doing wrong? – George Katsanos Jan 29 '15 at 10:33
  • DNS Server is supposed to serve other devices in your network (as the description says, you additionally need to use port-forwarding for that). That always requires those devices (including your Android device) are configured to use it. I didn't use any such DNS server on my Android devices, so I didn't check what exactly needs to be done here; but most likely you need to adjust the DNS entries in your network settings (long-press the WiFi network's entry, chose "Advanced", and adjust the first DNS entry – leave the second as-is for a "backup"). – Izzy Jan 29 '15 at 13:33
  • @GeorgeKatsanos You might be interested in this: stackoverflow.com/a/14211550/1768141 – Vinayak May 12 '15 at 17:35
2

As mentioned by lzzy, you can use DNSMasq server to achieve this. But Chrome uses own DNS resolving process and this method may not work.

To start the server use the following command:

sudo /usr/local/sbin/dnsmasq -d \
                             --no-hosts \
                             --no-resolv \
                             --conf-file=/dev/null \
                             --server=8.8.8.8 --server=8.8.4.4 \
                             --address=/example.com/192.168.0.101
  • sudo required to bind on port 53
  • -d start server on foreground
  • --no-hosts do not use local hosts file
  • --conf-file=/dev/null do not use any config file
  • --server=8.8.8.8 --server=8.8.4.4 addresses of upstream servers (Google DNS in this example)
  • --address=/example.com/192.168.0.101 hosts to override. You need to add your hosts at this line. You can also add multiple hosts there.

Then change DNS server on the device. This can be easily done using the DNS Changer app.

After these steps, the requests to these hosts should be sent to your addresses.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.