Separate VPNs, Using VPN 1's DNS

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

Compare all DNS policies.

The earlier examples use one VPN, with different choices about which traffic should use it. But you may want to separate your DNS requests from your application connections while still keeping both off the direct Internet path.

For example, you want your newsgroup reader to connect through one VPN, but you want its DNS requests to use another. Your ordinary browser can continue using the direct connection. Its DNS requests would still use the VPN reserved for DNS.

We will call the DNS connection VPN 1 and the application connection VPN 2. These are two separate paths from the same host, not one VPN running through another.

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 Two Existing VPN Paths

Use one Ubuntu host with Bash, iproute2, iptables/ip6tables, dig, getent, tcpdump, and an existing systemd-resolved stub at 127.0.0.53. Ubuntu 24.04 is the proposed lab baseline, not a tested-platform claim.

Begin with the packet-marks foundation: newsvpn, mark 0x100, table 101, source translation, return path, and independent IPv4/IPv6 account guards. Keep the priority-zero local rule. Use a separate lab configuration without another scenario’s DNS rules active.

  • VPN 1, tun1: DNS, using local address 10.9.0.2.
  • VPN 2, tun0: reader connections, retaining the foundation’s local address 10.8.0.2.
  • Direct, eth0: ordinary applications and both VPNs’ outer connections.

These are independent tunnels, not nested VPNs. Their interfaces, addressing, and return paths must already work. VPN setup and monitoring remain separate responsibilities. The shared resolver uses the main table under its own account; it does not inherit the reader’s mark. Resolve conflicting policy rules before continuing.

2. Set the Addresses

The first variation uses the DNS service provided by VPN 1. This keeps the DNS route and resolver with the same provider. You are trusting that provider with both the VPN connection and the DNS requests.

Choose this variation if you want to use the DNS service included with that VPN.

For 4A, obtain the supported resolver IPv4 address from VPN 1. VPN 2’s private DNS address is not interchangeable. Replace all example values and record the starting configuration before making changes.

set -euo pipefail
DIRECT_IF='eth0'
VPN_IF='tun0'
APP_VPN_ADDR='10.8.0.2'
DNS_VPN_IF='tun1'
DNS_VPN_ADDR='10.9.0.2'
DNS_UPSTREAM='REPLACE_WITH_CHOSEN_VARIANT_DNS_IPV4'
DNS_STUB='127.0.0.53'
NEWS_UID=$(id -u newsvpn)
test "$NEWS_UID" -ne 0
test "$DNS_UPSTREAM" != 'REPLACE_WITH_CHOSEN_VARIANT_DNS_IPV4'
test "$VPN_IF" != "$DNS_VPN_IF"
ip -4 addr show dev "$VPN_IF"
ip -4 addr show dev "$DNS_VPN_IF"
ip -4 rule show
ip -4 route show table main
ip -4 route show table 101
resolvectl status
iptables-save
ip6tables-save
readlink -f /etc/resolv.conf
cat /etc/resolv.conf
ss -lnutp 'sport = :53'

Confirm each local source address belongs to its stated interface. DNS_UPSTREAM must be a supported nonlocal unicast address, distinct from VPN transport and application endpoints. Resolve overlapping subnets or existing host routes first; do not force the topology with onlink or broad NAT. New chain names and comments must be unused. Confirm /etc/resolv.conf uses the existing stub and clients honor it.

The independent-provider variant, 4B, reuses these steps with a different DNS_UPSTREAM. Use one provider variant at a time.

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, VPN_IF (application VPN), and DNS_VPN_IF (DNS VPN), 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.

3. Restrict DNS to VPN 1

Prepare both chains, then attach them. Only the local stub and the selected upstream through VPN 1 are permitted DNS paths.

iptables -w -N DH_DNS_SPLIT4
iptables -w -A DH_DNS_SPLIT4 -o lo -d "$DNS_STUB" -p udp --dport 53 -j RETURN
iptables -w -A DH_DNS_SPLIT4 -o lo -d "$DNS_STUB" -p tcp --dport 53 -j RETURN
iptables -w -A DH_DNS_SPLIT4 -o "$DNS_VPN_IF" -d "$DNS_UPSTREAM" -p udp --dport 53 -j RETURN
iptables -w -A DH_DNS_SPLIT4 -o "$DNS_VPN_IF" -d "$DNS_UPSTREAM" -p tcp --dport 53 -j RETURN
iptables -w -A DH_DNS_SPLIT4 -p udp -m multiport --dports 53,853 -j REJECT
iptables -w -A DH_DNS_SPLIT4 -p tcp -m multiport --dports 53,853 -j REJECT
iptables -w -A DH_DNS_SPLIT4 -j RETURN
ip6tables -w -N DH_DNS_SPLIT6
ip6tables -w -A DH_DNS_SPLIT6 -p udp -m multiport --dports 53,853 -j REJECT
ip6tables -w -A DH_DNS_SPLIT6 -p tcp -m multiport --dports 53,853 -j REJECT
ip6tables -w -A DH_DNS_SPLIT6 -j RETURN
iptables -w -I OUTPUT 1 -m comment --comment dns-separate-vpns4 -j DH_DNS_SPLIT4
ip6tables -w -I OUTPUT 1 -m comment --comment dns-separate-vpns6 -j DH_DNS_SPLIT6

