{"id":3571,"date":"2022-12-12T19:45:39","date_gmt":"2022-12-12T19:45:39","guid":{"rendered":"https:\/\/geekmungus.co.uk\/?p=3571"},"modified":"2022-12-12T19:45:39","modified_gmt":"2022-12-12T19:45:39","slug":"using-dnsmasq-on-raspberry-pi-for-quick-hosts-file-fudge-for-android-mobile","status":"publish","type":"post","link":"https:\/\/geekmungus.co.uk\/?p=3571","title":{"rendered":"Using dnsmasq on Raspberry Pi for Quick Hosts File Fudge for Android Mobile"},"content":{"rendered":"\n<p class=\"wp-block-paragraph\">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&#8217;t want to root my Android phone to be able edit the hosts file. There are apps such as &#8220;Hosts Go&#8221;, but in my case these didn&#8217;t work.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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?<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Install dnsmasq Ubuntu 22.04, 20.04 and 18.04<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Note, if you are using Ubuntu 18.04 you need to disable this first because systemd-resolve uses port 53.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl disable systemd-resolved\r\nsudo systemctl stop systemd-resolved<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Also,\u00a0remove the symlinked\u00a0<code>resolv.conf<\/code>\u00a0file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ ls -lh \/etc\/resolv.conf \r\nlrwxrwxrwx 1 root root 39 Jan 8 12:45 \/etc\/resolv.conf -> ..\/run\/systemd\/resolve\/stub-resolv.conf\r\n\r\n$ sudo unlink \/etc\/resolv.conf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Create new\u00a0<strong>resolv.conf<\/strong>\u00a0file.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>echo nameserver 8.8.8.8 | sudo tee \/etc\/resolv.conf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Now install dnsmasq as follows:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo apt update\r\nsudo apt install dnsmasq<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">The main configuration is done in the \/etc\/dnsmasq.conf file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo vim \/etc\/dnsmasq.conf<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Put in this configuration, this should be the bare minimum you need:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code># Listen on this specific port instead of the standard DNS port\r\n# (53). Setting this to zero completely disables DNS function,\r\n# leaving only DHCP and\/or TFTP.\r\nport=53\n\r\n# Never forward plain names (without a dot or domain part)\r\ndomain-needed\n\r\n# Never forward addresses in the non-routed address spaces.\r\nbogus-priv\n\r\n# By  default,  dnsmasq  will  send queries to any of the upstream\r\n# servers it knows about and tries to favour servers to are  known\r\n# to  be  up.  Uncommenting this forces dnsmasq to try each query\r\n# with  each  server  strictly  in  the  order  they   appear   in\r\n# \/etc\/resolv.conf\r\nstrict-order\n\r\n# Set this (and domain: see below) if you want to have a domain\r\n# automatically added to simple names in a hosts-file.\r\nexpand-hosts\n\r\n# Set the domain for dnsmasq. this is optional, but if it is set, it\r\n# does the following things.\r\n# 1) Allows DHCP hosts to have fully qualified domain names, as long\r\n#     as the domain part matches this setting.\r\n# 2) Sets the \"domain\" DHCP option thereby potentially setting the\r\n#    domain of all systems configured by DHCP\r\n# 3) Provides the domain part for \"expand-hosts\"\r\n\rdomain=domain.com\r\n\r\n# Set Listen address\r\nlisten-address=127.0.0.1 # Set to Server IP for network responses<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">Once done restart dnsmasq with:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart dnsmasq<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Add Your DNS Records to dnsmasq<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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&#8217;ve overridden the real IP with this custom IP, just like what a hosts file does.<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>$ sudo vim \/etc\/hosts\r\n10.1.2.3 test.domain.com\r\n82.123.89.34 test2.domain.com\r<\/code><\/pre>\n\n\n\n<p class=\"wp-block-paragraph\">You need to restart\u00a0dnsmasq service after adding the records to the hosts file:<\/p>\n\n\n\n<pre class=\"wp-block-code\"><code>sudo systemctl restart dnsmasq<\/code><\/pre>\n\n\n\n<h2 class=\"wp-block-heading\">Quick Test<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<p class=\"wp-block-paragraph\">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.<\/p>\n\n\n\n<h2 class=\"wp-block-heading\">Conclusion<\/h2>\n\n\n\n<p class=\"wp-block-paragraph\">Hopefully this is a useful approach for those odd test situations.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>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&#8217;t want to root my Android phone to be able edit the hosts file. There are apps such as &#8220;Hosts Go&#8221;, but in my case these didn&#8217;t work. On a normal &#8230; <a title=\"Using dnsmasq on Raspberry Pi for Quick Hosts File Fudge for Android Mobile\" class=\"read-more\" href=\"https:\/\/geekmungus.co.uk\/?p=3571\" aria-label=\"Read more about Using dnsmasq on Raspberry Pi for Quick Hosts File Fudge for Android Mobile\">Read more<\/a><\/p>\n","protected":false},"author":4,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,17,20,21,1],"tags":[],"class_list":["post-3571","post","type-post","status-publish","format-standard","hentry","category-linux","category-networking","category-random","category-raspberry-pi","category-uncategorized"],"_links":{"self":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3571","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/users\/4"}],"replies":[{"embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=3571"}],"version-history":[{"count":2,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3571\/revisions"}],"predecessor-version":[{"id":3573,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=\/wp\/v2\/posts\/3571\/revisions\/3573"}],"wp:attachment":[{"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=3571"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=3571"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/geekmungus.co.uk\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=3571"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}