--- title: "DNS Follows VPN Applications" description: "Keep DNS for selected applications on their VPN while other applications retain their normal DNS path." url: /tutorials/networking/vpn-dns/dns-for-vpn-applications/ article_id: vpn-dns-1 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/). You may want your newsgroup reader to use a VPN while your other applications keep using the direct Internet connection. But what about the reader's DNS requests? If those requests use the host's ordinary resolver, they may take a different path from the connection that follows. In this example, the reader uses the VPN provider's DNS through the VPN. Other applications keep their normal DNS configuration. The reader's DNS and application connections must stay on the VPN path; neither is allowed to fall back to the direct connection. {{% 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 Application Route Begin with the [packet-marks configuration](/downloads/review/packet-marks-technical-prerequisite.txt) already prepared and verified: - Dedicated, unprivileged account `newsvpn`; complete packet mark `0x100`; rule priority `10100` selecting table `101`. - Working point-to-point interface `tun0`, local address `10.8.0.2`; direct interface `eth0`. - Table `101` contains `default dev tun0 src 10.8.0.2 metric 100` and `unreachable default metric 32767`. - Independent IPv4 account guard blocks output outside `tun0`; the IPv6 account guard blocks all IPv6 output. Marking, scoped source translation, reverse-path handling, and permitted replies are already verified. Substitute your actual interface names and tunnel address consistently. This walkthrough adds DNS selection and restrictions to that foundation; it does not install another application route. The VPN must leave ordinary applications' direct routing and DNS intact. Use a normal Ubuntu host with glibc and the system *systemd* manager, Bash, *iproute2*, *iptables*/*ip6tables*, `getent`, `dig`, and `tcpdump`. Ubuntu 24.04 is the proposed lab baseline, not a tested-platform claim. Record the [installed versions](/reference/networking/vpn-dns-policies/routing-tools-and-scope/#check-your-own-system). The reader must use ordinary resolver APIs, with application-specific DNS, DNS over HTTPS, and proxy-delegated resolution disabled. These rules do not contain hostile software or identify DNS hidden inside HTTPS. {{% callout style="important" %}} Keep independent recovery access and leave the reader and its helpers stopped during setup. Stop on any preparation error; leave the existing account guards installed. Test both DNS and real application connections before everyday use. {{% /callout %}} ## 2. Choose the VPN Resolver Choosing the VPN's resolver also means trusting that provider with the DNS requests it receives. The purpose here is to keep those requests on the selected path, not to claim that using a VPN makes the activity anonymous. Obtain one valid, nonlocal IPv4 resolver address from your VPN provider. Confirm its identity and availability before starting. Do not configure a public fallback in this example. Run the setup blocks in one dedicated Bash shell: ```bash set -euo pipefail VPN_DNS='REPLACE_WITH_PROVIDER_DNS_IPV4' NEWS_UID=$(id -u newsvpn) test "$NEWS_UID" -ne 0 test "$VPN_DNS" != 'REPLACE_WITH_PROVIDER_DNS_IPV4' ip -4 route get "$VPN_DNS" mark 0x100 ip -4 route show table 101 iptables -w -S OUTPUT ip6tables -w -S OUTPUT ``` Require the marked lookup to select `tun0` and table `101`, with the terminal route still present. Check that both account guards precede broad accept rules. A route lookup alone does not verify firewall behavior or source translation. `set -e` stops these simple setup blocks on errors; it does not roll back completed changes. ## 3. Restrict the Reader's DNS Create an unused chain, populate it, then attach it to *filter* `OUTPUT`. If `DH_NEWS_DNS` already exists, stop and inspect it instead of rerunning or flushing it. Use the same resolver address throughout. ```bash iptables -w -N DH_NEWS_DNS iptables -w -A DH_NEWS_DNS -o tun0 -d "$VPN_DNS" -p udp --dport 53 -j RETURN iptables -w -A DH_NEWS_DNS -o tun0 -d "$VPN_DNS" -p tcp --dport 53 -j RETURN iptables -w -A DH_NEWS_DNS -p udp -m multiport --dports 53,853 -j REJECT iptables -w -A DH_NEWS_DNS -p tcp -m multiport --dports 53,853 -j REJECT iptables -w -A DH_NEWS_DNS -j RETURN iptables -w -I OUTPUT 1 -m owner --uid-owner "$NEWS_UID" -m comment --comment newsvpn-dns-policy -j DH_NEWS_DNS iptables -w -S OUTPUT iptables -w -S DH_NEWS_DNS ``` Only `newsvpn` enters this chain. UDP/TCP port 53 may reach the selected resolver through `tun0`; other DNS destinations, including a loopback stub, are rejected. UDP/TCP port 853 is also rejected because those encrypted-DNS transports are not configured here. `RETURN` continues through the remaining `OUTPUT` rules; it does not accept the packet. Keep the account guard after this jump and before broad accepts. DNS restrictions use the account independently of its mark. Ordinary applications do not enter the chain. No new NAT rule or loopback exception is needed. ## 4. Give the Reader Its Own Resolver Files A shared resolver sends upstream queries under its own identity. It cannot be assumed to preserve the requesting application's account or mark. Here the reader uses private resolver files, so its own DNS sockets follow the existing application route. Create these files in a new root-owned directory: ```bash test ! -e /etc/datahacker/newsvpn-dns install -d -o root -g root -m 0755 /etc/datahacker/newsvpn-dns printf 'nameserver %s\noptions timeout:2 attempts:2\n' "$VPN_DNS" | tee /etc/datahacker/newsvpn-dns/resolv.conf >/dev/null cp -- /etc/nsswitch.conf /etc/datahacker/newsvpn-dns/nsswitch.conf sed -i '/^[[:space:]]*hosts[[:space:]]*:/d' /etc/datahacker/newsvpn-dns/nsswitch.conf printf '\nhosts: files dns\n' | tee -a /etc/datahacker/newsvpn-dns/nsswitch.conf >/dev/null chown root:root /etc/datahacker/newsvpn-dns/resolv.conf /etc/datahacker/newsvpn-dns/nsswitch.conf chmod 0644 /etc/datahacker/newsvpn-dns/resolv.conf /etc/datahacker/newsvpn-dns/nsswitch.conf ``` `hosts: files dns` checks `/etc/hosts`, then ordinary DNS. It avoids delegation through the host's `nss-resolve` module; changing only `resolv.conf` would not do that. Other NSS databases remain copied unchanged. The host's original files are not replaced. ## 5. Launch with the Private Configuration Define this helper in the same shell. It uses a transient system service to give each command a private **mount namespace**, while retaining the host's network namespace and existing routing. This is an application launch environment, not VPN startup or a persistent service installation. ```bash vpn_dns_run() { systemd-run --quiet --wait --pipe --collect \ --property=Type=exec \ --property=User=newsvpn \ --property=NoNewPrivileges=yes \ --property=CapabilityBoundingSet= \ --property=PrivateMounts=yes \ --property='RestrictAddressFamilies=AF_UNIX AF_INET' \ --property='UnsetEnvironment=LOCALDOMAIN RES_OPTIONS HOSTALIASES' \ --property='BindReadOnlyPaths=/etc/datahacker/newsvpn-dns/resolv.conf:/etc/resolv.conf /etc/datahacker/newsvpn-dns/nsswitch.conf:/etc/nsswitch.conf' \ --property='InaccessiblePaths=-/run/nscd -/var/run/nscd -/var/cache/nscd -/run/systemd/resolve/io.systemd.Resolve' \ -- "$@" } vpn_dns_run /usr/bin/id vpn_dns_run /usr/bin/cat /etc/resolv.conf vpn_dns_run /usr/bin/grep '^hosts:' /etc/nsswitch.conf cat /etc/resolv.conf grep '^hosts:' /etc/nsswitch.conf ``` Require the helper to run as `newsvpn` and show the private files; your ordinary shell must still show the host's configuration. The hidden paths prevent the usual shared resolver/cache interfaces from defeating this separation. Nonstandard cache paths or delegated helpers need separate review. The IPv4 socket restriction supplements the IPv6 firewall guard. Use verified absolute executable paths. Do not substitute `--user`, `--scope`, or plain `runuser -u newsvpn --`. The reader and its children must use this environment. Its configuration and download directory must be accessible to `newsvpn`; independently launched helpers do not inherit the private files. Arguments containing literal dollar signs need separate handling because *systemd* performs command-line expansion. ## 6. Check the Policy Allow expected diagnostic failures without exiting the shell. Use a known, permitted test name absent from `/etc/hosts`, preferably fresh names in a domain you control. The alternate resolver is a test target, not a fallback. ```bash set +e TEST_NAME='REPLACE_WITH_PERMITTED_FQDN.' OTHER_DNS='REPLACE_WITH_OTHER_PERMITTED_TEST_RESOLVER_IPV4' ``` Replace both placeholders; `OTHER_DNS` must differ from `VPN_DNS`. Check every setup and restoration command's result explicitly from this point onward. In another terminal, first open a root shell with `sudo -i`, then observe the queries while running each test separately: ```bash tcpdump -ni any -vv 'port 53 or port 853' ``` Match each query's name, time, destination, interface, and source. Reader queries must leave `tun0` with the expected tunnel source, never directly through `eth0` or another egress. Ordinary DNS should use its normal direct path. The physical interface still carries the encrypted VPN transport. Use uncached lookups to demonstrate upstream traffic. ### Normal and Blocked Lookups ```bash vpn_dns_run /usr/bin/getent ahostsv4 "$TEST_NAME" vpn_dns_run /usr/bin/dig -r -4 +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A vpn_dns_run /usr/bin/dig -r -4 +tcp +time=2 +tries=1 "$TEST_NAME" A runuser -u "$ORDINARY_USER" -- getent ahostsv4 "$TEST_NAME" iptables -w -nvL DH_NEWS_DNS vpn_dns_run /usr/bin/dig -r -4 @"$OTHER_DNS" +notcp +ignore +time=2 +tries=1 "$TEST_NAME" A vpn_dns_run /usr/bin/dig -r -4 @"$OTHER_DNS" +tcp +time=2 +tries=1 "$TEST_NAME" A iptables -w -nvL DH_NEWS_DNS ``` The first three lookups must use the VPN resolver; the ordinary lookup keeps its normal provider. Both alternate-resolver tests must fail, with matching rejection-counter increases and no delivered query. If the host has a loopback DNS stub, repeat the two alternate-resolver tests with its actual address; those must also be rejected. Confirm the DNS answer and reported server as well as the capture. A zero `dig` exit status alone does not establish successful name resolution; a failed command alone does not prove enforcement. If ordinary DNS is encrypted on another port, observe its configured provider endpoint separately. ### Remove and Restore the Preferred Route Keep the VPN interface present. Remove only the usable route from this example's table, leaving the terminal route and all guards intact: ```bash ip -4 route del default dev tun0 src 10.8.0.2 metric 100 table 101 ip -4 route show table 101 ip -4 route get "$VPN_DNS" mark 0x100 ``` Require the usable route to be absent and the marked lookup to fail. If a manager reinstalls the route, the fault has not been established; stop the test rather than repeatedly deleting routes. With a fresh `TEST_NAME`, repeat the first four normal-lookup commands. Reader lookups must fail without direct queries; ordinary DNS must continue. Routing may reject traffic before firewall counters increase. After checking that `tun0` still has the expected address, restore the exact route and repeat the normal and blocked tests: ```bash ip -4 addr show dev tun0 ip -4 route add default dev tun0 src 10.8.0.2 metric 100 table 101 ip -4 route get "$VPN_DNS" mark 0x100 ``` Stop on any restoration error. Cached answers may survive route loss. Also repeat the foundation's fresh application-connection tests, including a known server address and the IPv6 guard, so DNS failure does not stand in for application protection. After the checks pass, launch your chosen command-line reader through `vpn_dns_run`, using its verified absolute executable path and required configuration arguments. Confirm a fresh connection and permitted reader activity through the VPN, including its helpers. This walkthrough does not invent a universal launch command for different reader programs. These are finite routing-policy checks. VPN disconnect/reconnect handling belongs in the [VPN operation guide](/review/vpn-operation-integration/); remote service response and monitoring belong in the [availability guide](/review/dns-availability-review/). No monitor or automatic switching is added 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. ## 7. Withdraw This DNS Example Stop all reader and diagnostic commands first. Keep the foundation's account guards. Remove only this example's jump and chain, checking each command before continuing: ```bash iptables -w -D OUTPUT -m owner --uid-owner "$NEWS_UID" -m comment --comment newsvpn-dns-policy -j DH_NEWS_DNS iptables -w -F DH_NEWS_DNS iptables -w -X DH_NEWS_DNS ``` For partial setup, inspect what exists and omit a jump deletion only if it was never attached. Leave the reader stopped after withdrawal: its VPN-only guard alone does not restrict DNS to this provider. Keep the private files for inspection; the host's resolver files need no restoration. Review the copied NSS file after host NSS changes. Reboot persistence and integration with firewall reloads are deferred. ## Technical References - [systemd execution properties](https://www.freedesktop.org/software/systemd/man/latest/systemd.exec.html) and [systemd-run](https://www.freedesktop.org/software/systemd/man/latest/systemd-run.html). - [nss-resolve](https://www.freedesktop.org/software/systemd/man/latest/nss-resolve.html) and [NSS configuration](https://man7.org/linux/man-pages/man5/nsswitch.conf.5.html). - [iptables extensions](https://man7.org/linux/man-pages/man8/iptables-extensions.8.html), [chain behavior](https://www.netfilter.org/documentation/HOWTO/packet-filtering-HOWTO-7.html), and [route types](https://man7.org/linux/man-pages/man8/ip-route.8.html). All runtime checks remain untested on Linux. See the [technical change record](/review/vpn-dns-application-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-application-integrated/), [separate prose and technical change records](/review/vpn-dns-integration-changes/).