RETURN continues through the remaining firewall; it does not unconditionally accept a packet. Keep these restrictions and the account guard ahead of broad accepts, including established-connection accepts. Existing reply rules still apply.

This example allows ordinary IPv4 UDP/TCP DNS. Upstream IPv6 DNS and port 853 are blocked. Clients must not bypass the resolver through DoH, proxies, or other transports; port rules do not identify arbitrary DNS inside HTTPS. Containers, forwarded LAN traffic, and multicast discovery are outside this host-only example. Do not silently disable an existing encrypted-DNS configuration.

4. Route and Select the Resolver

Install the terminal host route before the usable VPN route:

ip -4 route add unreachable "$DNS_UPSTREAM/32" metric 32767
ip -4 route add "$DNS_UPSTREAM/32" dev "$DNS_VPN_IF" src "$DNS_VPN_ADDR" metric 100
ip -4 route show table main exact "$DNS_UPSTREAM/32"
ip -4 route get "$DNS_UPSTREAM"

If the usable route disappears, unreachable prevents a broader main-table route taking over. The firewall independently rejects DNS through the wrong interface. A tunnel that stops passing traffic while its route remains installed does not authorize fallback either.

Requests should use DNS_VPN_ADDR, with replies returning through VPN 1. Preferred route source selection does not rewrite an explicitly bound source. Verify both directions; do not widen the reader’s VPN 2 SNAT rule to compensate for a DNS return-path problem.

Select VPN 1 in the existing resolver while both interfaces are present:

resolvectl dns "$DIRECT_IF" ''
resolvectl domain "$DIRECT_IF" ''
resolvectl dns "$VPN_IF" ''
resolvectl domain "$VPN_IF" ''
resolvectl dns "$DNS_VPN_IF" "$DNS_UPSTREAM"
resolvectl domain "$DNS_VPN_IF" '~.'
resolvectl status

~. selects the resolver link; it is not an IP route. Reconcile other links, global DNS, and more-specific DNS domains through their configuration owner. Do not overwrite a managed /etc/resolv.conf.

These are temporary settings. A network manager can overwrite them, and interface removal loses per-link settings. Restore the verified route and resolver settings after recreation, keeping firewall restrictions in place, and repeat verification. Persistence and VPN lifecycle integration are deferred.

5. Allow the Reader to Use the Stub

Replace only the reader’s IPv4 account guard, installing its replacement before removing the old rule. VPN_IF remains VPN 2. Its IPv6 guard stays intact.

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

The reader asks the local stub for a name. The resolver sends its own upstream query through VPN 1; the reader’s later connection follows mark 0x100 through VPN 2. Run the reader under newsvpn using the host resolver. Do not use Example 1’s private DNS launcher or retain another scenario’s DNS rules.

6. Verify Each Path

VPN 1 and VPN 2 have separate jobs. If VPN 1’s DNS route becomes unavailable, fresh upstream DNS requests must stop. They must not switch to VPN 2 or the direct connection. If VPN 2’s application route becomes unavailable, the reader must stop making connections through that path; working DNS is not permission to use the direct connection.

Cached answers and existing connections can make an outage less apparent. A reader that already knows its server’s address may continue through VPN 2 when the DNS route is unavailable. That does not, by itself, mean DNS has escaped through another path.

Use a fresh, permitted name that exists. Run expected failures in the same root shell without stop-on-error handling, retaining the verified variables.

set +e
TEST_NAME='REPLACE_WITH_FRESH_PERMITTED_FQDN.'
ip -4 route get "$DNS_STUB" mark 0x100
ip -4 route get "$DNS_UPSTREAM"

The first lookup must use loopback; the second must select VPN 1. In a separate terminal, first open a root shell with sudo -i, then capture:

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

Run normal lookups and inspect counters:

runuser -u "$ORDINARY_USER" -- getent ahostsv4 "$TEST_NAME"
runuser -u newsvpn -- 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 -- 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_SPLIT4
ip6tables -w -nvL DH_DNS_SPLIT6
iptables -w -nvL DH_NEWS_STUB

