Search code examples
ubuntuwindows-subsystem-for-linuxv2ray

How to use v2rayN in windows wsl2 Ubuntu


I have v2rayN app in windows, when I set it on global it will not change my IP in terminals and I want to use it in wsl2 Ubuntu for my projects.

I tried to use it by connecting the port in ubuntu by

export http_proxy="socks5://127.0.0.1:10808"
export https_proxy="socks5://127.0.0.1:10808"

but I got Failed to connect to 127.0.0.1 port 10808 after 0 ms: Connection refused.

how can I do that?


Solution

  • ⭐️ If the above-mentioned solution is applied, it will require v2rayN to open LAN connection, and changes in Windows IPv4 Address may lead to proxy failure. Here is my effective solution:

    WSL Proxy Configuration 2024 New Solution (using v2rayN as an example) - Simple and Effective

    Introduction

    In the China region, using curl/wget raw.github... or git commands in WSL2 often fails due to slow internet connectivity. Therefore, network configuration is necessary while setting up the environment. However, WSL2, implemented via a virtual machine, does not share internet with Windows like WSL1, making it difficult to use the host machine’s network proxy.

    Common online tutorials include adjusting the DNS server address or using a LAN to implement the proxy, such as:

    However, these solutions are challenging to implement, and slight mishaps (like incorrect Windows Firewall settings) can cause all efforts to fail. Moreover, with recent updates in WSL, setting proxy environment variables might result in the message: WSL: A localhost proxy configuration was detected but not mirrored into WSL. WSL in NAT mode does not support localhost proxies. Thus, proxy settings fail. WSL: localhost configuration detected but not mirrored to WSL. NAT mode WSL does not support localhost.#10753

    I proposes a new solution for configuring network-related proxy in WSL using network mirroring, greatly simplifying the cumbersome setup process, and provides an example configuration of v2rayN without the need to modify DNS, disable firewall settings, or enable LAN sharing, making it a safe operation.

    System Requirements

    • Windows 11 23H2
    • WSL 2.2.4.0
    • Ubuntu 22.04

    Configuration Process

    WSL Installation

    Windows Subsystem for Linux (WSL) Installation Steps

    After installation, run WSL to test if it starts normally. Note that the version installed from the Microsoft Store is Ubuntu 22.04!

    Configuration

    Mirrored Mode Networking Explanation

    • On computers running Windows 11 22H2 or higher, you can set networkingMode=mirrored in the .wslconfig file under [wsl2] to enable Mirrored Mode Networking. This feature changes WSL to a new network architecture which aims to “mirror” Windows network interfaces into Linux, adding new network functionalities and enhancing compatibility.

    Create a .wslconfig file under C:\Users\your_user_name\, with content:

    [experimental]
    autoMemoryReclaim=gradual  
    networkingMode=mirrored
    dnsTunneling=true
    firewall=true
    autoProxy=false
    

    networkingMode=mirrored to enable mirrored mode. autoProxy=false to disable auto proxy and prevent v2rayN from switching proxy modes (like clear <-> set system proxy) which prompts WSL to restart with An Http proxy change has been detected on the host...

    Restart WSL from Command Prompt:

    for bash

    Re-enter WSL, edit vim ~/.bashrc, and add:

    # proxy setting
    alias proxy='
        export HTTPS_PROXY="http://127.0.0.1:10809";
        export HTTP_PROXY="http://127.0.0.1:10809";
        export ALL_PROXY="socks5://127.0.0.1:10808";
        git config --global http.proxy "socks5://127.0.0.1:10808";
        git config --global https.proxy "socks5://127.0.0.1:10808";
    '
    alias noproxy='
        unset HTTPS_PROXY;
        unset HTTP_PROXY;
        unset ALL_PROXY;
        git config --global --unset  http.proxy;
        git config --global --unset  https.proxy;
    '
    

    If you do not want to set a git proxy, simply remove the git-related parts. Port numbers can be obtained from v2rayN (software interface at the lower-left corner):

    v2rayN interface pic

    To activate the settings, run source ~/.bashrc.

    Before starting the proxy each time, type proxy. To stop using it, type noproxy.

    for zsh

    If using zsh, perform similar operations, edit vim ~./zshrc and add:

    # proxy setting
    proxy () {
      export HTTPS_PROXY="http://127.0.0.1:10809";
      export HTTP_PROXY="http://127.0.0.1:10809";
      export ALL_PROXY="socks5://127.0.0.1:10808";
      git config --global http.proxy "socks5://127.0.0.1:10808";
      git config --global https.proxy "socks5://127.0.0.1:10808";
      echo "Proxy on"
    }
    noproxy () {
      unset HTTPS_PROXY;
      unset HTTP_PROXY;
      unset ALL_PROXY;
      git config --global --unset  http.proxy;
      git config --global --unset  https.proxy;
      echo "HTTP Proxy off"
    }
    

    Testing

    wget www.google.com