云服务器Linux+Clash科学上网

-目录-

1. 下载Clash for Linux到Linux主机上

获取Clash文件

然后解压得到binaries,重命名一下。

gzip -d clash-linux-amd64-v3-v1.13.0.gz
mv clash-linux-amd64-v1.10.0 clash

添加执行权限。

chmod +x clash

2. 添加配置文件

创建配置文件目录,Clash会自动读取这个文件里的配置。

mkdir ~/.config/clash

接下来向~/.config/clash添加config.yamlCountry.mmdb。前者是节点配置文件,后者是GeoLite2 data created by MaxMind

添加config.yaml

添加Country.mmdb

3. 运行

在刚才有clash的目录下运行,不出意外的话应该显示:

clash
INFO[0000] Start initial compatible provider xx  
INFO[0000] Mixed(http+socks) proxy listening at: 127.0.0.1:7890 
INFO[0000] RESTful API listening at: [::]:9090  

除此之外,还要开启系统代理:

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

测试运行:

curl https://www.google.com

连通输出示例:此时可以curl -L https://www.google.com跟踪跳转

<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>302 Moved</TITLE></HEAD><BODY>
<H1>302 Moved</H1>
The document has moved
<A HREF="https://www.google.com.hk/url?sa=p&amp;hl=zh-CN&amp;pref=hkredirect&amp;pval=yes&amp;q=https://www.google.com.hk/&amp;ust=1677655826189564&amp;usg=AOvVaw0zuWWrC_vCIRd5fj5JIcRS">here</A>.
</BODY></HTML>

未连通输出示例:

curl: (35) Encountered end of file

4. (可选)以服务方式运行与快捷启动/关闭代理

以服务方式运行

移动相关文件到系统推荐的位置。

#在Clash当前文件的目录下
mv ~/.config/clash /etc
mv clash /usr/local/bin/clash

创建服务

vim /etc/systemd/system/clash.service
#写入以下内容
[Unit]
Description=Clash daemon, A rule-based proxy in Go.
After=network.target

[Service]
Type=simple
Restart=always
ExecStart=/usr/local/bin/clash -d /etc/clash

[Install]
WantedBy=multi-user.target

设置开机自启动

systemctl enable clash

运行Clash

systemctl start clash

查看状态与输出,若和之前输出相同,则无错。

设置快捷开启系统代理与关闭系统代理

~/.bashrc,中填入以下内容。

function proxy_off() {
unset http_proxy
unset https_proxy
unset no_proxy
echo -e "Proxy Off"
}


function proxy_on(){
export http_proxy="http://127.0.0.1:7890"
export https_proxy=$http_proxy
export no_proxy="localhost,127.0.0.1,localaddress,.localdomain.com"
echo -e "Proxy On"
}

然后source使其生效。

source ~/.bashrc

这样之后,可以直接输入proxy_onproxy_off,快捷开启和关闭。

5. 参考

在 Linux 上使用 Clash 作代理 | Verne in GitHub (einverne.github.io)

在 Linux 中使用 Clash | CodeSwift (iswiftai.com)

© 2019 - 2023 · YuYoung's Blog