How it started
Today I wanted to install nvm on my Mac, but the installation script hung:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
It just couldn't connect to GitHub. Strange. Didn't I already have V2rayN running?
I tried a few more times. The connection kept timing out. That's when I realized my terminal traffic wasn't going through the proxy at all.
Why the terminal bypasses the proxy
V2rayN defaults to system proxy mode, which changes the system's proxy settings. But commands run in Terminal or iTerm2 don't pick up those settings. They look for environment variables such as http_proxy.
That explains why the browser could reach blocked sites while curl, wget, and npm in my terminal were all connecting directly.
Turning on TUN mode
After some tinkering, I found TUN mode in V2rayN. It creates a virtual network interface that takes over network traffic at the system level. Whichever app you're using, its traffic goes through the proxy.
Here's how I enabled it:
- Click the V2rayN menu bar icon.
- Go to Settings → Tun Mode Settings.
- Check “Enable Tun Mode.”
- Enter your administrator password when prompted the first time.
- Restart V2rayN.
I tried again:
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.40.3/install.sh | bash
It downloaded instantly. Much better.
Checking that the proxy works
If you're unsure, check your outgoing IP address:
curl ipinfo.io
If the result shows your proxy node's IP address instead of your usual one, it's working.
Another option: Set environment variables manually
If you don't want TUN mode, since you don't always need a system-wide proxy, you can configure the terminal manually:
export http_proxy=http://127.0.0.1:10809
export https_proxy=http://127.0.0.1:10809
export all_proxy=socks5://127.0.0.1:10808
The ports depend on your V2rayN configuration. Common defaults are 10809 for HTTP and 10808 for SOCKS5.
To save yourself some typing, add a switch to ~/.zshrc:
proxy_on() {
export http_proxy=http://127.0.0.1:10809
export https_proxy=http://127.0.0.1:10809
export all_proxy=socks5://127.0.0.1:10808
echo "Proxy ON"
}
proxy_off() {
unset http_proxy https_proxy all_proxy
echo "Proxy OFF"
}
Then use proxy_on to turn it on and proxy_off to turn it off.
Summary
- It's normal for terminal commands to bypass the system proxy.
- TUN mode is the easy option if you want everything to use the proxy.
- Use environment variables if you want more control.
I leave TUN mode on now. It's one less thing to think about every time I install something.