Git clones stall, npm install times out, and a browser works while a terminal does not. These symptoms often come from the difference between an application-aware proxy and a system-wide traffic route. Clash TUN mode can capture connections from command-line programs that do not read desktop proxy settings, then apply Clash rules to them. This guide shows how to enable TUN safely, route developer tools such as Git, npm, pip, and Homebrew, and trace failures without accidentally sending private repositories or internal package registries through the wrong proxy.
Understand What TUN Mode Captures
A normal Clash HTTP or SOCKS proxy listens on a local port. Applications must know that port and speak the corresponding proxy protocol. A browser may follow the operating system’s proxy setting, but many terminal programs do not. A process started in a shell may instead connect directly, bypassing Clash entirely.
TUN mode creates a virtual network interface and routes selected system traffic through it. Mihomo, the core commonly used by current Clash-compatible clients, then inspects the connections and applies the profile’s rules. A command-line tool can therefore be routed even if it has no built-in Clash integration and no proxy environment variables are set.
TUN is not a rule that sends every terminal request to a proxy. It is a way to make connections visible to the core. The final action still depends on the matching rule and its policy group. If a domain matches a DIRECT rule, its traffic remains direct; if it matches a proxy rule, Clash sends it through the selected proxy group.
- Use TUN when: applications ignore system proxy settings, you want domain-based routing for most system traffic, or several CLI tools need consistent handling.
- Use an explicit proxy when: you only need to route one tool, want the behavior to be visible in a shell script, or cannot grant the client permission to create a virtual interface.
- Use both selectively: TUN can provide general capture while per-tool proxy settings can override or document behavior for a specific workflow.
Enable TUN in Your Clash Client
In Clash Verge, Clash Verge Rev, or another Mihomo-based client, select a profile that uses a compatible core, then find the TUN or service-mode settings. The exact labels vary by client version. Enable the TUN interface, approve the operating system’s permission prompt, and start the service or core with the required privileges. If the client offers an automatic route option, it is usually the simplest starting point on a personal machine.
On Windows, TUN setup may require administrator approval or installation of a supporting network driver. On macOS, the client may ask you to approve a system extension or network configuration. On Linux, the process needs permission to create a TUN device and change routes; running a core as an unprivileged user without the necessary capabilities may fail. Follow the client’s current prompt rather than copying old instructions for a different release.
A Mihomo profile can contain a TUN section similar to the following. Treat it as a reference, not a universal drop-in: supported fields and defaults can differ across core versions and clients. If your client manages TUN settings outside the profile, avoid defining conflicting values in both places.
tun:
enable: true
stack: mixed
auto-route: true
auto-detect-interface: true
dns-hijack:
- any:53
dns:
enable: true
enhanced-mode: fake-ip
nameserver:
- 1.1.1.1
- 8.8.8.8
The stack setting selects the TUN networking implementation. A mixed stack is a practical compatibility choice when it is supported by your core, but it is not guaranteed to be best for every operating system or workload. auto-route asks the core to install routes, while auto-detect-interface helps it choose the active network interface. DNS hijacking can direct ordinary DNS requests to Clash’s DNS module, but encrypted DNS, application-specific resolvers, and VPN software may behave differently.
After enabling TUN, verify that the client reports it as running and that ordinary browsing still works. Then test one command-line connection before changing multiple settings. If the connection works in the browser but not in a terminal, check the Clash connection list or logs while the terminal command is running. A matching connection entry indicates that TUN captured it; no entry suggests a routing, permission, or capture issue.
Build Rules for Development Traffic
Good developer routing starts with a clear distinction between public services and private infrastructure. Git hosting, public package registries, and documentation sites may need a proxy on your network. Internal Git servers, company registries, local development domains, and private address ranges usually need to stay direct or follow a separate trusted route.
Rules are evaluated in order, so put specific exceptions before broad service rules and keep the final catch-all rule last. The following example illustrates the structure; replace the group names with names that exist in your own profile. A rule that refers to a nonexistent group can make a configuration invalid or behave differently than intended.
rules: - DOMAIN-SUFFIX,git.internal.example,DIRECT - DOMAIN-SUFFIX,registry.internal.example,DIRECT - DOMAIN-SUFFIX,github.com,Developer - DOMAIN-SUFFIX,githubusercontent.com,Developer - DOMAIN-SUFFIX,npmjs.org,Developer - DOMAIN-SUFFIX,pythonhosted.org,Developer - DOMAIN-SUFFIX,pypi.org,Developer - DOMAIN-SUFFIX,homebrew.sh,Developer - IP-CIDR,10.0.0.0/8,DIRECT,no-resolve - IP-CIDR,172.16.0.0/12,DIRECT,no-resolve - IP-CIDR,192.168.0.0/16,DIRECT,no-resolve - GEOIP,LAN,DIRECT,no-resolve - MATCH,Default
The domain list is illustrative rather than exhaustive. Package managers can contact multiple hosts for metadata, archives, mirrors, authentication, and content delivery. A clone may reach the Git host successfully but fail when downloading a release asset from another domain. Use Clash’s connection view to identify the actual hostname, then add a narrow rule only when you understand why it needs a different policy.
Keep private destinations explicit. A broad rule that proxies every domain can expose internal hostnames to a proxy provider or make a private service unreachable. Conversely, broad direct rules can send traffic outside the intended route. For corporate networks, follow the organization’s policy and use its approved VPN, proxy, and certificate configuration. Do not assume that a private IP range alone captures every internal service; split DNS, IPv6, and custom internal domains may also need consideration.
Configure Git, npm, pip, and Homebrew
With TUN operating correctly, many tools can connect without extra proxy settings. That is often the cleanest arrangement: Clash captures the connection, and the profile determines the route. Before adding per-tool proxy configuration, test the command with the environment variables cleared in a temporary shell. This helps distinguish a TUN problem from a stale proxy address left by a previous setup.
If you prefer explicit proxy behavior, use the local HTTP or SOCKS port shown in your Clash client. Do not assume a default port: profiles and clients can use different values, and a mixed port may accept more than one protocol while separate HTTP and SOCKS ports remain distinct. The examples below use a placeholder port. Replace it with the actual listening port on your machine.
export http_proxy=http://127.0.0.1:7890 export https_proxy=http://127.0.0.1:7890 export all_proxy=socks5h://127.0.0.1:7891 export no_proxy=localhost,127.0.0.1,::1,.internal.example # Remove the variables when you want to test TUN-only routing unset http_proxy https_proxy all_proxy no_proxy
For Git, an explicit proxy can be useful for a single repository or a controlled shell session. Git supports both HTTP proxy configuration and environment variables, but a persistent global setting can surprise you when working with internal hosts or switching networks. Prefer a repository-specific or temporary setting when you need a narrow override, and remove it when it is no longer needed.
# Set a proxy for the current repository only git config http.proxy http://127.0.0.1:7890 # Inspect the effective Git proxy configuration git config --show-origin --get-regexp 'http\..*proxy' # Remove the repository-specific setting git config --unset http.proxy
For npm, configure a proxy only if TUN routing alone is insufficient or if your organization requires a specific proxy endpoint. An explicit npm setting can outlive the network situation in which it was created, so check it when installs fail unexpectedly. Be especially careful with private registry URLs: an internal registry should use the route and credentials required by your organization, not an assumed public proxy path.
# Configure npm to use the local HTTP proxy npm config set proxy http://127.0.0.1:7890 npm config set https-proxy http://127.0.0.1:7890 # Inspect the settings npm config get proxy npm config get https-proxy # Remove the settings to return to TUN-only routing npm config delete proxy npm config delete https-proxy
pip commonly respects proxy environment variables, making a temporary shell configuration easy to test. Homebrew also uses standard network and proxy behavior, but its downloads may involve more than one host. If a package manager reports that a particular archive host failed, inspect the hostname and route rather than assuming that the main project domain is the only dependency.
Use credentials cautiously. Avoid placing proxy passwords directly in shell history, committed scripts, or a shared configuration file. For a team workflow, document which settings are temporary, which are local to a developer machine, and how to remove them. A proxy variable exported in a shell startup file can silently affect every later terminal process, including tools that should use a direct internal connection.
Diagnose Clones and Package Install Failures
Debug one layer at a time. Start with the exact command and the failing hostname, then check whether the connection appears in Clash. If it appears, inspect the matched rule, selected policy, connection result, and DNS information. If it does not appear, focus on TUN activation, operating-system routes, permissions, and competing network software before editing domain rules.
- Confirm the target: identify whether the command uses HTTPS, SSH, a private registry, a mirror, or a redirected download host. The visible command may not reveal every destination.
- Check capture: run the command again while watching the Clash connection list. Confirm that the connection is present and see which rule and group handle it.
- Check DNS: compare the resolved hostname with the address and route shown by the client. A stale record, split-DNS requirement, or application-specific resolver can make a correct proxy rule appear ineffective.
- Check the selected policy: a rule can match correctly while its proxy group has no usable node, is set to a direct option, or is waiting for a manual selection.
- Check explicit overrides: inspect shell variables, Git configuration, npm configuration, and tool-specific settings. Remove stale values temporarily, then repeat the same test.
- Check the transport: distinguish a timeout from an authentication failure, certificate error, SSH handshake problem, or package integrity error. These point to different causes.
For a quick shell check, print the environment variables that commonly affect command-line requests and inspect the relevant tool configuration. On Unix-like systems, env | grep -i proxy can reveal exported variables. Git can report the origin of a configured value, and npm can display its active proxy fields. On Windows, inspect both the current PowerShell environment and the client’s active TUN state; a value set in one terminal session may not apply to another.
Git over HTTPS and Git over SSH are different connection paths. An HTTPS clone typically connects to port 443 and may follow an HTTP proxy setting. SSH commonly uses port 22, and many HTTP proxy configurations do not automatically tunnel SSH. If HTTPS works but SSH fails, test the SSH route separately and verify the host, port, firewall, and any supported SSH proxy method. Do not conclude that TUN is broken solely because one transport behaves differently.
On Windows, check whether another VPN, endpoint-security product, or virtual network adapter is competing for routes. On macOS, confirm that the approved network extension remains active after sleep or a network change. On Linux, verify that the TUN device and route were created and that DNS service management has not replaced the resolver configuration. When testing, change one setting at a time and restore it if it makes the behavior worse.
Frequently Asked Questions
Does TUN proxy every terminal command?
No. TUN makes eligible network traffic available to Clash, but rules determine whether each connection is direct, proxied, rejected, or handled by another policy. Local traffic, excluded networks, unsupported protocols, and operating-system routing choices can affect what is captured. Check the client’s connection view rather than assuming that enabling TUN means every request uses a remote proxy.
Should I use proxy environment variables with TUN?
Not necessarily. If TUN captures the connection and the rules route it as intended, environment variables may be unnecessary. They remain useful for an explicit, reproducible tool-level route or for diagnosing whether the TUN path is the source of a problem. Avoid keeping a global setting enabled without understanding how it affects internal services and other networks.
Why does Git work while npm or pip fails?
The tools may contact different domains, use different transports, follow redirects, or read different proxy configuration. The main Git host might match a rule while a package archive host does not. Inspect the failed connection in Clash, identify its actual hostname, and check the package manager’s own configuration and environment variables before adding a rule.
Will TUN break local development services?
It should not when loopback and private-network traffic are handled appropriately, but custom routes and broad proxy rules can interfere with local APIs, containers, virtual machines, or internal domains. Keep local and trusted destinations explicit, test services such as localhost and private registry hosts, and follow your network administrator’s guidance for corporate ranges.
Once TUN capture, DNS, and rule order are verified, developer traffic becomes much easier to reason about: public Git and package services follow the intended policy, while internal destinations can remain on a trusted route. Keep per-tool overrides minimal, record the local ports you actually use, and use Clash’s connection details to investigate the next failed clone or install. If you still need a client or a step-by-step setup reference, visit the download page or setup guide.