Keep DNS on a Dedicated Direct Route

3 Total views across this article’s versions; includes the historical baseline where available.

Compare all 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.

Open the Walkthrough Shell

From your ordinary administrator account, open a root login shell first:

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:

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, account-specific commands

1. Start with the Existing Routes

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

Run setup in one dedicated Bash shell. Replace the placeholder and example topology values:

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

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:

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:

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:

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

5. Let the Reader Use the 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:

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:

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:

tcpdump -ni any -vv 'port 53 or port 853'

Normal and Blocked Lookups

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:

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:

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:

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:

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 and availability monitoring 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

  1. Visit 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

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

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.

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:

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:

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:

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:

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:

iptables -w -F DH_NEWS_STUB
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

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.

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:

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:

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.

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:

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, policy rules, and iptables traversal.

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

Leave the Root Shell

After verification or withdrawal is complete, return to your ordinary shell:

exit

Review History

Preserved pre-review draft (text). Technical source review and local page validation were completed November 26, 2026. These simple examples retain explicit manual route/provider selection. The separate advanced Ubuntu walkthrough covers automatic controls, persistence, installation and rollback using the completed local reference source. Linux runtime validation remains deferred.