Files
nexus/raw/Home Office/Ubuntu Server科学上网.md

284 lines
6.5 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title:
source:
author: shenwei
published:
created:
description:
tags: [docker, proxychains, ubuntu, v2rayn]
---
#ubuntu #proxychains #docker #v2rayn
```table-of-contents
```
## 安装V2RayN
请参考以下文章来安装V2RayN
[[🟠3X-UI Xray on BandwagonVPS]]
[[🟠安装v2rayN]]
![[IMG-20251229190624376.png]]
## 验证代理可以科学上网
### 使用 `curl` 直接测试(最推荐)
这是最快、最直接的方法。我们可以强制 `curl` 使用 SOCKS5 代理去访问 Google 的状态页。
**执行命令:**
Bash
```
curl -x socks5h://127.0.0.1:10808 -v https://www.google.com
```
- **参数解释:**
- `-x socks5h://`:指定使用 SOCKS5 代理。注意加个 `h`,这表示让代理服务器去解析域名(防止本地 DNS 污染导致测试失败)。
- `-v`(Verbose) 显示详细连接过程。
- **判断标准:**
- 如果看到 `HTTP/2 200` 或者大量的 HTML 文本,说明**代理成功**。
- 如果显示 `Connection refused` 或 `Timeout`,说明**端口未开放或 V2Ray 未运行**。
## 配置 ProxyChains
ProxyChains 是最灵活的工具,它可以让原本不支持代理的终端命令通过代理运行。
1. **编辑配置文件:**
```
sudo nano /etc/proxychains4.conf
```
(如果是旧版本可能是 `/etc/proxychains.conf`)
2. 修改 ProxyList
滑动到文件末尾,注释掉默认的 socks4添加你的 V2Ray 节点信息:
```
[ProxyList]
# 格式: 类型 IP 端口
socks5 127.0.0.1 10808
```
3. 使用方法:
在任何命令前加上 proxychains4 即可。例如:
```
proxychains4 curl https://www.google.com
``````
使用:
``` bash
proxychains git clone https://github.com/...
proxychains curl https://google.com
```
## 2. 配置 Git 代理
Git 不会自动走系统变量,建议为其设置全局配置。
- **设置 SOCKS5 代理(推荐):**
Bash
```
git config --global http.proxy 'socks5://127.0.0.1:10808'
git config --global https.proxy 'socks5://127.0.0.1:10808'
```
- **取消设置:**
Bash
```
git config --global --unset http.proxy
git config --global --unset https.proxy
```
---
## 3. 配置 Docker Pull (Daemon 代理)
`docker pull` 是由 Docker 守护进程Daemon执行的它不读取普通用户的环境变量。
1. **创建配置目录:**
Bash
```
sudo mkdir -p /etc/systemd/system/docker.service.d
```
2. **创建代理配置文件:**
Bash
```
sudo nano /etc/systemd/system/docker.service.d/http-proxy.conf
```
3. **添加以下内容:**
Ini, TOML
```
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:10808/"
Environment="HTTPS_PROXY=http://127.0.0.1:10808/"
Environment="NO_PROXY=localhost,127.0.0.1,docker-registry.somecorporate.com"
```
_(注:这里通常使用 HTTP 代理端口)_
4. **重启 Docker 服务:**
```
sudo systemctl daemon-reload
sudo systemctl restart docker
```
**检查 Docker 守护进程是否加载了代理:**
**执行命令:**
```
docker info | grep -i proxy
```
- **预期输出:** 如果你配置成功,你应该能看到类似下面的信息:
```
HTTP Proxy: http://127.0.0.1:10808
HTTPS Proxy: http://127.0.0.1:10808
No Proxy: localhost,127.0.0.1
```
_(注:如果这里没有输出,说明 `/etc/systemd/system/docker.service.d/http-proxy.conf` 配置未生效,请记得执行 `systemctl daemon-reload` 和 `systemctl restart docker`)_
---
## 4. 配置 Docker 容器内应用代理
#### docker-compose.yml里面直接加 env
```
`environment:
- ALL_PROXY=socks5://172.24.0.1:10808
```
For example:gi
```
version: "3.9"
services:
homarr:
image: ghcr.io/homarr-labs/homarr
container_name: homarr
restart: unless-stopped
ports:
- "7575:7575"
volumes:
- /home/shenwei/Docker/homarr/appdata:/appdata
- /var/run/docker.sock:/var/run/docker.sock
environment:
- SECRET_ENCRYPTION_KEY=4a418def4be700be26672aa57a4c3d4b94abd2cf97021b5c4ecd3c1644c1f071
- ALL_PROXY=socks5://172.24.0.1:10808
```
2个方法知道如何获取docker network gate IP
1. Docker Portainer
![[IMG-20251229190624729.png]]
2. 获取运行时container的network gateway
适用于容器已经在运行的情况。进入容器的交互式 shell
```
docker exec -it <container_name_or_id> /bin/bash
```
- 如果容器没有 `bash`,可以用 `sh`
```
docker exec -it <container_name_or_id> sh
```
运行以下命令获取network gateway IP:
```
ip route | awk '/default/ { print $3 }'
```
For example:
``` bash
root@shenwei-HP-ZBook-01:/home/shenwei/Docker/homarr# docker exec -it homarr /bin/bash
23c94b2dfeb5:/app# ip route
default via 172.24.0.1 dev eth0
172.24.0.0/16 dev eth0 scope link src 172.24.0.2
23c94b2dfeb5:/app# ip route | awk '/default/ { print $3 }'
172.24.0.1
```
如果你希望容器内部的程序(如 `apt-get``pip`)能上网,有两种方案:
### 方案 A全局配置推荐 Docker 17.07+
修改当前用户的 Docker 客户端配置文件,这样所有 `docker run` 的容器都会自动带上代理环境变量。
1. **编辑配置文件:**
Bash
```
mkdir -p ~/.docker
nano ~/.docker/config.json
```
2. **添加内容:**
JSON
```
{
"proxies": {
"default": {
"httpProxy": "http://127.0.0.1:1081",
"httpsProxy": "http://127.0.0.1:1081",
"noProxy": "localhost,127.0.0.1"
}
}
}
```
**注意:** 如果你的容器使用的是 `bridge` 网络,`127.0.0.1` 指向的是容器内部。你需要将 IP 改为宿主机的虚拟网桥 IP通常是 `172.17.0.1`)。
### 方案 B运行时临时指定
在启动容器时通过 `-e` 参数注入环境变量:
Bash
```
docker run -e HTTP_PROXY="http://宿主机IP:1081" -e HTTPS_PROXY="http://宿主机IP:1081" my_image
```
---
## 总结建议
|**场景**|**推荐方式**|
|---|---|
|**临时终端命令**|`proxychains4 <command>`|
|**Git 操作**|`git config --global`|
|**下载 Docker 镜像**|修改 `systemd/system/docker.service.d`|
|**容器内部业务**|修改 `~/.docker/config.json`|
如果你在配置过程中遇到“连接被拒绝 (Connection Refused)”的问题,请检查 V2Ray 配置文件中是否开启了 HTTP 代理协议,并确认端口号是否正确。