ghostxu97 / clash-for-linux

Linux环境安装配置Clash工具,以实现代理上网效果。包含下载、安装、配置、运行、测试以及开机自启动、定期自动更新订阅功能的操作文档,希望对你有所帮助
379 stars 65 forks source link

Linux安装配置Clash代理

概括描述

Linux环境安装配置Clash工具,以实现代理上网效果。包含下载、安装、配置、运行、测试以及开机自启动、定期自动更新订阅功能的操作文档,希望对你有所帮助。


一、下载clash for linux

二、配置并运行clash

[root@localhost ~]# su
[root@localhost ~]# mkdir /opt/clash
[root@localhost ~]# gunzip clash-linux-amd64-v1.18.0.gz
[root@localhost ~]# mv Desktop/clash-linux-amd64-v1.18.0 /opt/clash/clash
[root@localhost ~]# cd /opt/clash/
[root@localhost clash]# wget -O config.yaml [订阅链接]
--2021-05-01 22:39:37--  [订阅链接]
Resolving www.sub-speeder.com (www.sub-speeder.com)... 104.21.18.176, 172.67.182.209, 2606:4700:3035::ac43:b6d1, ...
Connecting to www.sub-speeder.com (www.sub-speeder.com)|104.21.18.176|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 44830 (44K) [application/octet-stream]
Saving to: ‘config.yaml’

100%[======================================>] 44,830       287KB/s   in 0.2s   

2021-05-01 22:39:38 (287 KB/s) - ‘config.yaml’ saved [44830/44830]

[root@localhost clash]# wget -O Country.mmdb https://cdn.jsdelivr.net/gh/Hackl0us/GeoIP2-CN@release/Country.mmdb
--2021-05-01 22:39:55--  https://www.sub-speeder.com/client-download/Country.mmdb
Resolving www.sub-speeder.com (www.sub-speeder.com)... 172.67.182.209, 104.21.18.176, 2606:4700:3035::ac43:b6d1, ...
Connecting to www.sub-speeder.com (www.sub-speeder.com)|172.67.182.209|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 3878104 (3.7M) [application/octet-stream]
Saving to: ‘Country.mmdb’

100%[======================================>] 3,878,104   2.86MB/s   in 1.3s   

2021-05-01 22:39:58 (2.86 MB/s) - ‘Country.mmdb’ saved [3878104/3878104]
[root@localhost clash]# chmod +x clash 
[root@localhost clash]# ll
total 19832
-r-x--x--x. 1 root root 9326592 May  1 23:24 clash
-rw-r--r--. 1 root root   44830 May  1 22:39 config.yaml
-rw-r--r--. 1 root root 3878104 Oct  9  2020 Country.mmdb
[root@localhost clash]# ./clash -d .
INFO[0000] Start initial compatible provider Proxy      
INFO[0000] Start initial compatible provider Domestic   
INFO[0000] Start initial compatible provider AsianTV    
INFO[0000] Start initial compatible provider Others     
INFO[0000] Start initial compatible provider GlobalTV

三、启用系统代理

[root@localhost ~]# gsettings set org.gnome.system.proxy mode 'manual'
[root@localhost ~]# gsettings set org.gnome.system.proxy.http port 7890
[root@localhost ~]# gsettings set org.gnome.system.proxy.http host '127.0.0.1'
[root@localhost ~]# gsettings set org.gnome.system.proxy.socks port 7891
[root@localhost ~]# gsettings set org.gnome.system.proxy.socks host '127.0.0.1'
[root@localhost ~]# gsettings set org.gnome.system.proxy ignore-hosts "['localhost', '127.0.0.0/8', '::1']"

打开系统设置,点击网络代理右边的 ⚙ 按钮,选择手动,填写 HTTP 和 HTTPS 代理为 127.0.0.1:7890,填写 Socks 主机为 127.0.0.1:7891,即可启用系统代理,如下图所示。

image

四、测试验证

五、配置开机自启动

1.创建service文件

touch /etc/systemd/system/clash.service

2.编辑service文件

打开service文件

vi /etc/systemd/system/clash.service

填入以下内容(注意修改clash文件夹路径)

[Unit]
Description=clash daemon

[Service]
Type=simple
User=root
ExecStart=/opt/clash/clash -d /opt/clash/clash/
Restart=on-failure

[Install]
WantedBy=multi-user.target

保存并退出

3.启动Clash

systemctl start clash.service

4.设置Clash开机自启动

systemctl enable clash.service

以下为Clash相关的管理命令

启动Clash

systemctl start clash.service

重启Clash

systemctl restart clash.service

查看Clash运行状态

systemctl status clash.service

六、配置定时更新订阅

[root@localhost ~]# crontab -e
29 6    * * *   root    pgrep clash | xargs kill -s 9 
30 6    * * *   root    mv /opt/clash/config.yaml /opt/clash/configbackup.yaml 
31 6    * * *   root    wget -P /opt/clash/ -O config.yaml [你的订阅链接]
32 6    * * *   root    nohup /opt/clash/clash -d /opt/clash/
[root@localhost ~]# systemctl restart crond.service

七、参考链接

八、订阅链接提供商推荐(SPEEDER)

Speeder

推荐理由

九、打赏 - JUST FOR FUN

一杯咖啡钱, 打赏金额随意,感谢大家~ :) 微信 支付宝