前言:
跑过代码的小伙伴都知道,有时候需要使用实验室或者公司的服务器跑一些代码,就需要远程连接服务器使用。然而代码经常需要从huggingface等加载一些模型或者数据,就需要配置一个clash。但是服务器又不会给sudo权限,所以只能在个人空间下配置一个clash。

本文以Ubuntu为例在远程Linux服务器上配置Clash。

下载clash

Releases · doreamon-design/clash上下载一个适合自己的版本,我这里下载的是clash_2.0.24_linux_amd64.tar.gz,然后上传到服务器上。

解压

1
2
# 进入clash_2.0.24_linux_amd64.tar.gz所在目录后
tar -xvf clash_2.0.24_linux_amd64.tar.gz

将可执行文件移动到本地bin目录

注:下边的 ~是代表个人空间的根目录,这个目录下正常是有.local".config"文件夹的

1
2
3
4
5
6
7
8
mkdir -p ~/.local/bin
echo 'export PATH="$HOME/.local/bin:$PATH"' >> ~/.bashrc
source ~/.bashrc
# 如果使用的是zsh,将~/.bashrc改为~/.zshrc

mv clash ~/.local/bin/clash
# 赋予执行权限
chmod +x ~/.local/bin/clash

按照上面步骤操作完毕后,检验安装

1
clash -v

配置clash

准备配置目录

安装clash完毕后,要进行代理的配置

1
2
3
4
5
mkdir -p ~/.config/clash
cd ~/.config/clash

# 下载Country.mmdb文件
curl -o ~/.config/clash/Country.mmdb https://cdn.jsdelivr.net/gh/Dreamacro/maxmind-geoip@release/Country.mmdb

如果下载mmdb文件遇到问题,可以本地下载后上传到~/.config/clash文件夹。

下载配置文件

如果本地有代理的配置文件直接上传到~/.config/clash文件夹并命名为config.yaml即可,如果没有也可以使用订阅连接下载

1
curl -o config.yaml "你的订阅链接"

启动Clash

如果上述过程都没问题就可以运行了

1
2
3
4
5
6
7
8
# 前台启动
clash -d ~/.config/clash

# 或后台启动
nohup clash -d ~/.config/clash > ~/.config/clash/clash.log 2>&1 &

# 停止服务
pkill -x clash

代理成功启动后,需要设置代理

1
2
3
4
5
6
# 设置临时代理
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890

# 验证代理是否工作
curl -I http://www.google.com

如果不想每次都使用都设置代理,将以下内容添加到~/.bashrc

1
2
3
4
5
6
7
8
9
# Clash 代理设置
export http_proxy=http://127.0.0.1:7890
export https_proxy=http://127.0.0.1:7890
export HTTP_PROXY=http://127.0.0.1:7890
export HTTPS_PROXY=http://127.0.0.1:7890

# 可选:设置不代理的地址
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
export NO_PROXY="localhost,127.0.0.1,localaddress,.localdomain.com"

创建更便捷的管理脚本

创建启动脚本~/start_clash.sh

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
#!/bin/bash
CLASH_DIR="$HOME/.config/clash"
LOG_FILE="$CLASH_DIR/clash.log"

# 检查是否已运行
if pgrep -x "clash" > /dev/null; then
echo "Clash 已经在运行"
exit 1
fi

# 启动 clash
echo "启动 Clash..."
nohup clash -d "$CLASH_DIR" > "$LOG_FILE" 2>&1 &
echo "Clash 已启动,日志文件: $LOG_FILE"
echo "控制面板: http://clash.razord.top (设置外部控制地址: 127.0.0.1:9090)"

创建停止脚本~/stop_clash.sh

1
2
pkill -x clash
echo "Clash 已停止"
1
2
# 启用脚本
sh "脚本名"