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 8.8.8.8 | 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.
port=53

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

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

# 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
strict-order

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

# 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"

domain=domain.com

# Set Listen address
listen-address=127.0.0.1 # 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
10.1.2.3 test.domain.com
82.123.89.34 test2.domain.com

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. test.domain.com, rather than it using the real IP as looked up from DNS, it will now use the test IP, i.e. 10.1.2.3 in this particular example.

Conclusion

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 *