--- title: "Keep DNS on a Dedicated Direct Route" description: "Keep a designated DNS provider on the direct connection independently of application VPN routing." url: /tutorials/networking/vpn-dns/dedicated-direct-dns/ article_id: vpn-dns-3 content_kind: tutorials categories: [Networking] tags: [tutorial, networking, DNS, VPN, iproute2, iptables] noindex: true sitemap: disable: true --- [Compare all DNS policies](/reference/networking/vpn-dns-policies/). Using a VPN does not necessarily mean that you want to use its DNS service. You may already have a DNS provider you prefer, and you may want to keep using that provider regardless of whether an application connects through the VPN or directly to the Internet. For example, your newsgroup reader uses the VPN, while your web browser uses the direct connection. In this example, both applications use your designated DNS provider over the direct connection. Connecting or disconnecting the VPN does not change that choice. {{% callout style="note" %}} This is an integrated review draft. The commands and verification procedures have not been run on a Linux host. VPN operation and availability monitoring remain separate from this routing walkthrough. {{% /callout %}} ## Open the Walkthrough Shell {#walkthrough-shell} From your ordinary administrator account, open a root login shell first: ```bash sudo -i ``` Use Bash in that root shell for the remaining setup, checks, and withdrawal. Do not nest another root shell when following a shared section. Confirm root access and retain the original account for comparison tests: ```bash set -euo pipefail test "$(id -u)" -eq 0 ORDINARY_USER="${SUDO_USER:-}" test -n "$ORDINARY_USER" test "$ORDINARY_USER" != root test "$ORDINARY_USER" != newsvpn test "$(id -u "$ORDINARY_USER")" -ne 0 id "$ORDINARY_USER" ``` Stop if the account is not your intended ordinary user. Administrative commands now run without repeated `sudo`. `runuser -u ... --` deliberately runs a test under the named account; testing as root would not exercise the reader's UID guard. When following a linked foundation check, keep its ordinary-user tests under `ORDINARY_USER` too; a root request is not the ordinary-account comparison. The examples require `runuser` as well as the tools listed below. [Root login shells](https://man7.org/linux/man-pages/man8/sudo.8.html), [account-specific commands](https://man7.org/linux/man-pages/man1/runuser.1.html) ## 1. Start with the Existing Routes {#1-state-the-platform-and-starting-point} Use one Ubuntu host with Bash, *iproute2*, *iptables*/*ip6tables*, `dig`, `getent`, `tcpdump`, and an existing *systemd-resolved* stub at `127.0.0.53`. Ordinary applications already use that resolver. Ubuntu 24.04 is the proposed lab baseline, not a tested-platform claim. Begin with the [packet-marks foundation](/downloads/review/packet-marks-technical-prerequisite.txt): account `newsvpn`, mark `0x100`, table `101`, application VPN `tun0`, its source translation, return path, and independent IPv4/IPv6 guards. Keep the priority-zero local routing rule. The shared resolver uses the main table under its own account; it does not inherit the reader's mark. Use a separate lab configuration without other examples' DNS chains, DNS host routes, private launchers, or replacement guards. This is not a migration script between examples. Record the original resolver settings, routes, and firewall. Reconcile other DNS links, global servers, and competing domains before starting. Do not overwrite a managed `/etc/resolv.conf`. {{% callout style="important" %}} Keep independent recovery access and stop affected workloads during setup. Establish DNS restrictions before changing resolver selection or the reader's guard. Stop on errors and retain protection. Test real connections as well as lookups before everyday use. {{% /callout %}} Run setup in one dedicated Bash shell. Replace the placeholder and example topology values: ```bash set -euo pipefail DIRECT_DNS='REPLACE_WITH_DESIGNATED_DNS_IPV4' DIRECT_IF='eth0' DIRECT_GW='192.168.50.1' DIRECT_ADDR='192.168.50.10' VPN_IF='tun0' DNS_STUB='127.0.0.53' NEWS_UID=$(id -u newsvpn) test "$NEWS_UID" -ne 0 test "$DIRECT_DNS" != 'REPLACE_WITH_DESIGNATED_DNS_IPV4' ip -4 addr show dev "$DIRECT_IF" ip -4 rule show ip -4 route show table main ip -4 route show table 101 resolvectl status readlink -f /etc/resolv.conf cat /etc/resolv.conf ss -lnutp 'sport = :53' iptables -w -S OUTPUT ip6tables -w -S OUTPUT iptables -w -t mangle -S OUTPUT iptables -w -t nat -S POSTROUTING iptables-save ip6tables-save ``` Confirm the direct address and gateway belong to this host's network. Choose a valid, available, nonlocal IPv4 resolver beyond that gateway, distinct from VPN endpoints. Resolver legitimacy and availability are your responsibility. New chain names, comments, and DNS host routes must be unused; stop on conflicts instead of flushing configuration. `set -e` stops simple setup blocks on errors; it is not rollback. This example uses IPv4 UDP/TCP DNS. Upstream IPv6 DNS and port 853 are blocked; the reader's IPv6 guard stays installed. Applications must use the chosen policy; arbitrary DoH and delegated proxy DNS are not identified by port rules. Existing encrypted-DNS arrangements, multicast discovery, forwarding, and containers require separate treatment. ### Record the Original DNS Settings {#dns-before-state} Save the full `resolvectl status` output shown above outside this shell. Record all DNS servers and search/routing domains for `DIRECT_IF` and `VPN_IF`, including empty lists. Use the complete **DNS Servers** list, not just **Current DNS Server**. Preserve server order, any server qualifiers, and the `~` prefix on routing domains. Prepare the corresponding restore commands before changing anything. Leave the resolver's `default-route` setting untouched. The selected link's `~.` domain already selects ordinary unicast DNS within this example's stated scope; other DNS links, global servers, and competing domains must have been reconciled. This avoids replacing an automatically determined setting with an explicit value during withdrawal. It does not change the default IP route. Keep the same interfaces present and the lab configuration stable. If a manager changes these settings or an interface is recreated, stop and inspect its configuration owner instead of guessing the original values. ## 2. Restrict DNS to the Direct Provider Populate both chains before attaching them. Only the local IPv4 stub and the selected provider through the direct interface may pass this DNS check: ```bash iptables -w -N DH_DNS_DIRECT4 iptables -w -A DH_DNS_DIRECT4 -o lo -d "$DNS_STUB" -p udp --dport 53 -j RETURN iptables -w -A DH_DNS_DIRECT4 -o lo -d "$DNS_STUB" -p tcp --dport 53 -j RETURN iptables -w -A DH_DNS_DIRECT4 -o "$DIRECT_IF" -d "$DIRECT_DNS" -p udp --dport 53 -j RETURN iptables -w -A DH_DNS_DIRECT4 -o "$DIRECT_IF" -d "$DIRECT_DNS" -p tcp --dport 53 -j RETURN iptables -w -A DH_DNS_DIRECT4 -p udp -m multiport --dports 53,853 -j REJECT iptables -w -A DH_DNS_DIRECT4 -p tcp -m multiport --dports 53,853 -j REJECT iptables -w -A DH_DNS_DIRECT4 -j RETURN ip6tables -w -N DH_DNS_DIRECT6 ip6tables -w -A DH_DNS_DIRECT6 -p udp -m multiport --dports 53,853 -j REJECT ip6tables -w -A DH_DNS_DIRECT6 -p tcp -m multiport --dports 53,853 -j REJECT ip6tables -w -A DH_DNS_DIRECT6 -j RETURN iptables -w -I OUTPUT 1 -m comment --comment dns-direct-all4 -j DH_DNS_DIRECT4 ip6tables -w -I OUTPUT 1 -m comment --comment dns-direct-all6 -j DH_DNS_DIRECT6 ``` These are *filter* rules. `RETURN` continues through the rest of the firewall; it does not accept a packet or bypass the reader guard. Keep the DNS jumps and account guards before broad accepts, including established-connection accepts. Existing local-stub/reply permissions remain necessary. The IPv6 chain blocks upstream DNS, not all IPv6 application traffic. ## 3. Route the DNS Address Directly There are two decisions here: who answers your DNS requests, and how those requests reach that provider. This example keeps both decisions independent of the VPN. Unlike Example 2B, where you explicitly select between VPN and direct DNS modes, this configuration always uses the direct provider and direct path. There is nothing to switch when the VPN becomes unavailable. Install the terminal route first, then the preferred direct host route: ```bash ip -4 route add unreachable "$DIRECT_DNS/32" metric 32767 ip -4 route add "$DIRECT_DNS/32" via "$DIRECT_GW" dev "$DIRECT_IF" src "$DIRECT_ADDR" metric 100 ip -4 route show table main exact "$DIRECT_DNS/32" ip -4 route get "$DIRECT_DNS" ``` Both routes match the same address. If the usable route disappears, `unreachable` prevents a broader main-table route from taking over. The firewall independently requires the direct interface. This applies to all traffic to that address, not only DNS. `src` selects a preferred source; verify actual requests and replies rather than assuming it rewrites bound addresses. Keep the reader's separate table `101` and existing source translation unchanged. An earlier policy rule must not divert the shared resolver from the main table. No new DNS mark, table, or NAT rule is needed for this baseline. ## 4. Select the DNS Provider With both interfaces present, clear VPN DNS selection and use the direct link: ```bash resolvectl dns "$VPN_IF" '' resolvectl domain "$VPN_IF" '' resolvectl dns "$DIRECT_IF" "$DIRECT_DNS" resolvectl domain "$DIRECT_IF" '~.' resolvectl status ``` `~.` is the resolver's catch-all routing domain, not an IP route. More-specific domains and other links can compete, so inspect the effective settings. These temporary commands do not establish persistence: managers can overwrite them, and interface removal loses per-link settings. An unexpected provider must still be blocked by the firewall, even if that means DNS fails. [Resolver selection](https://www.freedesktop.org/software/systemd/man/latest/resolvectl.html) ## 5. Let the Reader Use the Stub {#5-let-the-reader-reach-only-the-existing-stub} Your DNS requests and the connection that follows them are separate traffic. Resolving the newsgroup server's name through a direct DNS connection does not mean the reader should connect to that server directly. The original account guard blocks loopback too. Build a replacement allowing only UDP/TCP port 53 to the existing stub, while retaining the drop for other traffic outside the application VPN: ```bash iptables -w -C OUTPUT -m owner --uid-owner "$NEWS_UID" ! -o "$VPN_IF" -m comment --comment newsvpn-guard4 -j DROP ip6tables -w -C OUTPUT -m owner --uid-owner "$NEWS_UID" -m comment --comment newsvpn-guard6 -j DROP iptables -w -N DH_NEWS_STUB iptables -w -A DH_NEWS_STUB -o lo -d "$DNS_STUB" -p udp --dport 53 -j RETURN iptables -w -A DH_NEWS_STUB -o lo -d "$DNS_STUB" -p tcp --dport 53 -j RETURN iptables -w -A DH_NEWS_STUB ! -o "$VPN_IF" -j DROP iptables -w -A DH_NEWS_STUB -j RETURN iptables -w -I OUTPUT 1 -m owner --uid-owner "$NEWS_UID" -m comment --comment newsvpn-stub-guard4 -j DH_NEWS_STUB iptables -w -S DH_NEWS_STUB iptables -w -S OUTPUT iptables -w -D OUTPUT -m owner --uid-owner "$NEWS_UID" ! -o "$VPN_IF" -m comment --comment newsvpn-guard4 -j DROP ``` Inspect the replacement and its position before deleting the original guard. The new guard and host-wide DNS check must both precede broad accepts. The IPv6 guard stays installed. Run the reader as `newsvpn` with the host resolver, not Example 1's private environment. Its local request reaches the stub; the resolver's separate upstream packet follows the direct DNS route. No general loopback or direct-application exception is added. ## 6. Verify the Two Paths If the application's VPN route becomes unavailable, DNS can keep working through the direct connection. Successful DNS does not make the direct application route acceptable. If the designated DNS path becomes unavailable, fresh requests must fail without quietly switching to the VPN's DNS or another provider. Allow expected diagnostic failures in the same shell. Replace the test names and addresses with permitted endpoints; the alternate IPv4 resolver must differ from `DIRECT_DNS`: ```bash set +e TEST_NAME='REPLACE_WITH_FRESH_PERMITTED_FQDN.' OTHER_DNS='REPLACE_WITH_OTHER_PERMITTED_DNS_IPV4' OTHER_DNS6='REPLACE_WITH_PERMITTED_DNS_IPV6' ip -4 route get "$DNS_STUB" mark 0x100 ip -4 route get "$DIRECT_DNS" ``` Require the marked stub route to remain local and the upstream route to use the direct interface. Use fresh existing names absent from hosts files and local caches. In another terminal, first open a root shell with `sudo -i`, then capture each test separately: ```bash tcpdump -ni any -vv 'port 53 or port 853' ``` ### Normal and Blocked Lookups ```bash runuser -u "$ORDINARY_USER" -- getent ahostsv4 "$TEST_NAME" runuser -u "$ORDINARY_USER" -- dig -r -4 @"$DNS_STUB" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A runuser -u "$ORDINARY_USER" -- dig -r -4 @"$DNS_STUB" +tcp +time=2 +tries=1 "$TEST_NAME" A runuser -u newsvpn -- getent ahostsv4 "$TEST_NAME" runuser -u newsvpn -- dig -r -4 @"$DNS_STUB" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A runuser -u newsvpn -- dig -r -4 @"$DNS_STUB" +tcp +time=2 +tries=1 "$TEST_NAME" A iptables -w -nvL DH_DNS_DIRECT4 iptables -w -nvL DH_NEWS_STUB ip6tables -w -nvL DH_DNS_DIRECT6 ``` From the root shell, test both upstream transports as the ordinary account: ```bash runuser -u "$ORDINARY_USER" -- dig -r -4 @"$DIRECT_DNS" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A runuser -u "$ORDINARY_USER" -- dig -r -4 @"$DIRECT_DNS" +tcp +time=2 +tries=1 "$TEST_NAME" A ``` Require both application groups' upstream queries and replies through `DIRECT_IF` to `DIRECT_DNS`, with the expected source. A stub response or exit code alone does not prove the path; TCP to the stub does not prove TCP upstream. Read the answer and correlate captures. Test alternatives from the ordinary account and a direct-to-provider query from the reader: ```bash runuser -u "$ORDINARY_USER" -- dig -r -4 @"$OTHER_DNS" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A runuser -u "$ORDINARY_USER" -- dig -r -4 @"$OTHER_DNS" +tcp +time=2 +tries=1 "$TEST_NAME" A runuser -u "$ORDINARY_USER" -- dig -r -6 @"$OTHER_DNS6" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A runuser -u "$ORDINARY_USER" -- dig -r -6 @"$OTHER_DNS6" +tcp +time=2 +tries=1 "$TEST_NAME" A runuser -u newsvpn -- dig -r -4 @"$DIRECT_DNS" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A runuser -u newsvpn -- dig -r -4 @"$DIRECT_DNS" +tcp +time=2 +tries=1 "$TEST_NAME" A ``` All must fail without delivered queries. Repeat ordinary-account UDP/TCP checks to the designated provider with `-p 853`. Compare DNS-chain and account-guard counters before and after attempts; routing can reject traffic before a counter increments. No usable IPv6 route makes an IPv6 failure inconclusive without enforcement evidence. Confirm that unrelated loopback services remain blocked for the reader. ### Remove and Restore the Direct Route Remove only the usable DNS route, leaving its terminal route and all guards installed: ```bash ip -4 route del "$DIRECT_DNS/32" via "$DIRECT_GW" dev "$DIRECT_IF" src "$DIRECT_ADDR" metric 100 ip -4 route show table main exact "$DIRECT_DNS/32" ip -4 route get "$DIRECT_DNS" ``` Require the usable entry to be absent and the lookup to fail. Repeat fresh lookups from both accounts; no query may move to the VPN or another provider. If a manager reinstalls the route, the intended fault was not established. Verify the direct interface, address, and gateway are unchanged, then restore the exact route: ```bash ip -4 route add "$DIRECT_DNS/32" via "$DIRECT_GW" dev "$DIRECT_IF" src "$DIRECT_ADDR" metric 100 ``` Stop on restoration errors and repeat normal and blocked checks. Verify fresh real reader connections through its VPN and ordinary application connections directly. Use the packet-marks foundation's known-address/table-101 failure checks: protected application traffic must stop while direct DNS remains usable. A DNS-route test alone is not a VPN-outage test. [VPN operation](/review/vpn-operation-integration/) and [availability monitoring](/review/dns-availability-review/) remain separate. No service, monitor, or automatic switching is added. Record each check as passed, failed, or inconclusive; every Linux runtime case remains untested here. ### Compare the Public Exit Addresses {#public-exit-check} 1. Visit [WhatIsMyIP.com](https://www.whatismyip.com/) in your ordinary browser and note the displayed public IPv4 address. 2. Visit the same website in a browser using the VPN-selected account's route and compare the displayed IPv4 address. It should show the application VPN's public exit address. Use the separate DNS, route-loss, and application-isolation checks for diagnosis. ### Check the Local DNS Exception {#local-isolation-check} The shared-stub exception must not grant access to other loopback services. Choose an existing, permitted test listener on a different TCP port. First confirm the ordinary account can connect to that exact loopback address and port, then require the same attempt as `newsvpn` to be blocked. Compare the `DH_NEWS_STUB` drop counter and capture evidence before and after. A refusal from an unused port proves nothing about the guard. Keep the listener and its test method unchanged between attempts; do not create a proxy or broaden an allowance for this check. ## 7. Withdraw This Example Stop affected workloads. ### Restore the Account Guard {#restore-account-guard} Keep the reader and its helpers stopped. Continue in the existing root shell; if it was lost, run `sudo -i` once, use Bash, and recover the recorded `NEWS_UID` and application `VPN_IF`. Use the same firewall backend as setup, with no other firewall manager changing its rules. Run each command separately and check its result. After an interruption or unexpected error, inspect again before continuing. ```bash set -e iptables -w -S ip6tables -w -S OUTPUT ``` Inspect the full rules and their order, not just comments or chain names. The original IPv4 guard (`newsvpn-guard4`) must match the recorded account and block output outside its application VPN. The IPv6 guard (`newsvpn-guard6`) must still block that account. Both must precede broad acceptance rules. If neither IPv4 guard is effective, or IPv6 protection is missing, stop normal withdrawal and recover the original protection using verified recorded values. Leave unknown or shared rules untouched. **Keep a valid original guard already present.** Only if it is absent and the replacement is verified, restore it: ```bash iptables -w -I OUTPUT 1 -m owner --uid-owner "$NEWS_UID" ! -o "$VPN_IF" -m comment --comment newsvpn-guard4 -j DROP ``` Verify the original guards before removing anything: ```bash iptables -w -C OUTPUT -m owner --uid-owner "$NEWS_UID" ! -o "$VPN_IF" -m comment --comment newsvpn-guard4 -j DROP ip6tables -w -C OUTPUT -m owner --uid-owner "$NEWS_UID" -m comment --comment newsvpn-guard6 -j DROP iptables -w -S OUTPUT ip6tables -w -S OUTPUT ``` The checks confirm existence; inspect the listings for correct position. If the exact replacement jump is present, remove it: ```bash iptables -w -D OUTPUT -m owner --uid-owner "$NEWS_UID" -m comment --comment newsvpn-stub-guard4 -j DH_NEWS_STUB ``` Inspect the whole filter table again: ```bash iptables -w -S ``` Require the original guard to remain effective and **no jump or goto references to `DH_NEWS_STUB`**. If duplicate owned jumps remain, remove them one at a time and inspect again. Stop for unexpected references from other chains. If the replacement jump was already absent, skip its deletion. Only when this example's chain exists and is unreferenced, empty it, check success, then delete it: ```bash iptables -w -F DH_NEWS_STUB ``` ```bash iptables -w -X DH_NEWS_STUB ``` Skip these two commands if the chain is already absent. Never flush a referenced chain, `OUTPUT`, or an entire table. Keep the IPv6 guard and DNS restrictions installed. Restoring the original guard blocks the local DNS exception; keep the reader stopped while completing the DNS restoration below. ### Restore the Recorded DNS Lists {#restore-dns-settings} Keep the DNS restrictions and application guards installed. For each of `DIRECT_IF` and `VPN_IF`, restore exactly the server and domain lists saved before setup. Run one command at a time and check its result before continuing. These are command forms: replace the uppercase names with the recorded interface and entries. Include every original entry, whether one or several; quote routing domains to preserve their `~` prefix. ```text resolvectl dns INTERFACE ORIGINAL_SERVER_1 ORIGINAL_SERVER_2 resolvectl domain INTERFACE 'ORIGINAL_DOMAIN_1' '~ORIGINAL_ROUTING_DOMAIN' ``` For an originally empty list, use one empty argument instead: ```text resolvectl dns INTERFACE '' resolvectl domain INTERFACE '' ``` For example, **only if the saved configuration** had `eth0` using `1.1.1.1` and `1.0.0.1` with no domains, and `tun0` with no DNS servers or domains, the restoration would be: ```bash resolvectl dns eth0 1.1.1.1 1.0.0.1 resolvectl domain eth0 '' resolvectl dns tun0 '' resolvectl domain tun0 '' ``` These are illustrative previous values, not a new provider selection. Substitute your recorded values; do not assume that the VPN lists were empty. ```bash resolvectl status ``` Compare the affected links with the saved lists before continuing. A direct-DNS lookup may still be blocked by the installed restrictions; check the configuration now and perform the ordinary-account lookup/path tests after deliberately removing those restrictions. Do not use `resolvectl revert`: it resets defaults rather than restoring your saved lists. **If you ran an earlier draft:** its commands also changed `default-route`. Restoring only the lists is not a complete undo of that older setup. Restore a recorded explicit value through `resolvectl default-route`; an automatically determined baseline needs its configuration owner's restoration. Do not guess a value or use a blanket reset. The revised setup leaves this setting alone. After restoring the prior DNS policy, remove only this example's routes and chains: ```bash ip -4 route del "$DIRECT_DNS/32" via "$DIRECT_GW" dev "$DIRECT_IF" src "$DIRECT_ADDR" metric 100 ip -4 route del unreachable "$DIRECT_DNS/32" metric 32767 iptables -w -D OUTPUT -m comment --comment dns-direct-all4 -j DH_DNS_DIRECT4 ip6tables -w -D OUTPUT -m comment --comment dns-direct-all6 -j DH_DNS_DIRECT6 iptables -w -F DH_DNS_DIRECT4 iptables -w -X DH_DNS_DIRECT4 ip6tables -w -F DH_DNS_DIRECT6 ip6tables -w -X DH_DNS_DIRECT6 ``` Inspect partial setup or earlier route loss first; omit a deletion only when that exact entry is already absent. Never flush `OUTPUT` or unrelated routes. After removing the intended DNS restrictions, repeat the existing ordinary-account lookup and packet-path checks with a fresh permitted name. Require the original provider/path; a cached answer is insufficient. Keep the reader stopped until its own intended DNS environment and application route are verified: restoring the original account guard removes its local-stub exception. References: [route types](https://man7.org/linux/man-pages/man8/ip-route.8.html), [policy rules](https://man7.org/linux/man-pages/man8/ip-rule.8.html), and [iptables traversal](https://man7.org/linux/man-pages/man8/iptables.8.html). See the [separate technical change record](/review/vpn-dns-always-direct-changes/). ## Understand the Privacy Choice Direct DNS is intentional here. The initial command example uses ordinary DNS, so query contents can be visible to observers on the direct path. Choosing a particular provider does not encrypt those requests or prevent that provider from seeing them. [DNS Privacy Considerations](https://www.rfc-editor.org/rfc/rfc9076.html) explains the distinction. Encrypted DNS to the designated provider can be a separate variation. It changes how the DNS connection is protected, while the provider and direct-route choices remain the same. Do not silently replace an existing encrypted-DNS setup with the plaintext example. The purpose is to give DNS its own consistent policy, while preserving the different routes you chose for your applications. See the [account-guard withdrawal technical change record](/review/vpn-dns-account-guard-changes/). ## Leave the Root Shell After verification or withdrawal is complete, return to your ordinary shell: ```bash exit ``` See the [separate root-session and isolation change record](/review/vpn-dns-root-session-isolation-changes/). These commands remain untested on Linux. ## Review History [Preserved combined draft](/review/vpn-dns-always-direct-integrated/), [separate prose and technical change records](/review/vpn-dns-integration-changes/).