在linux终端下使用代理(Clash+Proxychains4)
参考资料:
- Ubuntu 使用 Clash For Linux 客户端教程 - 彧繎博客 (opclash.com)
- linux - tails os and proxychains - getting denied connection - Stack Overflow
由于Clash已经删库跑路了,所以只能用在网上找到的链接
1. 安装并配置clash
1 | |
注意事项:
- 这里的导入订阅可以直接从window的clash中将yaml配置文件复制到linux机器上,而不用wget
- 值得注意的是,我们这里都是在个人目录下配置的,也可以在根目录下配置。这会导致订阅配置文件的地址不同而已(家目录下)
2. 配置代理
有两种方式,一种是通过环境变量,一种是通过proxychains4
(1)通过环境变量
-
临时:在当前终端export:export http_proxy=http://127.0.0.1:7890 https_proxy=http://127.0.0.1:7890
-
永久:写入~/.bashrc(完成后记得source ~/.bashrc):
echo -e "export http_proxy=http://127.0.0.1:7890\nexport https_proxy=http://127.0.0.1:7890" >> ~/.bashrc
(2)通过proxychains4(推荐)
可以直接apt-get install,也可以通过源码安装(非root用户只能通过源码安装,但笔者没在非root用户下试过,一般配置好configure,设置好环境变量应该可以)
1 | |
接下来修改ProxyChains的配置文件/etc/proxychains.conf
1 | |
- 上述条目解除注释,其余注释
- [ProxyList]后添加代理,这里我们添加我们之前设置的代理(clash使用的端口号默认为7890)
到了这一步,直接启动clash之后就可以使用了,以访问谷歌为例:
- 已设置环境变量:
curl google.com - 通过proxychains4:
proxychains4 curl google.com
其它:
-
还可以设置git代理
1
git config --global http.proxy http://127.0.0.1:7890 && git config --global https.proxy http://127.0.0.1:7890 -
为什么选择proxychains4:可以清楚地知道自己哪条命令用来代理,并且可以为多种命令设置代理,如docker、k8s等
3. 设置开机自启动
-
sudo vim /etc/systemd/system/clash.service
-
加入下列内容(记得修改ExecStart路径)
1
2
3
4
5
6
7
8
9
10[Unit]
Description=Clash - A rule-based tunnel in Go
Documentation=https://github.com/Dreamacro/clash/wiki
[Service]
OOMScoreAdjust=-1000
ExecStart=/usr/local/bin/clash -f /root/.config/clash/config.yaml
Restart=on-failure
RestartSec=5
[Install]
WantedBy=multi-user.target -
# 配置开机自启 systemctl enable clash # 启动 clash 服务 (重启、终止对应为restart stop) systemctl start clash
在linux终端下使用代理(Clash+Proxychains4)
https://lai-jx.github.io/2023/11/07/proxy/