惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

Y
Y Combinator Blog
有赞技术团队
有赞技术团队
J
Java Code Geeks
H
Hackread – Cybersecurity News, Data Breaches, AI and More
美团技术团队
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
Hugging Face - Blog
Hugging Face - Blog
人人都是产品经理
人人都是产品经理
酷 壳 – CoolShell
酷 壳 – CoolShell
钛媒体:引领未来商业与生活新知
钛媒体:引领未来商业与生活新知
C
Check Point Blog
博客园 - 【当耐特】
The GitHub Blog
The GitHub Blog
Recent Announcements
Recent Announcements
The Cloudflare Blog
Microsoft Azure Blog
Microsoft Azure Blog
腾讯CDC
Vercel News
Vercel News
IT之家
IT之家
MyScale Blog
MyScale Blog
博客园_首页
Martin Fowler
Martin Fowler
WordPress大学
WordPress大学
罗磊的独立博客

Jun's Blog

2023 年度总结 OS 学习记 之 XV6 如何编译 GraalVM LLVM 中端优化之 InstCombine C++ 中 inline 关键字的语义 链接与库 C++ 移动语义基础 如何优化矩阵相乘 2022年度总结 CUDA初学笔记 汇编语言之保护模式 汇编语言之实模式 浅析 libc++ 中的 string 实现 std::expected 基本使用 C++模板基础 CSAPP第九章笔记之虚拟内存 CSAPP第八章笔记之异常控制流 2021年度总结 编译安装GCC12 如何给LLVM贡献代码 CSAPP第三章笔记Part 2 CSAPP第三章笔记Part 1 GDB基本使用笔记 读Effetive Modern C++ 之类型推导 给计算机新生的一封信 CMake学习笔记 由Redis学习数据结构--字典 C++中lambda表达式基础 由Redis学习数据结构--链表 对Python及爬虫行业的思考
Linux下使用v2ray
Jun · 2021-02-28 · via Jun's Blog

· Jun

v2ray是一个强大的代理工具,但苦于Linux下一直没有一个好用的客户端,便萌生了直接使用裸v2ray的念头.v2ray本身是不区分服务端和客户端的,只要配置好相关文件,即可正常使用.

安装v2ray

下载v2ray core

1
https://github.com/v2ray/v2ray-core/releases/

解压:

1
unzip v2ray-linux-64.zip -d v2ray-linux-64

解压后使用mv将相应的文件放置到对应的路径

1
2
3
4
5
6
7
8
9
v2ray -> /usr/local/bin/v2ray
v2ctl -> /usr/local/bin/v2ctl
geoip.dat -> /usr/local/share/v2ray/geoip.dat
geosite.dat -> /usr/local/share/v2ray/geosite.dat
config.json -> /usr/local/etc/v2ray/config.json
access.log -> /var/log/v2ray/access.log
error.log -> /var/log/v2ray/error.log
v2ray.service -> /etc/systemd/system/v2ray.service
v2ray@.service -> /etc/systemd/system/v2ray@.service
  • 日志文件要保证所有人都有读写权限

  • 要在配置文件中指定日志路径

配置文件

注意原生的V2ray并不支持订阅。

以下配置文件仅为参考,你可以将其它客户端中的配置文件完全导出,然后直接替换/usr/local/etc/v2ray/config.json

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
{
  "dns": {
    "hosts": {
      "domain:googleapis.cn": "googleapis.com"
    },
    "servers": [
      "1.1.1.1"
    ]
  },
  "inbounds": [
    {
      "listen": "127.0.0.1",
      "port": 10808,
      "protocol": "socks",
      "settings": {
        "auth": "noauth",
        "udp": true,
        "userLevel": 8
      },
      "sniffing": {
        "destOverride": [
          "http",
          "tls"
        ],
        "enabled": true
      },
      "tag": "socks"
    },
    {
      "listen": "127.0.0.1",
      "port": 10809,
      "protocol": "http",
      "settings": {
        "userLevel": 8
      },
      "tag": "http"
    }
  ],
  "log": {
    "loglevel": "warning",
    "access":"/var/log/v2ray/access.log",
    "error":"/var/log/v2ray/error.log"
  },
  "outbounds": [
    {
    //此处根据具体设置
      "mux": {
        "concurrency": -1,
        "enabled": false
      },
      "protocol": "vmess",
      "settings": {
        "vnext": [
          {
          //此处根据具体设置
            "address": "example.com",
            "port": 10000,
            "users": [
              {
                "alterId": 0,
                "id": "xxxxxxxxxxxx",
                "level": 8,
                "security": "auto"
              }
            ]
          }
        ]
      },
      "streamSettings": {
        "network": "tcp",
        "security": ""
      },
      "tag": "proxy"
    },
    {
      "protocol": "freedom",
      "settings": {},
      "tag": "direct"
    },
    {
      "protocol": "blackhole",
      "settings": {
        "response": {
          "type": "http"
        }
      },
      "tag": "block"
    }
  ],
  "policy": {
    "levels": {
      "8": {
        "connIdle": 300,
        "downlinkOnly": 1,
        "handshake": 4,
        "uplinkOnly": 1
      }
    },
    "system": {
      "statsOutboundUplink": true,
      "statsOutboundDownlink": true
    }
  },
  "routing": {
    "domainStrategy": "IPIfNonMatch",
    "rules": []
  },
  "stats": {}
}

使用V2ray

1
2
3
4
5
6
7
8
# 启动V2ray
sudo systemctl start v2ray

# 检查V2ray状态
sudo systemctl status v2ray

# 设置V2ray开机自启动
sudo systemctl enable v2ray

检验代理是否成功生效

终端下使用curl,查看它在代理模式下是否能返回数据:

1
curl -x socks5://127.0.0.1:10808 https://www.google.com -v

如果能返回google.com的源代码,即表示配置成功。

如果显示超时或者无法建立连接,即表示配置有错误,具体可以查看日志排查原因。

系统设置

当我们配置好代理后,我们很多情况下并不能开箱即用,而是需要将代理信息写入相关配置文件后,才能使用。具体操作如下:

设置处启用代理,并填入相关端口

打开设置,选择网络->代理->手动 并填入相应端口

此操作后可以使用浏览器测试是否能正常打开网页,如果还是失败的话,考虑修改浏览器自带的代理配置。

将代理设置写入shell profile

虽然我们设置了系统代理,但是终端下并不会走代理,所以还要配置以下设置

将此行写入用户的shell profile 如果使用的是bash,写入~/.bashrc,如果是zsh,写入~/.zshrc 如果不确定,就直接写入~/.bashrc

1
2
3
//端口具体情况具体对待,不清楚打开代理工具看一下.
export ALL_PROXY="socks5://127.0.0.1:10808"
export http_proxy="http://127.0.0.1:10809"

然后我们要让配置文件生效.

如果还是无法走代理可以试试重开一个终端.

使用proxychains

对于某些不会走socks5的应用,我们还可以通过proxychains,使它强制走代理

1
2
3
/etc/proxychains.conf
//编辑配置文件,取消sock4配置,加入这一行
socks5 127.0.0.1 10808

此后,直接在原始命令前加一个proxychains,便可走代理.比如:

1
proxychains wget https://example.com/index.html

sshsocks5代理

1
ssh -o "ProxyCommand=nc -X 5 -x 127.0.0.1:10808 %h %p" user@hostname

其中端口按实际情况修改, 如果想永久保持使用,可以使用alias做一个别名。

注意这里需要先安装nc

1
alias pssh='ssh -o "ProxyCommand=nc -X 5 -x 127.0.0.1:10808 %h %p"'

这里为了区分原始的ssh,我们创建了一个pssh,下次对于需要使用代理的,把ssh换成pssh即可. 将此行写入用户的shell profile 如果使用的是bash,写入~/.bashrc,如果是zsh,写入~/.zshrc 如果不确定,就直接写入~/.bashrc