在Linux下使用Clash
date
May 27, 2023
slug
clashlinux
status
Published
tags
工具
网络
summary
type
Post
安装
在官方下载Clash,以Centos 7.9为例可选择clash-linux-amd64-v1.16.0.gz。
# 下载
wget -O ~/clash.gz https://github.com/Dreamacro/clash/releases/download/v1.16.0/clash-linux-amd64-v1.16.0.gz
# 解压
gunzip clash.gz
# 添加执行权限
chmod +x clash
Clash 运行时需要
Country.mmdb
文件,当第一次启动 Clash 时(使用 ./clash
命令) 会自动下载(会下载至 /home/XXX/.config/clash
文件夹下)。自动下载可能会因网络原因较慢,可以访问该链接手动下载。Country.mmdb 文件利用 GeoIP2 服务能识别互联网用户的地点位置,以供规则分流时使用。
# 为了方便管理,我将clash也移动到其数据目录下
mv ~/clash /home/XXX/.config/clash
# 运行后可在控制台看到日志输出
./clash
INFO[0000] Mixed(http+socks) proxy listening at: [::]:7890
INFO[0000] RESTful API listening at: 127.0.0.1:9090
配置
配置快捷命令
一般情况下不需要使用Clash,所以要配置快捷命令方便开启、关闭。新建
/etc/profile.d/clash.sh
并加入以下两段内容。1、Clash环境变量
export CLASH_HOME=/root/.config/clash
export PATH=$PATH:$CLASH_HOME/
2、代理开关
alias use_proxy="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"
alias dis_proxy="unset http_proxy https_proxy all_proxy"
在启动Clash后,当需要使用代理时执行
use_proxy
,不需要时则执行dis_proxy
。配置订阅内容
一般网络服务商提供 Clash 订阅链接,可以直接下载订阅文件内容保存到
config.yaml
中即可。Clash 配置文件的完整参数介绍见官方文档。如果订阅链接是加密的,那么可将本地Windows或Mac版Clash解密后的config.yaml文件拷贝上去。这可能导致订阅内容不是最新的~~~
scp config.yaml root@x.x.x.x:~/root/.config/clash/
重新启动clash,然后执行
curl
ipinfo.io
根据其返回IP所在城市判断代理是否成功。配置开机启动
创建 systemd 服务配置文件
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=/root/.config/clash/clash -d /root/.config/clash
[Install]
WantedBy=multi-user.target
开机启动配置
# 启用开机启动
systemctl enable clash
# 启动服务/查看状态
systemctl start clash
systemctl status clash
# 查看日志
journalctl -xe | grep clash
参考资料: