How to Set Up V2Ray Proxy on Ubuntu 22.04

Estimated reading: 8 minutes 698 views

This tutorial is going to show you how to set up a V2Ray proxy server on Ubuntu 22.04. V2Ray is a lightweight, fast, and secure SOCKS5 proxy to bypass Internet censorship. We will learn how to set up the server side and how to configure the desktop client on Ubuntu.

V2Ray Features

  • Lightweight and fast. In my test, I can watch YouTube 4K videos with V2Ray. YouTube is blocked in my country (China).
  • Runs on Linux and most BSD servers.
  • There is an official V2Ray client software for Linux, macOS, Windows, and BSD. For Android and iOS, third-party apps are available.
  • Easy to set up for system administrators
  • V2Ray can be configured to operate on TCP port 443 and uses the standard TLS protocol to encrypt network traffic. It appears to be a standard HTTPS protocol, which makes it difficult to block.
  • Supports the KCP transport protocol, which is useful in network environments with high packet loss.
  • Routing support. You can configure it to only route traffic for websites/domains that are blocked in your country or area.
  • You can run it behind Cloudflare CDN.
  • V2Ray is more than a simple proxy tool. It’s designed as a platform that developers can use to build new protocols and tools.

Requirements

To follow this tutorial, you will need a VPS (Virtual Private Server) that can access blocked websites freely (Outside of your country or Internet filtering system). I recommend Greencloud VPS.

Step 1: Install V2Ray on Ubuntu 22.04 Server

SSH into your remote Ubuntu server. If you are running Ubuntu 22.04/20.04, then I recommend manually installing V2Ray, because the v2ray package in the repository has a problem when starting it up. Run the following command to install dependency packages.

apt-get update
apt install curl unzip

Download the official V2Ray install script. (I don’t usually recommend installing software with third-party scripts, but this is the install script provided by official V2Ray developers, so I use it.)

curl -O https://raw.githubusercontent.com/v2fly/fhs-install-v2ray/master/install-release.sh

Run the install script.

bash install-release.sh

Sample output:

If it’s not running, then restart it.

systemctl restart v2ray

Enable auto-start at system boot time.

systemctl enable v2ray

Check status:

systemctl status v2ray

Step 2: Set Up NTP Time Sync

It’s very important that your server has accurate time, or V2Ray can fail. This is to ensure the best security for users. Check the time on your server.

timedatectl

Sample output:

As you can see, my server system clock is synchronized.

Step 3: Configure V2Ray on the Server

Edit the V2Ray configuration file with a command-line text editor such as Nano.

nano /usr/local/etc/v2ray/config.json

Delete everything in this file, then add the following lines. Replace the ID with some random characters in UUID format. You can use an online UUID generator.

