Using dnsmasq on Raspberry Pi for Quick Hosts File Fudge for Android Mobile

Linux Networking Random Raspberry Pi Uncategorized

During some upcoming changes I needed to test a mobile application to use a test IP address for a standard hostname. Problem is, I didn’t want to root my Android phone to be able edit the hosts file. There are apps such as “Hosts Go”, but in my case these didn’t work.

On a normal desktop machine you can just edit the hosts file with your new IP for the original hostname/FQDN, close and re-open your web browser and then you can browse that test site without any issues, on an Android mobile its not quite that easy.

What I found is I could setup a Raspberry Pi with dnsmasq for DNS. Tell my DHCP server to give out this DNS server out within the scope options and then run my tests, putting everything back to normal afterwards. So how can you do this?

These instructions are very minimal, but will get you a basic DNS server up, then you just ensure your device is using this for DNS, plumb in your test hostname/FQDN and away you go.

Install dnsmasq Ubuntu 22.04, 20.04 and 18.04

Note, if you are using Ubuntu 18.04 you need to disable this first because systemd-resolve uses port 53.

sudo systemctl disable systemd-resolved
sudo systemctl stop systemd-resolved

Also, remove the symlinked resolv.conf file:

$ ls -lh /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Jan 8 12:45 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

$ sudo unlink /etc/resolv.conf

Create new resolv.conf file.

echo nameserver | sudo tee /etc/resolv.conf

Now install dnsmasq as follows:

sudo apt update
sudo apt install dnsmasq

The main configuration is done in the /etc/dnsmasq.conf file:

sudo vim /etc/dnsmasq.conf

Put in this configuration, this should be the bare minimum you need:

# Listen on this specific port instead of the standard DNS port
# (53). Setting this to zero completely disables DNS function,
# leaving only DHCP and/or TFTP.

# Never forward plain names (without a dot or domain part)

# Never forward addresses in the non-routed address spaces.

# By  default,  dnsmasq  will  send queries to any of the upstream
# servers it knows about and tries to favour servers to are  known
# to  be  up.  Uncommenting this forces dnsmasq to try each query
# with  each  server  strictly  in  the  order  they   appear   in
# /etc/resolv.conf

# Set this (and domain: see below) if you want to have a domain
# automatically added to simple names in a hosts-file.

# Set the domain for dnsmasq. this is optional, but if it is set, it
# does the following things.
# 1) Allows DHCP hosts to have fully qualified domain names, as long
#     as the domain part matches this setting.
# 2) Sets the "domain" DHCP option thereby potentially setting the
#    domain of all systems configured by DHCP
# 3) Provides the domain part for "expand-hosts"

# Set Listen address
listen-address= # Set to Server IP for network responses

Once done restart dnsmasq with:

sudo systemctl restart dnsmasq

Add Your DNS Records to dnsmasq

If you add some DNS records with their IP address to the /etc/hosts file, then dnsmasq will serve these to clients instead of the real IP address when queried. I.e. you’ve overridden the real IP with this custom IP, just like what a hosts file does.

$ sudo vim /etc/hosts

You need to restart dnsmasq service after adding the records to the hosts file:

sudo systemctl restart dnsmasq

Quick Test

So now from your client phone, once you have made a change to your DHCP server to issue the DNS server IP in the scope options, ensure you have picked up the correct details.

Now if you attempt to browse to the URL, e.g., rather than it using the real IP as looked up from DNS, it will now use the test IP, i.e. in this particular example.


Hopefully this is a useful approach for those odd test situations.

Leave a Reply

Your email address will not be published. Required fields are marked *