Test upstream UDP and TCP explicitly. TCP to the stub does not prove TCP upstream. Use fresh names and inspect the destination, source, interface, and reply rather than counting a cache hit as a successful path test.

runuser -u "$ORDINARY_USER" -- dig -r -4 @"$DNS_UPSTREAM" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A
runuser -u "$ORDINARY_USER" -- dig -r -4 @"$DNS_UPSTREAM" +tcp +time=2 +tries=1 "$TEST_NAME" A
runuser -u newsvpn -- dig -r -4 @"$DNS_UPSTREAM" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A
runuser -u newsvpn -- dig -r -4 @"$DNS_UPSTREAM" +tcp +time=2 +tries=1 "$TEST_NAME" A

The first two should succeed through VPN 1. The reader’s direct-to-upstream attempts must fail: its DNS must use the stub. Also test another permitted IPv4 resolver, an IPv6 resolver, wrong-interface attempts, and port 853; none may become an upstream alternative. Test both UDP and TCP. An IPv6 request with no route does not prove firewall enforcement; interpret route state, counters, and captures together.

Use the foundation’s connection checks with fresh reader connections to a permitted server: reader traffic must use VPN 2, while ordinary applications stay direct. Verify replies as well as requests.

Remove the DNS Route

Keep both interfaces present. Remove only the usable DNS route; leave the terminal route and guards installed:

ip -4 route del "$DNS_UPSTREAM/32" dev "$DNS_VPN_IF" src "$DNS_VPN_ADDR" metric 100
ip -4 route show table main exact "$DNS_UPSTREAM/32"
ip -4 route get "$DNS_UPSTREAM"

Repeat fresh lookups from both accounts. Upstream DNS must fail; known-address reader connections may still use VPN 2. Confirm the route remains absent during the test. If its owner reinstalls it, route loss was not established.

Restore it, check the route, and repeat normal lookups:

ip -4 route add "$DNS_UPSTREAM/32" dev "$DNS_VPN_IF" src "$DNS_VPN_ADDR" metric 100

Remove the Application Route

Remove only the foundation’s usable application route:

ip -4 route del default dev "$VPN_IF" src "$APP_VPN_ADDR" metric 100 table 101
ip -4 route show table 101

Its unreachable default must remain. Fresh known-address reader connections must fail while DNS through VPN 1 continues. Restore the exact route and repeat connection checks:

ip -4 route add default dev "$VPN_IF" src "$APP_VPN_ADDR" metric 100 table 101

Repeat with both usable routes removed: both paths must fail. Restore each separately; each restores only its own role. Stop and inspect any unexpected command failure. Never weaken guards, flush connection tracking, or count existing connections as proof. These are finite routing-state checks, not VPN-operation tests or availability monitoring.

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.

For 4A/4B, compare against VPN 2’s 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 any application route removed during the verification checks.

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. In 4A/4B, VPN_IF is VPN 2, the application path; do not substitute DNS_VPN_IF (VPN 1).

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, VPN_IF (application VPN), and DNS_VPN_IF (DNS VPN), 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. For 4A/4B, restore the DNS VPN interface’s original lists with two more commands as well.

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 "$DNS_UPSTREAM/32" dev "$DNS_VPN_IF" src "$DNS_VPN_ADDR" metric 100
ip -4 route del unreachable "$DNS_UPSTREAM/32" metric 32767
iptables -w -D OUTPUT -m comment --comment dns-separate-vpns4 -j DH_DNS_SPLIT4
ip6tables -w -D OUTPUT -m comment --comment dns-separate-vpns6 -j DH_DNS_SPLIT6
iptables -w -F DH_DNS_SPLIT4
iptables -w -X DH_DNS_SPLIT4
ip6tables -w -F DH_DNS_SPLIT6
ip6tables -w -X DH_DNS_SPLIT6

Inspect partial setup or prior route loss first. Omit a deletion only if 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.

This arrangement gives you separate paths for DNS and selected application traffic. It does not guarantee that the two cannot be associated with you. A VPN provider may know your originating IP address or account, and a resolver can observe the requests it receives. EFF’s VPN explanation and DNS Privacy Considerations describe these limits.

Treat the separate paths as a way to control where traffic goes and reduce particular exposures. The providers you choose, the information you give them, and the applications you use still matter.

VPN 1’s native DNS may associate queries with its VPN session. Provider validity, trust, and availability remain your responsibility.

References: route types and source selection, firewall traversal, resolver settings, and DNS privacy.

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.