配置过程

首先从Dremacro/clash仓库下载最新的发行版。注意选择适合自己CPU架构的版本。可以通过dpkg --print-architecturearch来查看CPU架构,x86_64amd64是等价的架构名称。

1
2
3
4
wget https://github.com/Dreamacro/clash/releases/download/v1.10.0/clash-linux-amd64-v3-v1.10.0.gz
gunzip clash-linux-amd64-v3-v1.10.0.gz
mv clash-linux-amd64-v3-v1.10.0 <你的保存目录>/clash
chmod +x <你的保存目录>/clash

将clash所在目录加入PATH,例如在~/.bashrc中添加
1
export PATH=your/path/to/clash:$PATH

再进行clash初始化,执行clash命令即可,初始化完毕后,在~/.config/clash看到了config.ymlCountry.mmdb两个重要文件。

接着从你购买的VPN服务商那里获取clash的订阅连接,例如我使用的kingfast,将其覆盖保存为前面的config.yml

1
wget https://sub.o4o.win/api/sub/****?type=clash -O ~/.config/clash/config.yml

此时再次启动clash,即可看到clash输出日志。

最后配置http/https系统代理即可

1
2
export http_proxy=http://127.0.0.1:7890/ # 此处7890端口需要替换成你配置的clash端口
export https_proxy=$http_proxy

此外,可以将clash注册为系统服务,方法参见github源项目的wiki

私人Tips

如果不是clash一直保持运行,那么每次都需要手动设定代理,如果clash没有启动,而代理设置没有关闭,会报错无法连接代理服务器。为此本人在~/.bashrc中使用下列代码来动态检查是否开启系统http/https代理,供大家参考:

1
2
3
4
5
6
7
8
9
10
# detect clash VPN
http_proxy_port=7890 # 此处端口需要自行修改
lsof -i:${http_proxy_port}|grep "clash" 1>/dev/null 2>&1

if [ $? = 0 ];then
export http_proxy=http://127.0.0.1:${http_proxy_port}/
export https_proxy=$http_proxy
alias np='unset http_proxy https_proxy'
echo `whoami`"您好,检测到Clash已开启,http/https系统代理在当前终端已用,如需在当前终端禁用,执行np"
fi

如果配置成为了系统服务,为了方便切换是否开启代理而不需要关闭clash服务,可以参考如下配置。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
http_proxy_port=7890
systemctl status clash | grep "active (running)" 1>/dev/null 2>&1

if [ $? = 0 ];then
function up {
export http_proxy=http://127.0.0.1:${http_proxy_port}/
export https_proxy=$http_proxy
echo "当前终端http代理已启用"
}

function down {
unset http_proxy https_proxy
echo "当前终端http代理已忽略"
}

echo `whoami`"您好,检测到clash服务已开启,可以通过up/down在当前终端启用http代理"
fi