{
  "log": {
    "loglevel": "warning",
    "access": "/var/log/v2ray/access.log",
    "error": "/var/log/v2ray/error.log"
  },
  "inbounds": [
    {
      "port": 10000,
      "listen":"127.0.0.1",
      "protocol": "vmess",
      "settings": {
        "clients": [
          {
            "id": "840da3a8-831b-11f0-8de9-0242ac120002",
            "alterId": 64
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "wsSettings": {
        "path": "/ray"
        }
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "freedom",
      "settings": {}
    }
  ]
}

Save and close the file. V2Ray server will listen on port 10000. Then restart V2Ray for the changes to take effect.

systemctl restart v2ray

Check listening ports.

ss -lnpt | grep v2ray

Sample output:

Step 4: Configure Reverse Proxy

Install Nginx web server.

apt install nginx

Create a virtual host file for V2Ray.

nano /etc/nginx/conf.d/v2ray.conf

Add the following lines to this file. Replace example.com with your own domain name. You can also use a subdomain.

server {
  listen 80;
  server_name    example.com;

  index index.html;
  root /usr/share/nginx/html/;

  access_log /var/log/nginx/v2ray.access;
  error_log /var/log/nginx/v2ray.error;

    location /ray { # Consistent with the path of V2Ray configuration
      if ($http_upgrade != "websocket") { # Return 404 error when WebSocket upgrading negotiate failed
          return 404;
      }
      proxy_redirect off;
      proxy_pass http://127.0.0.1:10000; # Assume WebSocket is listening at localhost on port of 10000
      proxy_http_version 1.1;
      proxy_set_header Upgrade $http_upgrade;
      proxy_set_header Connection "upgrade";
      proxy_set_header Host $host;
      # Show real IP in v2ray access.log
      proxy_set_header X-Real-IP $remote_addr;
      proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
    }
}

Save and close the file.  Then test the Nginx configuration.

nginx -t

If the test is successful, reload Nginx.

systemctl reload nginx

Step 5: Enable HTTPS

We enable HTTPS so that your national firewall doesn’t know you are using a proxy.

Install the latest version of the Let’s Encrypt client certbot from the Snap store.

apt install snapd
snap install core
snap refresh core
snap install --classic certbot
ln -s /snap/bin/certbot /usr/bin/certbot

Next, download the Let’s Encrypt SSL for your domain.

certbot certonly --nginx --agree-tos --no-eff-email --staple-ocsp --preferred-challenges http -m your_email -d Your_domain
certbot --nginx -d Your_domain

Next, generate the dhparam using the following command.

openssl dhparam -dsaparam -out /etc/ssl/certs/dhparam.pem 4096

Step 6: Configure Firewall

If you are using an iptables firewall on your server, then you need to allow traffic to TCP port 443 by running the following command.

iptables -I INPUT -p tcp --dport 443 -j ACCEPT

If you are using the UFW firewall, then run the following commands:

ufw allow 443/tcp

Step 7: Install and Configure V2Ray on Client Computer

In this case, we will install and configure it on Windows.

Windows

Windows users can download the V2Ray client from its GitHub release page. Click the Show all assets link, then you can find the V2Ray Windows ZIP file.

Unzip the file. You will find a config.json file. Use NotePad or your favorite text editor to open this file. Delete everything in this file, then add the following lines.

{
  "inbounds": [
    {
      "port": 1090,
      "listen": "127.0.0.1",
      "protocol": "socks",
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls"]
      },
      "settings": {
        "auth": "noauth",
        "udp": false
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
            "address": "example.com",
            "port": 443,
            "users": [
              {
                "id": "840da3a8-831b-11f0-8de9-0242ac120002",
                "alterId": 0
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "security": "tls",
        "wsSettings": {
          "path": "/ray"
        }
      }
    }
  ]
}

Replace server_ip_address with your actual server IP address. The id parameter on the V2Ray client must match the id parameter on the V2Ray server. It’s like a pre-shared password.

Save and close the file. Then open Windows PowerShell and run the following command to start V2Ray, assuming the v2ray-windows-64 folder is extracted to your Downloads folder.

cd C:\Users\Administrator\Downloads\v2ray-windows-64
.\v2ray.exe run

Linux Desktop

Please follow the same procedure in step 1 to install V2Ray on a Linux desktop. Once it’s done, edit the configuration file.

nano /usr/local/etc/v2ray/config.json

Delete everything in this file, then add the following lines.

{
  "inbounds": [
    {
      "port": 1090,
      "listen": "127.0.0.1",
      "protocol": "socks",
      "sniffing": {
        "enabled": true,
        "destOverride": ["http", "tls"]
      },
      "settings": {
        "auth": "noauth",
        "udp": false
      }
    }
  ],
  "outbounds": [
    {
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
            "address": "example.com",
            "port": 443,
            "users": [
              {
                "id": "840da3a8-831b-11f0-8de9-0242ac120002",
                "alterId": 0
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "ws",
        "security": "tls",
        "wsSettings": {
          "path": "/ray"
        }
      }
    }
  ]
}

The id The parameter on the V2Ray client must match the id parameter on the V2Ray server. It’s like a pre-shared password. The alterId must be set to 0, so the V2Ray client will enable AEAD encryption algorightm.

Save and close the file.  Then restart V2Ray for the changes to take effect.

sudo systemctl restart v2ray

Check listening ports.

sudo ss -lnpt | grep v2ray

Sample output:

LISTEN 0      4096       127.0.0.1:1090       0.0.0.0:*    users:(("v2ray",pid=495211,fd=3))

V2Ray client listens on port 1090 (for web browsers) and will redirect requests to the V2Ray server, which is listening on port 10086.

Step 8: Enable TCP BBR

TCP BBR is a TCP congestion control algorithm that can drastically improve connection speed. Run the following two commands to enable the TCP BBR algorithm.

echo "net.core.default_qdisc=fq" | sudo tee -a /etc/sysctl.d/60-custom.conf

echo "net.ipv4.tcp_congestion_control=bbr" | sudo tee -a /etc/sysctl.d/60-custom.conf

Then apply the changes with the following command. The -p option will load sysctl settings from the /etc/sysctl.d/60-custom.conf file. This command will preserve our changes across system reboots.

sysctl -p /etc/sysctl.d/60-custom.conf

Troubleshooting

If V2Ray doesn’t work, please check the logs under /var/log/v2ray/ (access.log and error.log).

If you see the following error, it’s likely because the client’s time is wrong. Please configure time sync.

rejected proxy/vmess/encoding: failed to read request header > websocket: close 1000 (normal)

If you encounter the following error, please set alterId it to 0 in the V2Ray client configuration file to enable AEAD.

invalid user: VMessAEAD is enforced and a non VMessAEAD connection is received.

How to Upgrade V2Ray

Simply run the install script again.

sudo bash install-release.sh

Wrapping Up

That’s it! I hope this tutorial helped you install V2Ray proxy on Ubuntu. As always, if you found this post useful, then subscribe to our free newsletter to get more tips and tricks 🙂

Share this Doc

How to Set Up V2Ray Proxy on Ubuntu 22.04

Or copy link

CONTENTS