在网络环境受限的情况下,我们经常需要在服务器上配置代理来访问外部资源。Sing-box 是一个功能强大的通用代理工具,支持多种协议。本文将详细介绍如何在 Ubuntu 24.04 上配置 Sing-box 作为代理服务器,并让 Docker 等应用使用该代理。

1、什么是 Sing-box?

Sing-box 是一个通用的代理工具,支持多种协议如 VMess、Trojan、Shadowsocks、WireGuard 等。它可以在服务器上运行,为本地应用提供 SOCKS5 或 HTTP 代理服务。

2、准备工作

在开始之前,请确保:

  • 已获取到可用的代理服务器配置信息
  • 拥有服务器的 root 权限或 sudo 权限
  • 服务器能够访问 GitHub(用于下载 Sing-box)

3. 安装 Sing-box

3.1 下载并安装

推荐使用官方预编译版本,这是最简单的方式:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
# 创建临时目录
mkdir -p /tmp/sing-box && cd /tmp/sing-box

# 下载最新版本(以 Linux AMD64 为例)
wget https://github.com/SagerNet/sing-box/releases/latest/download/sing-box-linux-amd64.tar.gz

# 解压
tar -xvf sing-box-linux-amd64.tar.gz
cd sing-box-*

# 创建配置目录
sudo mkdir -p /etc/sing-box

# 拷贝可执行文件到系统路径
sudo cp sing-box /usr/local/bin/
sudo chmod +x /usr/local/bin/sing-box

# 验证安装
sing-box version

3.2 验证安装

安装完成后,运行以下命令验证:

1
sing-box version

如果显示版本信息,说明安装成功。

4. 配置 Sing-box

4.1 创建配置文件

将你的代理配置保存到 /etc/sing-box/config.json

1
sudo vim /etc/sing-box/config.json

4.2 配置文件示例

以下是一个基本的配置文件示例,请根据你的实际代理服务器信息进行修改:

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
{
"log": {
"level": "info",
"timestamp": true
},
"inbounds": [
{
"type": "socks",
"tag": "socks-in",
"listen": "127.0.0.1",
"listen_port": 10808,
"sniff": true
},
{
"type": "http",
"tag": "http-in",
"listen": "127.0.0.1",
"listen_port": 10809,
"sniff": true
}
],
"outbounds": [
{
"type": "vmess",
"tag": "vmess-out",
"server": "your-server-ip",
"server_port": 443,
"uuid": "your-uuid-here",
"security": "auto",
"tls": {
"enabled": true,
"server_name": "your-domain.com"
}
},
{
"type": "direct",
"tag": "direct"
}
],
"route": {
"rules": [
{
"geoip": "private",
"outbound": "direct"
}
]
}
}

重要提示:

  • your-server-ip 替换为你的代理服务器 IP
  • your-uuid-here 替换为你的 UUID
  • your-domain.com 替换为你的域名(如果使用 TLS)
  • 根据你的代理协议类型调整 outbounds 配置

4.3 验证配置文件

在启动服务之前,先验证配置文件是否正确:

1
sing-box check -c /etc/sing-box/config.json

如果显示 “config check passed”,说明配置正确。

5. 配置系统服务

5.1 创建 systemd 服务文件

创建服务文件以管理 Sing-box:

1
sudo vim /etc/systemd/system/sing-box.service

添加以下内容:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
[Unit]
Description=Sing-box Proxy Service
Documentation=https://sing-box.sagernet.org/
After=network-online.target
Wants=network-online.target

[Service]
Type=simple
User=root
Group=root
ExecStart=/usr/local/bin/sing-box run -c /etc/sing-box/config.json
ExecReload=/bin/kill -HUP $MAINPID
Restart=on-failure
RestartSec=10
LimitNOFILE=infinity

[Install]
WantedBy=multi-user.target

5.2 启动并启用服务

1
2
3
4
5
6
7
8
9
10
11
# 重新加载 systemd 配置
sudo systemctl daemon-reload

# 启用服务(开机自启)
sudo systemctl enable sing-box

# 启动服务
sudo systemctl start sing-box

# 检查服务状态
sudo systemctl status sing-box

3.3 查看服务日志

如果服务启动失败,可以查看日志:

1
sudo journalctl -u sing-box -f

6. 测试代理连接

6.1 设置终端代理

