There are situations where you may want some of your Internet traffic to use a VPN, while other traffic uses your ordinary Internet connection. But what about DNS? You may want all of your DNS requests to go through the VPN, regardless of which route the application uses afterward.
For example, you might use a VPN for your newsgroup reader while keeping your web browser on the direct Internet connection. In this example, both applications send their DNS requests through the VPN. The browser’s connection to the website still uses the direct route.
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 Connections
Use one Ubuntu host with Bash, iproute2, iptables/ip6tables, dig, getent, and tcpdump. Ubuntu 24.04 is the proposed lab baseline. Its existing systemd-resolved service must provide the 127.0.0.53 stub used by ordinary applications. The VPN and application routes already work; this walkthrough creates neither a VPN nor a resolver service.
For the small resolver example below, the VPN link starts with no DNS servers or search/routing domains configured. The direct link already supplies ordinary DNS. There must be no other DNS links, global upstream configuration, or search/routing domains competing with this example. Inspect resolvectl status before continuing; a different starting configuration needs its own resolver settings and restoration plan. Do not overwrite a managed /etc/resolv.conf.
The upstream transport is IPv4 UDP/TCP port 53. Upstream IPv6 DNS and port 853 are blocked. Application-controlled DNS over HTTPS and proxy resolution must be disabled or follow the chosen policy; port rules cannot identify DNS hidden inside HTTPS. Multicast name discovery, forwarded traffic, and containers are outside this example.
Run setup in one dedicated Bash shell, replacing the placeholder and example interface/address values:
set -euo pipefail
VPN_DNS='REPLACE_WITH_APPROVED_DNS_IPV4'
VPN_IF='tun0'
VPN_ADDR='10.8.0.2'
test "$VPN_DNS" != 'REPLACE_WITH_APPROVED_DNS_IPV4'
ip -4 addr show dev "$VPN_IF"
ip -4 rule show
ip -4 route show table main exact "$VPN_DNS/32"
iptables -w -S OUTPUT
ip6tables -w -S OUTPUT
resolvectl status
readlink -f /etc/resolv.conf
cat /etc/resolv.conf
Choose one supported, nonlocal unicast DNS address, distinct from the VPN transport endpoint. Confirm its validity and availability yourself. The tunnel must carry traffic to it and return replies to VPN_ADDR. Record the existing configuration. Stop if a route to that address already exists or another policy diverts the shared resolver away from the main table. The commands below assume a point-to-point tunnel; next-hop arrangements require their own route. set -e is not an automatic rollback.
2. Restrict DNS for Every Account
Create unused chains and populate both before attaching them. If either chain already exists, stop and inspect it; do not flush it or append duplicates.
iptables -w -N DH_DNS_VPN4
iptables -w -A DH_DNS_VPN4 -o lo -d 127.0.0.0/8 -p udp --dport 53 -j RETURN
iptables -w -A DH_DNS_VPN4 -o lo -d 127.0.0.0/8 -p tcp --dport 53 -j RETURN
iptables -w -A DH_DNS_VPN4 -o "$VPN_IF" -d "$VPN_DNS" -p udp --dport 53 -j RETURN
iptables -w -A DH_DNS_VPN4 -o "$VPN_IF" -d "$VPN_DNS" -p tcp --dport 53 -j RETURN
iptables -w -A DH_DNS_VPN4 -p udp -m multiport --dports 53,853 -j REJECT
iptables -w -A DH_DNS_VPN4 -p tcp -m multiport --dports 53,853 -j REJECT
iptables -w -A DH_DNS_VPN4 -j RETURN
ip6tables -w -N DH_DNS_VPN6
ip6tables -w -A DH_DNS_VPN6 -o lo -d ::1 -p udp --dport 53 -j RETURN
ip6tables -w -A DH_DNS_VPN6 -o lo -d ::1 -p tcp --dport 53 -j RETURN
ip6tables -w -A DH_DNS_VPN6 -p udp -m multiport --dports 53,853 -j REJECT
ip6tables -w -A DH_DNS_VPN6 -p tcp -m multiport --dports 53,853 -j REJECT
ip6tables -w -A DH_DNS_VPN6 -j RETURN
iptables -w -I OUTPUT 1 -m comment --comment dns-vpn-all4 -j DH_DNS_VPN4
ip6tables -w -I OUTPUT 1 -m comment --comment dns-vpn-all6 -j DH_DNS_VPN6
There is no account match. An application’s request to a local stub can pass this check, but the stub’s upstream request must use the chosen IPv4 server and VPN interface. Other upstream DNS, including IPv6 and UDP/TCP 853, is rejected. Existing encrypted-DNS arrangements require separate review rather than silently replacing them.
These are filter rules. RETURN continues through the remaining firewall; it does not accept a packet or override an application guard. Other traffic keeps its existing rules. Inspect both jumps ahead of broad accepts, including established-connection accepts:
iptables -w -S OUTPUT
iptables -w -S DH_DNS_VPN4
ip6tables -w -S OUTPUT
ip6tables -w -S DH_DNS_VPN6
3. Route the DNS Address Through the VPN
Selecting a DNS provider does not, by itself, determine the route used to reach it. A request to that server could still travel over your ordinary Internet connection.
Add a preferred VPN host route and a terminal route underneath it:
ip -4 route add unreachable "$VPN_DNS/32" metric 32767
ip -4 route add "$VPN_DNS/32" dev "$VPN_IF" src "$VPN_ADDR" metric 100
ip -4 route show table main exact "$VPN_DNS/32"
ip -4 route get "$VPN_DNS"
Both routes match the same address. The lower metric selects the VPN route; if it is removed, the unreachable host route prevents the ordinary default from taking over. The firewall independently rejects DNS sent through another interface. This routes all traffic to that address, not just DNS; no new DNS mark or table is needed.
An earlier policy rule may select another table. For example, the reader’s existing mark 0x100 selects table 101; that path must also use the VPN or fail. Keep its source translation and independent guards. The route’s src is a preferred source, not an address rewrite; verify the actual tunnel source and replies.
4. Select the DNS Provider
Your VPN provider may offer a DNS service, or you may prefer another DNS provider that you can reach through the VPN. Either approach can work. The important point is that you are choosing both who answers your DNS requests and how those requests get there.
For the stated starting configuration, set the VPN link’s DNS server and catch-all DNS routing domain:
resolvectl dns "$VPN_IF" "$VPN_DNS"
resolvectl domain "$VPN_IF" '~.'
resolvectl status
~. directs ordinary unicast DNS selection to this link. It is a resolver setting, not an IP route. More-specific domains or another link with ~. would compete, which is why the starting configuration excludes them. The direct link’s saved DNS settings remain available for deliberate withdrawal, but the firewall forbids using that upstream path while this policy is installed. Resolver commands, DNS selection
These are temporary settings. A manager can overwrite them, and an interface’s removal loses them. Persistence and reconnect integration are separate work. A resolver attempting another provider after such a change must still be blocked by the firewall; it is not an authorized fallback.
For the VPN-selected reader, use only Example 1’s private resolver files and launch helper, using this same VPN_DNS in the existing root shell. If those files already exist, inspect them and confirm their contents instead of recreating them. Do not install Example 1’s separate DNS chain or run its withdrawal sequence: the host-wide chains above already restrict DNS here. Keep both application account guards. Do not send that guarded reader to the host stub or add a broad loopback allowance.
5. Verify the Policy
If the permitted VPN paths become unavailable, new DNS requests that need an upstream answer must fail. Allowing them to switch quietly to direct DNS would defeat the purpose of this example.
Some things may continue working. Your host may already have an answer cached, and an established connection may not need another lookup. The checks below distinguish the shared resolver’s main-table route from the reader’s separate route in table 101.
In the same shell, allow expected diagnostic failures. Use a known, permitted name absent from local hosts files and caches; use fresh names for successive checks. Alternate resolvers are permitted test targets, not configured fallbacks.
set +e
TEST_NAME='REPLACE_WITH_PERMITTED_FQDN.'
OTHER_DNS4='REPLACE_WITH_OTHER_TEST_RESOLVER_IPV4'
OTHER_DNS6='REPLACE_WITH_TEST_RESOLVER_IPV6'
Replace these values before testing; OTHER_DNS4 must differ from VPN_DNS. Check setup and restoration results explicitly from here onward.
In another terminal, first open a root shell with sudo -i, then observe each test separately:
tcpdump -ni any -vv 'port 53 or port 853'
Normal and Blocked Lookups
From the root shell, run these checks as your ordinary account:
runuser -u "$ORDINARY_USER" -- getent ahostsv4 "$TEST_NAME"
runuser -u "$ORDINARY_USER" -- dig -r -4 +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A
runuser -u "$ORDINARY_USER" -- dig -r -4 +tcp +time=2 +tries=1 "$TEST_NAME" A
runuser -u "$ORDINARY_USER" -- dig -r -4 @"$VPN_DNS" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A
runuser -u "$ORDINARY_USER" -- dig -r -4 @"$VPN_DNS" +tcp +time=2 +tries=1 "$TEST_NAME" A
Require upstream traffic to the selected resolver through the VPN, with the expected source and replies. A local stub may appear in dig output; follow its upstream query in the capture. The explicit @VPN_DNS tests check upstream UDP and TCP independently, since a stub may choose its own upstream transport. Check the answer, not just the exit status. Encrypted VPN transport on the physical interface is expected; direct plaintext upstream DNS is not.
Repeat the NSS and UDP/TCP tests through Example 1’s vpn_dns_run helper, with verified absolute executable paths, so the reader’s private environment is tested too. It is a prerequisite here, not a newly installed service.
Record counters, attempt alternate IPv4 and IPv6 resolvers, then inspect the counters again:
iptables -w -nvL DH_DNS_VPN4
ip6tables -w -nvL DH_DNS_VPN6
runuser -u "$ORDINARY_USER" -- dig -r -4 @"$OTHER_DNS4" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A
runuser -u "$ORDINARY_USER" -- dig -r -4 @"$OTHER_DNS4" +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
iptables -w -nvL DH_DNS_VPN4
ip6tables -w -nvL DH_DNS_VPN6
Require failures with matching rejection evidence and no delivered queries. Test the ordinary account first so the reader’s older guard cannot hide a missing host-wide restriction. Repeat alternate IPv4 tests in the reader environment. No usable IPv6 route makes an IPv6 failure inconclusive unless counters or an IPv6-capable lab demonstrate enforcement.
Remove and Restore the Preferred DNS Route
Keep the VPN interface, terminal route, and firewall checks present. Remove only this example’s usable DNS route:
ip -4 route del "$VPN_DNS/32" dev "$VPN_IF" src "$VPN_ADDR" metric 100
ip -4 route show table main exact "$VPN_DNS/32"
ip -4 route get "$VPN_DNS"
Require the usable entry to be absent and the unmarked lookup to fail. If a manager reinstalls it, the test fault has not been established. Repeat fresh ordinary-account lookups: upstream DNS must fail without a direct fallback. A cached answer is not evidence of an upstream query, and routing can stop a packet before firewall counters increase.
The reader may still resolve through its separate working table 101. That is not a direct leak; this test removes only the main-table DNS route. Use Example 1’s separate table-101 route-loss check to verify that path, retaining all guards.
After checking the tunnel address is unchanged, restore the owned route:
ip -4 addr show dev "$VPN_IF"
ip -4 route add "$VPN_DNS/32" dev "$VPN_IF" src "$VPN_ADDR" metric 100
ip -4 route get "$VPN_DNS"
Stop on restoration errors. Repeat normal and blocked tests, then verify fresh real application connections: the reader through the VPN and an ordinary application directly. DNS policy does not change those application routes.
These are finite routing-policy checks. VPN operation covers tunnel startup and reconnects; availability monitoring covers unresponsive remote services. Neither is implemented here.
Compare the Public Exit Addresses
- Visit WhatIsMyIP.com in your ordinary browser and note the displayed public IPv4 address.
- 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.
6. Withdraw Only This Example
Stop affected workloads. For this baseline, clear the two DNS settings added to the previously empty VPN link, retaining firewall protection:
resolvectl domain "$VPN_IF" ''
resolvectl dns "$VPN_IF" ''
resolvectl status
Confirm the recorded direct DNS configuration remains intact. If the interface disappeared, its per-link settings are already gone; inspect rather than ignoring unexpected errors. Do not use these empty values to restore a host that started with different VPN DNS settings.
Remove the exact owned routes:
ip -4 route del "$VPN_DNS/32" dev "$VPN_IF" src "$VPN_ADDR" metric 100
ip -4 route del unreachable "$VPN_DNS/32" metric 32767
Only when deliberately restoring the previous DNS policy, remove the host-wide restrictions:
iptables -w -D OUTPUT -m comment --comment dns-vpn-all4 -j DH_DNS_VPN4
ip6tables -w -D OUTPUT -m comment --comment dns-vpn-all6 -j DH_DNS_VPN6
iptables -w -F DH_DNS_VPN4
iptables -w -X DH_DNS_VPN4
ip6tables -w -F DH_DNS_VPN6
ip6tables -w -X DH_DNS_VPN6
Check each result. For partial setup or a prior fault, omit a deletion only after confirming that specific entry is absent. Leave the reader’s account guards intact. Withdrawal permits ordinary direct DNS again; never run it automatically on VPN failure. No persistent service, monitor, or automatic switch is added.
Technical references: route types and metrics, policy rules, iptables behavior, and port matching.
Understand the Privacy Choice
Sending DNS through the VPN keeps those queries off your ISP’s direct DNS path. It does not hide the destinations of application connections that you deliberately send directly to the Internet. You are also trusting the VPN provider and the DNS provider with the traffic they handle.
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.