大陆服务器或校园网服务器的依赖安装及网络加速指南

admin 2025-12-22 04:22:59 网络安全文章 来源:ZONE.CI 全球网 0 阅读模式

文章总结: 本文介绍了解决中国大陆云服务器或校园网服务器无法直接访问外网资源问题的方法,通过SSH反向端口转发将本地代理转发到远程服务器,实现网络加速。主要步骤包括使用ssh-R命令建立隧道,设置环境变量,配置Docker代理,并提供了完整的自动化脚本。该方法可有效解决apt/pip/npm/dockerpull等依赖安装超时问题,适用于需要稳定外网访问的服务器环境。 综合评分: 85 文章分类: 网络安全,云安全,内网渗透,安全工具,实战经验


cover_image

大陆服务器或校园网服务器的依赖安装及网络加速指南

原创

喜吾安璇

攻防SRC

2025年11月13日 14:57 陕西

由于许多中国大陆云服务器或校园网服务器无法直接访问外网资源,导致环境搭建(如 apt/pip/npm/go/modules/docker pull)经常超时。除了依赖 xget 等加速工具外,还可以通过“本地代理转发”让服务器共享你的本地代理网络,使其具备稳定的外网访问能力。本章节以一台北京地区的服务器为例,演示如何将本机的代理安全、高效地转发至服务器,以解决环境构建过程中的网络受限问题。

未加速前

设置后的效果

方法和原理

把本地机器(带 Clash 代理)上的端口,通过 SSH “反向洞穿”到云服务器,让云服务器也能使用你本地的代理。

  • SSH 反向端口转发(Reverse Port Forwarding)
  • ssh -R 机制 一种“从外网穿回内网”的隧道代理就是本地能畅游互联网 → 云服务器不能,SSH -R 的作用就是:「让云服务器借你的代理上外网」。实现云服务器科学上网与外网访问的最安全、通用的方法。
ssh -R 7890:127.0.0.1:7890 ubuntu@your_server

让远程服务器的 7890 端口
连接到本地机器的 127.0.0.1:7890

例子:ssh -R 7890:127.0.0.1:7890 [email protected]

注意:登陆成功之后需要运行以下命令让本地代理在云服务器生效

export http_proxy=http://127.0.0.1:7890 export https_proxy=http://127.0.0.1:7890

在服务器上运行此脚本可正常使用docker pull

setup-docker-proxy.sh
#!/bin/bash

# ==============================
# Docker & Shell 代理自动配置脚本
# ==============================

PROXY_HOST="127.0.0.1"
PROXY_PORT="7890"
PROXY_URL="http://${PROXY_HOST}:${PROXY_PORT}"

echo "==> 设置当前 Shell 的代理环境变量..."
export http_proxy=$PROXY_URL
export https_proxy=$PROXY_URL
echo "HTTP Proxy: $http_proxy"
echo "HTTPS Proxy: $https_proxy"

echo "==> 创建 Docker systemd 代理配置目录..."
sudo mkdir -p /etc/systemd/system/docker.service.d

echo "==> 写入 Docker systemd 代理配置文件..."
sudo bash -c&nbsp;"cat > /etc/systemd/system/docker.service.d/http-proxy.conf <<EOF
[Service]
Environment=\"HTTP_PROXY=${PROXY_URL}\"
Environment=\"HTTPS_PROXY=${PROXY_URL}\"
Environment=\"NO_PROXY=localhost,127.0.0.1\"
EOF"

echo&nbsp;"==> 重新加载 systemd 配置..."
sudo systemctl daemon-reload

echo&nbsp;'==> 重启 Docker...'
sudo systemctl restart docker

echo&nbsp;"==> 检查 Docker 是否加载代理配置:"
systemctl show docker | grep -i proxy ||&nbsp;echo&nbsp;"未检测到 Proxy 配置"

echo&nbsp;"==> 用 curl 测试代理连通性(若输出你代理出口 IP 则成功):"
curl cip.cc ||&nbsp;echo&nbsp;"curl 测试失败,请检查代理或 SSH 转发设置。"

echo&nbsp;"==> 完成。"

运行日志

ubuntu@orz:~$&nbsp;export&nbsp;http_proxy=http://127.0.0.1:7890&nbsp;export&nbsp;https_proxy=http://127.0.0.1:7890
ubuntu@orz:~$ curl cip.cc
IP : 38.181.72.251
地址 : 中国 香港 九龙半岛

数据二 : 中国香港九龙半岛 | Cogent

数据三 : 美国 | 科进

URL : http://www.cip.cc/38.181.72.251
ubuntu@orz:~$ git&nbsp;clone&nbsp;https://github.com/ctfd/ctfd
Cloning into&nbsp;'ctfd'...
remote: Enumerating objects: 20158,&nbsp;done.
remote: Counting objects: 100% (66/66),&nbsp;done.
remote: Compressing objects: 100% (50/50),&nbsp;done.
remote: Total 20158 (delta 36), reused 16 (delta 16), pack-reused 20092 (from 3)
Receiving objects: 100% (20158/20158), 43.82 MiB | 4.26 MiB/s,&nbsp;done.
Resolving deltas: 100% (11827/11827),&nbsp;done.
ubuntu@orz:~$ cat /etc/systemd/system/docker.service.d/http-proxy.conf
[Service]
Environment="HTTP_PROXY=http://127.0.0.1:7890"
Environment="HTTPS_PROXY=http://127.0.0.1:7890"
Environment="NO_PROXY=localhost,127.0.0.1"

ubuntu@orz:~$ curl https://registry-1.docker.io/v2/ -I
HTTP/1.1 200 Connection established

HTTP/2 401
date: Thu, 13 Nov 2025 01:36:29 GMT
content-type: application/json
content-length: 87
docker-distribution-api-version: registry/2.0
www-authenticate: Bearer realm="https://auth.docker.io/token",service="registry.docker.io"
strict-transport-security: max-age=31536000

ubuntu@orz:~$ sudo docker pull ctfd/ctfd
Using default tag: latest
latest: Pulling from ctfd/ctfd
1adabd6b0d6b: Pull complete
93300bbaa9a6: Pull complete
dc5c0390855f: Pull complete
01f590d3d125: Pull complete
f086496fd65e: Pull complete
4f4fb700ef54: Pull complete
d8dd73a6544b: Pull complete
11c5601cba82: Pull complete
4cc9e06559a1: Pull complete
Digest: sha256:0de331947204628900fb68e01478dfd6f209aadf4eac269a2be08ee92af8ef0d
Status: Downloaded newer image&nbsp;for&nbsp;ctfd/ctfd:latest
docker.io/ctfd/ctfd:latest

查看原文:《大陆服务器或校园网服务器的依赖安装及网络加速指南》

评论:0   参与:  2