临时设置终端代理环境变量:

1
2
3
4
5
6
# 设置 HTTP 代理
export http_proxy=http://127.0.0.1:10809
export https_proxy=http://127.0.0.1:10809

# 设置 SOCKS5 代理
export socks_proxy=socks5://127.0.0.1:10808

6.2 测试连接

测试是否能通过代理访问外网:

1
2
3
4
5
6
7
8
# 使用 HTTP 代理测试
curl -x http://127.0.0.1:10809 https://www.google.com

# 使用 SOCKS5 代理测试
curl -x socks5h://127.0.0.1:10808 https://www.google.com

# 测试 IP 地址
curl -x http://127.0.0.1:10809 https://ipinfo.io

6.3 永久设置代理(可选)

如果希望永久设置代理,可以添加到 shell 配置文件中:

1
2
3
4
5
6
7
# 对于 bash
echo 'export http_proxy=http://127.0.0.1:10809' >> ~/.bashrc
echo 'export https_proxy=http://127.0.0.1:10809' >> ~/.bashrc

# 对于 zsh
echo 'export http_proxy=http://127.0.0.1:10809' >> ~/.zshrc
echo 'export https_proxy=http://127.0.0.1:10809' >> ~/.zshrc

7. 配置 Docker 使用代理

7.1 创建 Docker 代理配置

1
2
3
4
5
# 创建配置目录
sudo mkdir -p /etc/systemd/system/docker.service.d

# 创建代理配置文件
sudo vim /etc/systemd/system/docker.service.d/proxy.conf

添加以下内容:

1
2
3
4
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:10809/"
Environment="HTTPS_PROXY=http://127.0.0.1:10809/"
Environment="NO_PROXY=localhost,127.0.0.1,::1"

7.2 重启 Docker 服务

1
2
3
4
5
6
7
8
# 重新加载 systemd 配置
sudo systemctl daemon-reload

# 重启 Docker
sudo systemctl restart docker

# 验证 Docker 状态
sudo systemctl status docker

5.3 测试 Docker 代理

1
2
3
4
5
# 测试拉取镜像
docker pull hello-world

# 查看 Docker 信息
docker info

8. 故障排除

8.1 常见问题

问题 1:Sing-box 启动失败

1
2
3
4
5
# 检查配置文件语法
sing-box check -c /etc/sing-box/config.json

# 查看详细日志
sudo journalctl -u sing-box -n 50

问题 2:代理连接失败

1
2
3
4
5
6
# 检查端口是否监听
sudo netstat -tlnp | grep sing-box

# 测试本地连接
telnet 127.0.0.1 10808
telnet 127.0.0.1 10809

问题 3:Docker 无法拉取镜像

1
2
3
4
5
# 检查 Docker 代理配置
sudo systemctl show docker --property=Environment

# 重启 Docker 服务
sudo systemctl restart docker

8.2 安全建议

  1. 防火墙配置:确保只允许本地访问代理端口
  2. 定期更新:保持 Sing-box 版本最新
  3. 日志监控:定期检查服务日志
  4. 备份配置:备份重要的配置文件

9. 管理命令

9.1 服务管理

1
2
3
4
5
6
7
8
9
10
11
12
13
14
# 启动服务
sudo systemctl start sing-box

# 停止服务
sudo systemctl stop sing-box

# 重启服务
sudo systemctl restart sing-box

# 查看状态
sudo systemctl status sing-box

# 查看日志
sudo journalctl -u sing-box -f

9.2 配置管理

1
2
3
4
5
6
7
8
# 重新加载配置(需要重启服务)
sudo systemctl restart sing-box

# 备份配置
sudo cp /etc/sing-box/config.json /etc/sing-box/config.json.backup

# 恢复配置
sudo cp /etc/sing-box/config.json.backup /etc/sing-box/config.json

总结

通过以上步骤,你已经成功在 Ubuntu 24.04 上配置了 Sing-box 代理服务器。主要完成的工作包括:

  1. ✅ 安装并配置 Sing-box
  2. ✅ 创建 systemd 服务实现开机自启
  3. ✅ 配置本地 SOCKS5 和 HTTP 代理
  4. ✅ 设置 Docker 使用代理
  5. ✅ 测试代理连接

现在你的服务器可以通过 Sing-box 代理访问外部资源,Docker 也能正常拉取镜像了。

参考链接