20

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

3
  • 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, 2014 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.) Jul 29, 2014 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, 2014 at 11:15

4 Answers 4

10

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
3
  • 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? Jan 29, 2015 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, 2015 at 13:33
  • @GeorgeKatsanos You might be interested in this: stackoverflow.com/a/14211550/1768141
    – Vinayak
    May 12, 2015 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.

0

The simplest way to define a host to IP mapping is hosts file as mostly DNS resolvers honor that file. But as mentioned in other answers, it's not possible to edit /etc/hosts without rooting Android device. Accessing web server with IP address also won't work for you due to virtual hosting. Here are a few options you can go with on unrooted device:

  • Use a VPN app which intercepts DNS traffic and looks up a custom hosts file before making queries to configured upstream DNS server.
  • Run a DNS server; they can be configured to return a predefined IP address for a specific name. For instance if using dnsmasq, add address=/mysite.local/10.1.2.3 to “dnsmasq.conf”. Or on dnscrypt-proxy add mysite.local 10.1.2.3 to “cloaking-rules.txt”.
  • Configure phone to use a proxy or VPN and then add the entry 10.1.2.3 mysite.local to hosts file on proxy server or run a local DNS server on VPN server.
  • If bootloader is unlockable, edit hosts file from custom recovery.

For more details see: How to always resolve a domain name to a fixed IP without rooting?


RELATED:

0

You can't change the file on a read-only filesystem, but you can use bind option, to substitute file with located on rw fs. This should work only till the next reboot.

Example:

mount /sdcard/hosts /etc/hosts -o bind
bind file /sdcard/hosts  to /etc/hosts
1
  • 1
    While the answer is correct and follows the Magisk's systemless approach, the question is specifically about unrooted devices. You can't run mount without root privileges. Your answer is more suitable here: android.stackexchange.com/q/60150/218526 Sep 12, 2020 at 15:42

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .