主题
SSH SOCKS5 代理配置文档
创建时间: 2026-08-22
最后更新: 2026-08-22
代理服务器: 8.153.84.140 (root@22端口)
本地监听: 127.0.0.1:1080
📋 目录
架构概述
网络拓扑
┌─────────────┐ SSH Tunnel ┌──────────────────┐
│ │ SOCKS5 Proxy (1080) │ │
│ 本机 │ ◄──────────────────────────► │ 8.153.84.140 │
│ (xxx) │ 127.0.0.1:1080 │ (root) │
│ │ │ │
└─────────────┘ └──────────────────┘
│
│ HTTPS/TCP
▼
┌─────────────┐
│ GitHub │
│ Internet │
└─────────────┘方案选型
最终方案: SSH SOCKS5 动态端口转发(Dynamic Port Forwarding)
优势:
- ✅ 无需在远程服务器安装额外软件
- ✅ 利用已有的 SSH 免密登录
- ✅ 所有流量加密传输
- ✅ 不需要在云安全组开放额外端口(使用 22 端口)
- ✅ 支持 TCP 和 UDP 协议
- ✅ 自动断线重连(autossh)
对比方案:
Dante SOCKS5 Server: 需要编译安装、配置防火墙、开放 1080 端口Squid Proxy: 仅支持 HTTP/HTTPS,不支持 SOCKS5SSH 静态端口转发: 需要为每个目标端口单独配置
客户端配置
依赖条件
SSH 免密登录已配置
- 本机用户:
xxx - 远程用户:
root - 远程服务器:
8.153.84.140 - SSH 端口:
22 - 密钥文件:
~/.ssh/id_rsa
- 本机用户:
本地软件依赖
bashautossh # 自动重连 SSH 隧道 curl # 测试代理 git # 版本控制(需要代理)
配置文件清单
| 文件路径 | 用途 | 创建时间 |
|---|---|---|
/etc/systemd/system/ssh-socks5-tunnel.service | SSH 隧道 systemd 服务 | 2026-08-22 |
/etc/profile.d/socks5_proxy.sh | 全局代理环境变量 | 2026-08-22 |
~/.gitconfig | Git 代理配置 | 2026-08-22 |
1. SSH 隧道 Systemd 服务
文件: /etc/systemd/system/ssh-socks5-tunnel.service
ini
[Unit]
Description=SSH SOCKS5 Tunnel to 8.153.84.140
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=xxx
ExecStart=/usr/bin/autossh -M 0 -o "ServerAliveInterval 30" -o "ServerAliveCountMax 3" -o "StrictHostKeyChecking no" -p 22 -N -D 1080 root@8.153.84.140
Restart=always
RestartSec=10
Environment="AUTOSSH_GATETIME=0"
[Install]
WantedBy=multi-user.target配置说明:
-M 0: 禁用 autossh 监控端口(依赖 SSH 自身的 ServerAlive)-o "ServerAliveInterval 30": 每 30 秒发送心跳包-o "ServerAliveCountMax 3": 3 次心跳无响应则断开-N: 不执行远程命令,仅做端口转发-D 1080: 在本地 1080 端口创建 SOCKS5 代理AUTOSSH_GATETIME=0: 禁用初始连接超时检查
启用服务:
bash
sudo systemctl daemon-reload
sudo systemctl enable ssh-socks5-tunnel
sudo systemctl start ssh-socks5-tunnel2. 全局代理环境变量
文件: /etc/profile.d/socks5_proxy.sh
bash
# SOCKS5 Proxy via SSH tunnel to 8.153.84.140
export http_proxy="socks5h://127.0.0.1:1080"
export https_proxy="socks5h://127.0.0.1:1080"
export HTTP_PROXY="socks5h://127.0.0.1:1080"
export HTTPS_PROXY="socks5h://127.0.0.1:1080"
export no_proxy="localhost,127.0.0.1,10.*,172.*,192.168.*,*.local"
export NO_PROXY="localhost,127.0.0.1,10.*,172.*,192.168.*,*.local"配置说明:
socks5h://: 使用 SOCKS5 协议,远程 DNS 解析(推荐)no_proxy: 排除内网地址,避免代理内部流量- 支持的模式:
localhost,127.0.0.1,10.*,172.*,192.168.*,*.local
生效方式:
- 新终端: 自动生效
- 当前终端:
source /etc/profile.d/socks5_proxy.sh
3. Git 代理配置
文件: ~/.gitconfig
ini
[http]
proxy = socks5h://127.0.0.1:1080
[https]
proxy = socks5h://127.0.0.1:1080配置命令:
bash
git config --global http.proxy socks5h://127.0.0.1:1080
git config --global https.proxy socks5h://127.0.0.1:1080验证配置:
bash
git config --global --get http.proxy
git config --global --get https.proxy
---
## 管理命令
### 服务管理
```bash
# 查看服务状态
sudo systemctl status ssh-socks5-tunnel
# 启动服务
sudo systemctl start ssh-socks5-tunnel
# 停止服务
sudo systemctl stop ssh-socks5-tunnel
# 重启服务
sudo systemctl restart ssh-socks5-tunnel
# 查看服务日志
sudo journalctl -u ssh-socks5-tunnel -f
# 查看最近 50 行日志
sudo journalctl -u ssh-socks5-tunnel -n 50进程管理
bash
# 查看 SSH 隧道进程
ps aux | grep 'ssh.*-D.*1080' | grep -v grep
# 查看 autossh 进程
ps aux | grep autossh | grep -v grep
# 查看监听端口
ss -tlnp | grep 1080
netstat -tlnp | grep 1080
# 强制停止所有隧道
sudo pkill -f 'ssh.*-D.*1080'代理测试
bash
# 测试 GitHub 连通性
curl --socks5-hostname 127.0.0.1:1080 -I https://github.com
# 测试 Git 操作
git ls-remote --heads https://github.com/octocat/Hello-World.git
# 查看出口 IP
curl --socks5-hostname 127.0.0.1:1080 https://api.ipify.org
# 测试 DNS 解析
curl --socks5-hostname 127.0.0.1:1080 https://dns.google/resolve?name=github.com验证测试
测试记录 (2026-08-22)
1. 代理连通性测试
bash
$ curl --socks5-hostname 127.0.0.1:1080 -sI https://github.com
HTTP/2 200
date: Sat, 22 Aug 2026 09:56:17 GMT
content-type: text/html; charset=utf-8✅ 通过
2. Git 克隆测试
bash
$ cd /home/xxx/docs/work && git clone https://github.com/knoai/knowrite.git
Cloning into 'knowrite'...
remote: Enumerating objects: 234, done.
remote: Counting objects: 100% (234/234), done.
remote: Compressing objects: 100% (123/123), done.
remote: Total 234 (delta 98), reused 201 (delta 85), pack-reused 0
Receiving objects: 100% (234/234), 293.80 KiB | 1.23 MiB/s, done.
Resolving deltas: 100% (98/98), done.✅ 通过 - 成功克隆到 /home/xxx/docs/work/knowrite
3. 服务状态检查
bash
$ sudo systemctl status ssh-socks5-tunnel
● ssh-socks5-tunnel.service - SSH SOCKS5 Tunnel to 8.153.84.140
Loaded: loaded (/etc/systemd/system/ssh-socks5-tunnel.service; enabled)
Active: active (running) since Sat 2026-08-22 18:07:11 CST
Main PID: 2332448 (autossh)
Tasks: 2 (limit: 309166)
CPU: 16ms
CGroup: /system.slice/ssh-socks5-tunnel.service
├─2332448 /usr/lib/autossh/autossh ...
└─2332454 /usr/bin/ssh ... -D 1080 root@8.153.84.140✅ 通过 - 服务运行正常
4. 端口监听检查
bash
$ ss -tlnp | grep 1080
LISTEN 0 128 127.0.0.1:1080 0.0.0.0:* users:(("ssh",pid=2332454,fd=5))
LISTEN 0 128 [::1]:1080 [::]:* users:(("ssh",pid=2332454,fd=4))✅ 通过 - 端口正常监听
故障排查
常见问题
1. 代理连接失败
症状: curl: (7) Failed to connect to 127.0.0.1 port 1080: Connection refused
排查步骤:
bash
# 检查服务状态
sudo systemctl status ssh-socks5-tunnel
# 检查端口监听
ss -tlnp | grep 1080
# 查看日志
sudo journalctl -u ssh-socks5-tunnel -n 50
# 手动测试 SSH 连接
ssh -p 22 -N -D 1080 root@8.153.84.140解决方案:
- 重启服务:
sudo systemctl restart ssh-socks5-tunnel - 检查网络:
ping 8.153.84.140 - 检查 SSH 密钥:
ssh -v root@8.153.84.140
2. Git 克隆超时
症状: fatal: unable to access 'https://github.com/...': Failed to connect
排查步骤:
bash
# 检查 Git 代理配置
git config --global --get http.proxy
git config --global --get https.proxy
# 测试代理连通性
curl --socks5-hostname 127.0.0.1:1080 -I https://github.com
# 检查环境变量
echo $https_proxy解决方案:
- 重新配置 Git 代理:bash
git config --global http.proxy socks5h://127.0.0.1:1080 git config --global https.proxy socks5h://127.0.0.1:1080 - 使用浅克隆加速:
git clone --depth 1 <url>
3. 环境变量未生效
症状: 新终端中 echo $https_proxy 为空
排查步骤:
bash
# 检查 profile 脚本是否存在
ls -l /etc/profile.d/socks5_proxy.sh
# 检查脚本权限
stat /etc/profile.d/socks5_proxy.sh
# 手动加载
source /etc/profile.d/socks5_proxy.sh解决方案:
- 确保文件可读:
sudo chmod 644 /etc/profile.d/socks5_proxy.sh - 重新登录或新开终端
4. 内网服务无法访问
症状: 配置代理后,内网服务(如 GitLab)访问失败
排查步骤:
bash
# 检查 no_proxy 配置
echo $no_proxy
# 测试内网服务
curl http://gitlab.internal.com解决方案:
- 确认
no_proxy包含内网地址段 - 临时禁用代理:
unset http_proxy https_proxy - 为特定域名添加例外:bash
export no_proxy="localhost,127.0.0.1,*.internal.com,10.*,172.*,192.168.*"
5. 隧道频繁断开
症状: 代理间歇性失效,需要频繁重启
排查步骤:
bash
# 查看断开日志
sudo journalctl -u ssh-socks5-tunnel | grep "ServerAlive"
# 检查网络稳定性
ping -c 100 8.153.84.140 | grep "packet loss"解决方案:
- 调整心跳间隔(编辑 service 文件):ini
ExecStart=/usr/bin/autossh -M 0 -o "ServerAliveInterval 15" -o "ServerAliveCountMax 5" ... - 重启服务:
sudo systemctl restart ssh-socks5-tunnel
配置历史
版本变更记录
| 日期 | 版本 | 变更内容 | 操作人 |
|---|---|---|---|
| 2026-08-22 | v1.0 | 初始配置,使用 43.133.251.5 (Dante SOCKS5) | xxx |
| 2026-08-22 | v1.1 | 切换为 SSH 隧道方案(43.133.251.5 端口受限) | xxx |
| 2026-08-22 | v2.0 | 切换到 8.153.84.140(更快的代理服务器) | xxx |
历史配置备份
v1.0 - Dante SOCKS5 (已废弃)
远程服务器: 43.133.251.5 (Rocky Linux 9.4)
安装步骤 (仅供参考):
bash
# 安装依赖
yum install -y gcc make openssl-devel pam-devel libevent-devel
# 下载并编译
cd /tmp
curl -L -o dante-1.4.4.tar.gz https://www.inet.no/dante/files/dante-1.4.4.tar.gz
tar xzf dante-1.4.4.tar.gz
cd dante-1.4.4
./configure --prefix=/usr/local
make -j$(nproc)
make install
# 配置文件 /etc/sockd.conf
internal: 0.0.0.0 port = 1080
external: eth0
socksmethod: none
client pass { from: 0.0.0.0/0 to: 0.0.0.0/0 }
socks pass { from: 0.0.0.0/0 to: 0.0.0.0/0 protocol: tcp udp }
# 启动服务
systemctl start sockd
systemctl enable sockd
# 防火墙
iptables -I INPUT -p tcp --dport 1080 -j ACCEPT废弃原因: 云安全组未开放 1080 端口,外部无法连接
v1.1 - SSH 隧道 (43.133.251.5)
配置: 与当前 v2.0 相同,仅服务器地址不同
切换原因: 43.133.251.5 网络延迟较高,GitHub 克隆速度慢
附录
A. 快速参考卡片
bash
# 启动代理
sudo systemctl start ssh-socks5-tunnel
# 测试代理
curl --socks5-hostname 127.0.0.1:1080 -I https://github.com
# 克隆仓库
git clone https://github.com/user/repo.git
# 查看状态
sudo systemctl status ssh-socks5-tunnel
# 查看日志
sudo journalctl -u ssh-socks5-tunnel -fB. 相关资源
C. 性能优化建议
DNS 缓存: 安装本地 DNS 缓存减少查询延迟
bashsudo apt install dnsmasq连接复用: 配置 SSH 连接复用减少握手开销
bash# ~/.ssh/config Host 8.153.84.140 ControlMaster auto ControlPath ~/.ssh/sockets/%r@%h-%p ControlPersist 600压缩传输: 启用 SSH 压缩(适用于低带宽环境)
bash# 修改 service 文件 ExecStart=/usr/bin/autossh ... -C -N -D 1080 root@8.153.84.140
文档维护: xxx
联系方式: 内部文档系统
最后验证: 2026-08-22 18:07 CST