Every time your phone looks up a website, it asks a DNS server for the IP address. The default DNS servers are usually provided by your carrier or Wi‑Fi router. Using a local resolver gives you three practical benefits:
All of this can be achieved on a stock Android device without rooting, thanks to the lightweight dnsmasq package that runs inside Termux.
127.0.0.1 where supported, or (2) run a simple VPN that forwards DNS queries to dnsmasq. The VPN method works on virtually every device.pkg update && pkg upgrade -y
pkg install nano wget curl proot -y
termux-wake-lock permission is granted (prevents the session from sleeping while dnsmasq runs). In Termux run:
termux-wake-lock
You can later release it with termux-wake-unlock.These commands are safe on any Android version that supports Termux (Android 5.0+).
dnsmasq package from the Termux repository:
pkg install dnsmasq -y
which dnsmasq
You should see something like /data/data/com.termux/files/usr/bin/dnsmasq.If the package cannot be found, run pkg install root-repo first – the dnsmasq binary lives in the root repository.
nano:
nano ~/dnsmasq.conf
# Listen only on the loopback interface – keeps the resolver local
interface=lo
listen-address=127.0.0.1
# Cache size – 1000 entries is a good balance for a phone
cache-size=1000
# Use Google’s DNS over TLS as upstream (privacy‑preserving)
server=8.8.8.8#853
server=8.8.4.4#853
# Optional: load a hosts file for ad‑blocking (see step 4)
#addn-hosts=/data/data/com.termux/files/home/ads-hosts.txt
This configuration tells dnsmasq to accept queries on 127.0.0.1, cache up to 1000 entries, and forward unknown names to Google’s DNS‑over‑TLS servers (port 853). Using TLS protects the upstream traffic from eavesdropping.
wget -O ~/ads-hosts.txt https://raw.githubusercontent.com/StevenBlack/hosts/master/hosts
addn-hosts line in ~/dnsmasq.conf (see step 3) and point it to the file you just downloaded.Because the list is stored locally, lookups for blocked domains are resolved instantly, saving data and improving page load times.
dnsmasq -C ~/dnsmasq.conf -k
The -k flag keeps dnsmasq in the foreground, which is useful for debugging.
dnsmasq: started, version 2.86 cachesize 1000, DNS servers 8.8.8.8#853, 8.8.4.4#853
If you see errors about “permission denied”, ensure Termux has storage permission (termux-setup-storage) and that the configuration file is readable.
Ctrl + C.At this point the resolver is active on 127.0.0.1, but Android is still using the network‑provided DNS servers.
Because you cannot change the system DNS without root, you have two practical options.
127.0.0.1.
Some Android skins (e.g., Pixel, OnePlus) accept 127.0.0.1 as a custom DNS. If the network refuses the address, use Option 2.
We will use the open‑source Termux:API helper termux-vpn (available in the termux-api package). The VPN runs in user space and does not require root.
pkg install termux-api -y
~/dns-vpn.sh:
cat > ~/dns-vpn.sh <<'EOF'
#!/data/data/com.termux/files/usr/bin/bash
# Start a dummy VPN that only redirects DNS to 127.0.0.1
# The VPN interface will be tun0
termux-vpn -a tun0 -p 0 -d 127.0.0.1:53
EOF
chmod +x ~/dns-vpn.sh
tmux if you like):
./dns-vpn.sh
You should see a message like “VPN started on tun0”.
127.0.0.1:53, where dnsmasq answers them.If the VPN fails to start, make sure you have granted Termux the “Display over other apps” and “VPN” permissions. Android will prompt you the first time the script runs.
dig @127.0.0.1 example.com +short
You should receive the IP address of example.com. If you see “connection timed out”, double‑check that dnsmasq is still running and that the VPN (Option 2) or Wi‑Fi DNS setting (Option 1) points to 127.0.0.1.
ads.example.com). The page should fail to load, confirming the blocklist is active.
dig command twice and note the response time. The second query should be virtually instantaneous.listen-address=127.0.0.1#5353) and adjust the VPN script accordingly.server=…#853 lines with plain DNS (port 53) or use Cloudflare’s 1.1.1.1.
addn-hosts line in dnsmasq.conf is uncommented and points to the correct file. Restart dnsmasq after any change.Manually keeping the terminal open works, but you may want a more hands‑free approach.
tmux (pkg install tmux -y) and start a session:
tmux new -s dns
Inside the tmux window, launch dnsmasq as in Step 5.
Ctrl + B, D. The process keeps running even if you close the Termux UI.tmux attach -t dns.For a fully automated start‑up, you can add the dnsmasq command to Termux’s ~/.bashrc or create a small cron job using the cronie package, but be aware that Android may still kill background processes to save battery.
If you switch to a different Wi‑Fi network that does not allow custom DNS, or if you need to conserve battery, stop the services:
# Stop dnsmasq (if running in foreground, press Ctrl+C)
# Stop the VPN
pkill -f termux-vpn
# Release the wake lock
termux-wake-unlock
By installing Termux, adding dnsmasq, and routing DNS traffic through a local VPN or Wi‑Fi custom DNS, you gain a privacy‑first, cache‑enabled DNS resolver without ever rooting your device. The setup is reversible, works on Android 5.0+, and can be extended with custom blocklists or alternative upstream providers.









