How to Set Up a Local DNS Server on Android with dnsmasq (No Root)

10 min read Learn how to turn your Android device into a local DNS server using dnsmasq in Termux. Step‑by‑step guide, configuration tips, and troubleshooting without needing root access. October 11, 2026 19:00 How to Set Up a Local DNS Server on Android with dnsmasq (No Root)

Before you start

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.

  • Battery: Keep the phone plugged in or ensure at least 50 % charge. dnsmasq will stay active as long as Termux’s session runs.
  • Wi‑Fi: The DNS server will only answer requests from the same Wi‑Fi network. Connect to the network you intend to use before starting.
  • Permissions: Termux needs storage permission to read/write its configuration files. Grant it when prompted.
  • Port conflict: dnsmasq uses UDP/TCP port 53. If another app (e.g., a VPN or a built‑in DNS‑over‑TLS service) already binds to that port, the server will fail to start.

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).

1. Install Termux and dnsmasq

  1. Open the Google Play Store (or F-Droid) and install Termux. The official builds are updated regularly and include the required package manager.
  2. Launch Termux. The first launch will initialize the environment – wait for the $ prompt.
  3. Update the package repository and upgrade existing packages:
    pkg update && pkg upgrade -y
  4. Install dnsmasq 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.

2. Create a basic dnsmasq configuration

  1. Navigate to Termux’s private etc directory (it’s created automatically):
    cd $PREFIX/etc
  2. Create a file named dnsmasq.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.

  3. Paste the following minimal configuration (replace 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.

  4. Save and exit the editor.

3. (Optional) Add a custom hosts file for ad‑blocking or local names

  1. Create a simple hosts file:
    nano $HOME/ads-hosts.txt
  2. Add entries in the classic IP hostname format, for example:
    0.0.0.0    ads.example.com
    0.0.0.0    tracker.example.org
  3. Save the file. dnsmasq will read it automatically because we referenced it in dnsmasq.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.

4. Start the DNS server

  1. Run dnsmasq in the foreground first to verify the configuration:
    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.

  2. For regular use, start dnsmasq as a background service using Termux’s 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.

  3. Verify the process is running:
    ps | grep dnsmasq

5. Point Android (and optionally other devices) to the local DNS

  1. Find your phone’s IP address on the Wi‑Fi network:
    ifconfig wlan0

    Look for the inet value – e.g., 192.168.1.10. This is the address you will configure as the DNS server.

  2. On the same Android device, open Settings > Network & Internet > Wi‑Fi, long‑press your connected network, and select Modify network. Expand Advanced options and change IP settings from DHCP to Static.
  3. In the DNS 1 field, enter the IP you obtained in step 1 (e.g., 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.

  4. Save the changes. Your phone will briefly disconnect and reconnect to Wi‑Fi.
  5. If you have other devices (laptops, IoT gadgets) on the same network, repeat the DNS configuration on each, pointing them to 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.

6. Test the DNS server

  1. From the same Android device, open a terminal in Termux and run:
    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.

  2. Test ad‑blocking (if you added a hosts file) by querying a known ad domain:
    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.

  3. On a regular web browser, visit a site you know resolves quickly. If the page loads, the DNS chain is working.

7. Troubleshooting

  • dnsmasq fails to start: “address already in use” – Another app is already listening on port 53. Common culprits are VPN clients, Private DNS, or certain security apps. Disable those temporarily or change dnsmasq’s listening port (add port=5353 to dnsmasq.conf) and update the DNS settings on your devices accordingly.
  • No DNS responses – Verify the Wi‑Fi interface name. Some devices use wlan0, others eth0 or rmnet0. Adjust the interface= line in dnsmasq.conf to match.
  • Device cannot reach the internet after setting static DNS – Double‑check that you kept the original gateway and subnet mask values when switching to static IP. Only the DNS fields should change.
  • Termux session stops when the screen locks – Use 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.

8. Safety, cleanup, and optional enhancements

  • Stopping the server: Run 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.
  • Persisting across reboots: Android does not keep Termux sessions alive after a reboot. You can create a small script (~/start-dns.sh) that launches dnsmasq, then use Termux:Boot (a separate add‑on) to run the script automatically when the device boots.
  • Logging: dnsmasq writes logs to stdout. To keep a persistent log, start it with dnsmasq -k > $HOME/dnsmasq.log 2>&1 &. Review the log if you suspect queries are being dropped.
  • Advanced filtering: dnsmasq supports 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.

User Comments (0)

Add Comment
We'll never share your email with anyone else.