· Advanced Config · 8 min read

Clash DNS Configuration: nameserver, fallback, and DNS hijacking

Learn how mihomo’s nameserver, fallback, and fallback-filter settings work, what dns-hijack does in TUN mode, and how to apply ready-to-use DNS configuration examples.

Configuration order: verify DNS resolution first, then intercept requests

First, make sure mihomo’s DNS module returns results through its local port. Then configure TUN to intercept the system’s DNS requests. Test these steps separately: if the first fails, check the configuration and upstream servers; if it succeeds but the second fails, check the client settings and system routes first. Avoid changing upstream servers, enabling TUN, and switching to Fake-IP all at once—it becomes difficult to tell which change caused the result.

This guide uses mihomo (formerly Clash Meta) as an example. The GUI client handles configuration imports, overrides, and starting the core. Whether a field takes effect depends on the core actually running and the final, active YAML configuration. Don’t assume that a client with “Clash” in its name supports mihomo’s TUN settings unchanged. If your subscription updates automatically, put local-only settings in the override layer supported by your client, then check the final configuration after a refresh.

What nameserver, fallback, and fallback-filter do

FieldPurposeCommon misconception
nameserverPrimary upstream for queries not handled by a specific DNS policy.Assuming the list order defines a fixed primary-to-backup failover sequence.
fallbackProvides another set of upstreams for filtering results; it is not simply a timeout backup.Assuming the other group won’t receive a query if the primary upstream responds.
fallback-filterFilters results based on the addresses returned by the primary upstream or the queried domain.Mistaking address geolocation for the client’s location or for a proxy-routing rule.
default-nameserverResolves hostnames for DNS upstreams that use domain names.Treating it as the default upstream for regular application queries.
proxy-server-nameserverResolves domain names in proxy node addresses.Creating a startup dependency by using a resolver that can only be reached through that node.

With fallback configured, regular queries may be sent to both upstream groups in parallel, with the result selected according to the filter conditions. Don’t judge by which group responds first. For example, with geoip: true and geoip-code: CN, address queries handled by this condition will use the fallback result if the primary upstream returns an address outside the specified geographic region. This checks the location associated with the returned address, not the upstream server’s location. Geolocation also doesn’t indicate resolution quality: a service may use different points of presence in different regions, and switching answers can change the network route used to reach it.

Use fallback-filter.ipcidr to treat primary-upstream results within specified IP ranges as candidates for replacement by the fallback group. Use fallback-filter.domain to specify domains that should be resolved by the fallback group. If an internal domain must always use your organization’s DNS, consider nameserver-policy and make sure the core can reach that resolver. Public upstreams can’t reliably guess answers for private domains. Parallel queries also mean the upstream group whose result isn’t selected may still receive the query.

A DNS configuration you can merge: start with redir-host

Merge the snippet below into the existing top-level dns: section. Don’t add a second top-level key with the same name or overwrite existing proxies, proxy groups, or rules. The example listens on 127.0.0.1:1053, so you can test DNS independently of the system’s default resolution path. redir-host also makes it easy to inspect the actual resolved addresses. Make sure the public resolvers in the example are reachable from your current network. To test only the primary upstream, remove the fallback and fallback-filter sections first, then add them back one at a time once the primary is working.

dns:
  enable: true
  listen: 127.0.0.1:1053
  enhanced-mode: redir-host
  default-nameserver:
    - 223.5.5.5
  nameserver:
    - https://dns.alidns.com/dns-query
  fallback:
    - https://1.1.1.1/dns-query
  fallback-filter:
    geoip: true
    geoip-code: CN
    ipcidr:
      - 240.0.0.0/4

The primary upstream is a DoH service addressed by hostname. The core must first resolve that hostname through a bootstrap resolver before it can send an HTTPS query. Here, 223.5.5.5 is a plaintext DNS bootstrap address; specifying HTTPS for the upstream doesn’t mean the bootstrap query also uses HTTPS. If the logs show that the DoH hostname can’t be resolved, first check that the bootstrap address is reachable. If the hostname resolves but the HTTPS handshake fails, check the system clock, network connectivity, and certificate errors.

TUN mode dns-hijack: match UDP and TCP port 53

Set dns-hijack under the top-level tun: section to send DNS traffic entering TUN that matches the specified conditions to the core’s DNS module. It doesn’t choose an upstream or change the operating system’s DNS port to 1053. The listener port above is for testing DNS resolution by the core; these rules match the destination port applications originally send their DNS requests to.

tun:
  enable: true
  stack: mixed
  auto-route: true
  auto-detect-interface: true
  dns-hijack:
    - 'any:53'
    - 'tcp://any:53'

any:53 matches UDP port 53; the second entry matches TCP port 53. TCP isn’t only for apps with special settings: a resolver may retry over TCP if a UDP response is truncated. On desktop, if the client has a separate “TUN mode” or “virtual network adapter” switch, make sure the adapter actually starts after saving the YAML, and check whether the switch overrides the fields in this example. On mobile, the app manages VPN routing; desktop auto-route settings aren’t a cross-platform switch.

These two rules only handle matching traffic that actually enters TUN. If the system sends queries to a LAN router, they may take the local network route instead of entering TUN. A browser’s Secure DNS may use HTTPS over port 443, while apps with built-in DoT typically use TCP port 853; neither matches these port 53 rules. When troubleshooting, first note the resolver, protocol, and destination port used by the app. Then determine whether routing failed to intercept the traffic or the query simply doesn’t match either rule.

Verify and roll back: diagnose by symptom

  1. Local DNS query times out: Check that dns.enable is true in the final configuration. Review the startup logs for listener errors, port conflicts, or upstream timeouts. You can use dig @127.0.0.1 -p 1053 example.com A +tcp to test a local DNS response over TCP, but this still isn’t a TUN test.
  2. Local queries work, but system lookups are unchanged: After enabling TUN, run nslookup example.com without specifying a server and note which server the tool reports. Then check the client logs and system routes to confirm whether the request reaches the core. The server address shown by the tool is the destination it queried; it isn’t necessarily the upstream mihomo ultimately used. Account for system, app, and core caches before comparing results, so an old answer isn’t mistaken for the result of the new configuration.
  3. Domains resolve, but proxy nodes won’t connect: Check whether the node’s server is a hostname or an IP address. If it’s a hostname, investigate its resolution path separately. If needed, set proxy-server-nameserver to a resolver that’s reachable before the node connects. If the node address is an IP, changing this field won’t affect the connection; check the port, network, and client logs instead.
  4. DNS responds, but pages still time out: In the connections panel, check the target domain, matched rule, and outbound route. A DNS answer only completes the resolution step; it doesn’t prove the destination port is reachable or change whether the app connects through DIRECT or a proxy. If the issue started after these changes, first turn off TUN and confirm the system’s original route works. Then restore the DNS section from before the changes and reload the configuration.

How to interpret results after switching to Fake-IP

If you need Fake-IP, switch enhanced-mode to fake-ip only after the upstream and TUN tests pass. With the commonly used fake-ip-range: 198.18.0.1/16, addresses in that range are usually expected mappings, not the website’s real IP addresses. The core must continue handling the app’s subsequent connections so it can map those addresses back to domain names. If an internal service genuinely requires real addresses, use fake-ip-filter to create the smallest practical exclusion, and make sure a reachable upstream can resolve the internal domain. After switching back to redir-host, clear the affected app’s DNS cache and test the same domain again to avoid using a stale mapping.

Download Clash