Running a DNS server on a mobile device is useful for a few reasons: you can block ads locally, test custom hostnames during development, or create a tiny network for IoT devices when you have no router. The method described here works on Android 9 and newer, does not require root, and relies on Termux – a powerful terminal emulator that runs in a sandboxed environment.
All commands are entered inside the Termux app. If you see a Permission denied error, double‑check that you granted storage access (Settings > Apps > Termux > Permissions).
$ prompt.pkg update && pkg upgrade -ydnsmasq and a few helper utilities:
pkg install dnsmasq net-tools curl -y
Why these packages? dnsmasq provides the DNS server, net-tools gives ifconfig for checking your device IP, and curl is handy for testing DNS resolution.
etc directory (it’s created automatically):
cd $PREFIX/etcdnsmasq.conf:
nano dnsmasq.conf
If nano is unfamiliar, use the arrow keys to move, type your content, then press Ctrl+X, Y, and Enter to save.
192.168.1.0/24 with your actual Wi‑Fi subnet if different):
# Listen only on the Wi‑Fi interface (avoid binding to cellular data)
interface=wlan0
# Bind to the specific subnet of your local network
listen-address=192.168.1.0/24
# Do not read /etc/resolv.conf – we provide our own upstream servers
no-resolv
# Forward all queries to Google’s public DNS (you can change this)
server=8.8.8.8
server=8.8.4.4
# Optional: block known ad domains (example list)
addn-hosts=/data/data/com.termux/files/home/ads-hosts.txt
This configuration tells dnsmasq to answer queries only from devices on the same Wi‑Fi network, forward them to Google DNS, and optionally use a local hosts file for ad‑blocking.
nano $HOME/ads-hosts.txtIP hostname format, for example:
0.0.0.0 ads.example.com
0.0.0.0 tracker.example.orgdnsmasq.conf.You can download ready‑made ad‑blocking hosts lists from reputable sources and place them in the same file – just be sure the file size stays reasonable for a mobile device.
dnsmasq --no-daemon --log-queries
You should see lines like dnsmasq[1234]: query[A] example.com from 192.168.1.42. Keep this terminal open; press Ctrl+C to stop after confirming it works.
termux-wake-lock to keep the CPU awake:
termux-wake-lock
dnsmasq -k &
The -k flag tells dnsmasq to stay in the foreground, but the trailing & puts it in the background while the wake‑lock prevents Android from sleeping the process.
ps | grep dnsmasqifconfig wlan0
Look for the inet value – e.g., 192.168.1.10. This is the address you will configure as the DNS server.
192.168.1.10). Leave DNS 2 empty or set a fallback public DNS (e.g., 8.8.8.8) if you want a safety net.
Why static? Android’s Wi‑Fi UI does not expose a per‑network DNS override for DHCP. Switching to static lets you manually set the DNS while keeping the rest of the network parameters (gateway, subnet mask) unchanged.
192.168.1.10.Changing DNS to a non‑responsive address will break internet connectivity. Keep a second device handy to revert the setting if needed.
dig @192.168.1.10 example.com +short
You should see the IP address returned by Google’s DNS (or whatever upstream you configured). If you get no answer, check the dnsmasq logs for errors.
dig @192.168.1.10 ads.example.com +short
It should return 0.0.0.0 or no answer, indicating the host entry is being honoured.
port=5353 to dnsmasq.conf) and update the DNS settings on your devices accordingly.wlan0, others eth0 or rmnet0. Adjust the interface= line in dnsmasq.conf to match.termux-wake-lock as shown, or enable “Run in background” in Termux Settings > Battery. Some manufacturers (e.g., Huawei, Xiaomi) aggressively kill background processes; you may need to whitelist Termux in the device’s battery‑optimization settings.pkill dnsmasq or simply close the Termux session. Remember to revert the DNS settings on your Android device to “Automatic” (DHCP) to restore normal operation.~/start-dns.sh) that launches dnsmasq, then use Termux:Boot (a separate add‑on) to run the script automatically when the device boots.dnsmasq -k > $HOME/dnsmasq.log 2>&1 &. Review the log if you suspect queries are being dropped.address=/.example.com/127.0.0.1 rules for wildcard blocking, or you can integrate dnscrypt‑proxy for encrypted DNS queries. Those are beyond the scope of this beginner guide but worth exploring once you’re comfortable.With these steps you now have a fully functional local DNS server running on your Android phone. It can speed up development testing, provide lightweight ad‑blocking, or serve as a learning platform for networking concepts – all without rooting the device.









