Ubuntu 20.04 clash for linux 配置

参考:

ClashForLinux自动更新订阅配置

Clash For Linux自动更新订阅配置

配置

通过systemctl来管理Clash的进程,对应clash.service文件,通过两个脚本start-clash.shstop-clash.sh来管理Clash的启停,具体配置如下。

clash.service

置于/lib/systemd/system/clash.service,注意修改用户名和订阅链接。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
[Unit]
Description=clash daemon
After=network.target

[Service]
Type=simple
User=tangger
ExecStart=/home/<用户名>/.config/clash/start-clash.sh
ExecStop=/home/<用户名>/.config/clash/stop-clash.sh
Environment="HOME=/home/<用户名>/"
Environment="CLASH_URL=<填入您的订阅链接>"

[Install]
WantedBy=multi-user.target

start-clash.sh

置于${HOME}/.config/clash/start-clash.sh,注意修改用户名。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#!/bin/bash
# save this file to ${HOME}/.config/clash/start-clash.sh

# save pid file
echo $$ > ${HOME}/.config/clash/clash.pid

diff ${HOME}/.config/clash/config.yaml <(curl -s ${CLASH_URL})
if [ "$?" == 0 ]
then
/usr/local/bin/clash -d /home/<用户名>/.config/clash/
else
curl -L -o ${HOME}/.config/clash/config.yaml ${CLASH_URL}
/usr/local/bin/clash -d /home/<用户名>/.config/clash/
fi

stop-clash.sh

置于${HOME}/.config/clash/stop-clash.sh

1
2
3
4
5
6
7
#!/bin/bash
# save this file to ${HOME}/.config/clash/stop-clash.sh

# read pid file
PID=`cat ${HOME}/.config/clash/clash.pid`
kill -9 ${PID}
rm ${HOME}/.config/clash/clash.pid

赋予执行权限

运行:

1
2
chmod +x ${HOME}/.config/clash/start-clash.sh
chmod +x ${HOME}/.config/clash/stop-clash.sh

最后

运行:

1
2
systemctl enable clash
systemctl start clash

ps -ef | grep clash验证下clash是否已在运行,Setting - Network中配置Network Proxy,然后访问网页测试。没问题的话,clash将开机自动启动,并自动更新配置。

Tips

  • clash二进制文件放在/usr/local/bin/
  • 注意文件路径
  • .sh记得加执行权限

本博客所有文章除特别声明外,均采用 CC BY-SA 4.0 协议 ,转载请注明出处!