A Brief Configuration Guide for Clash (Meta) DNS
This guide provides a brief overview of DNS configuration in the Clash Meta kernel. It covers the basics of DNS, the issues of DNS hijacking, pollution, and leaks, and how Clash’s DNS configuration can resolve them. The article also includes detailed steps for setting up DNS in Clash Meta, helping users enhance their browsing experience by securing DNS queries. Ideal for readers familiar with Clash configuration files, this guide aims to simplify DNS setup and improve internet security.
Preface
Due to the Clash kernel being discontinued for a long timedeleted, and encountering persistent “strange” DNS resolution issues during my usage, I was troubled until I resolved my problem by using the Clash Meta kernel. Therefore, in the following, “Clash” will refer to the “Clash Meta kernel” [1].
Regarding the DNS configuration of Clash, I have “tinkered” with it multiple times. After reading some blogs and documentation [3-5], I felt it necessary to write an article to briefly summarize Clash’s DNS configuration, hoping to be of help. This article is suitable for readers who already have some understanding of Clash configuration files (for example, those who can customize Clash configuration files). If you are only using Clash, it is recommended to first read the official Clash documentation and other tutorials to gain a deep understanding of Clash’s configuration files [7] before reading this article.
What are DNS, DNS Hijacking, Pollution, and Leaks?
For these topics, you can study the fundamentals of networking and read some related articles/videos here. They will not be elaborated on here.
How to Solve?
Clash’s DNS configuration can help us solve these problems. Clash supports multiple DNS configuration methods [2], including traditional UDP and TCP-based DNS, as well as more secure options like DoH, DoT, DoQ, etc. This article will not introduce these DNS configuration methods. You can refer to the official Clash documentation [1-2].
Clash DNS Configuration
1 | dns: |
When Does Clash Perform DNS Requests?
To answer this question, let’s first understand the workflow of Clash DNS:
Clash DNS’s workflow is as shown in the above diagram. When Clash receives a request, it first performs rule matching. If the accessed website, for example, www.example.com
, matches a rule, Clash will perform the corresponding action based on the rule, such as direct connection, proxy, interception, etc.
- Proxy Match: If it matches “proxy”, Clash will forward the request to the proxy server, which will perform DNS resolution. In this case, Clash does not perform DNS requests.
- Direct Match: If it matches “direct”, Clash will perform DNS requests.
- Reject Match: If it matches “reject”, Clash will intercept directly, and Clash does not perform DNS requests.
If none of the rules match the domain, and it encounters IP-based rules (e.g., IP-CIDR,192.168.0.0/16,DIRECT
), then Clash will also perform DNS requests.
The Process of Domain Name Resolution in ClashDNS
When Clash performs a DNS request to resolve a domain name, it first checks the cache. If the cache contains the resolution result for the domain name, Clash will directly return the cached result. Otherwise, Clash will perform a DNS request based on the configured DNS servers.
- FakeIP-Direct Domains that are not hit will attempt to match the
nameserver-policy
. If the domain matches anameserver-policy
, Clash will use the DNS servers defined in thatnameserver-policy
to perform the DNS request. - If it does not match the
nameserver-policy
, Clash will use the DNS servers innameserver
to perform the DNS request. default-nameserver
is the default DNS, used to resolve the domains of “DNS servers” (for example, whennameserver: https://doh.pub/dns-query
is used, to resolvedoh.pub
).
How to Prevent DNS Hijacking, Pollution, and Leaks?
We can use secure DNS servers, such as DoH, DoT, etc. Some domestic providers may have DNS pollution issues, so we can use public DNS servers, especially with a fallback
strategy (or nameserver-policy
), to let foreign websites use foreign DNS servers.
Conclusion
DNS is an important infrastructure of the internet. Currently, there is also some research in academia on DNS security. For example, Professor Duan Haixin’s team at Tsinghua University has many outstanding research results in this area.
The security of DNS is crucial for our online experience. Clash’s DNS configuration can help users solve problems like DNS hijacking, pollution, and leaks in daily browsing. I hope this article is helpful to everyone.
References
[2] Clash Meta DNS Configuration
[3] Sukka - I Have Unique DNS Configuration and Usage Tips
[4] What is Fake-IP
[5] An In-Depth Introduction to Clash DNS’s Working Principles