Clash 安装与配置
安装 Clash
Clash 提供了多种安装方式,以下是常见的几种方法:
-
使用 Homebrew(macOS)
brew install clash
安装完成后,可以通过
clash命令启动。 -
从官方下载
- 访问 Clash 官方网站
- 下载并安装对应平台的二进制文件。
-
手动下载 & 解压
- 从官网或第三方镜像下载
.zip或.tar.gz文件。 - 解压后将
clash放入/usr/local/bin目录,并赋予执行权限。
- 从官网或第三方镜像下载
-
Docker 环境
docker pull clash docker run -d --name clash clash --start
-
Snap(Ubuntu 等)
snap install clash
-
卸载旧版本
clash flush # 退出所有会话
然后按照上述方法重新安装。
配置 Clash
-
通过命令行配置
clash -x <配置文件路径>
创建一个
clash.json文件:{ "port": 789, "rules": [ { "domain": "example.com", "replace": "http://localhost:300" } ] }启动时使用:
clash -x clash.json
-
通过 GUI 配置
- 打开
clash,点击右上角的齿轮图标。 - 输入配置信息并保存。
- 打开
导入配置文件
-
从 Git 仓库导入
- 支持 Gitea、GitHub、GitLab。
- 从 Gitea 仓库导入:
clash import https://gitea.example.com/username/repository.git
-
导入本地文件
clash import <配置文件路径>
Clash 的基本用法
创建自定义规则
Clash 的核心功能是规则管理,以下是常见规则类型:
-
域名匹配
{ "domain": "example.com", "replace": "http://localhost:300" }- 将
example.com转发到本地服务器。
- 将
-
路径匹配
{ "path": "/api/*", "replace": "http://localhost:300/api/inspect" }- 过滤所有以
/api/开头的请求。
- 过滤所有以
-
域名黑名单
{ "domain": "ads.example.com", "block": true }禁止访问某个域名。
过滤域名
- 使用
clash filter命令添加域名过滤规则:clash filter domain example.com
- 过滤所有来自
example.com的请求。
- 过滤所有来自
设置路由规则
- 将特定域名路由到本地服务器:
{ "domain": "localhost:300", "route": "/", "replace": "http://localhost:300" }- 访问
http://example.com会被转发到http://localhost:300。
- 访问
代理设置
- 将所有流量代理到本地服务器:
clash proxy http://localhost:300
这将将所有出站流量通过本地服务器处理。
反向代理
- 将本地服务器的流量反向代理到外部服务器:
{ "listen": 789, "reverse_proxy": { "target": "http://example.com:80", "port": 80 } }- 将
localhost:300反向代理到example.com:80。
- 将
Clash 的高级配置
使用 JSON 配置文件
- 创建一个
clash.json文件,定义详细的规则和设置。{ "port": 789, "rules": [ { "domain": "example.com", "replace": "http://localhost:300" }, { "domain": "ads.example.com", "block": true } ], "filter": [ "ads.example.com", "malicious-site.com" ] } - 启动时使用:
clash -x clash.json
环境变量代入
- 在 JSON 配置文件中使用环境变量:
{ "port": ${PORT:789}, "rules": [ { "domain": "${DOMAIN:example.com}", "replace": "http://localhost:${PORT}" } ] }这样可以根据不同的环境(如开发、测试)动态调整配置。
插件支持
Clash 提供了丰富的插件系统,常用插件包括:
-
clipboard:自动复制域名和响应。
-
geo:根据地理位置过滤域名。
-
bloomfilter:优化域名过滤性能。
-
安装插件:
clash install bloomfilter
-
启用插件:
clash -p bloomfilter
Clash 的常见问题与故障排除
Clash Crash
-
Clash 突然崩溃,重启服务:
clash restart
-
如果无法启动,尝试清除缓存:
clash flush
DNS 解析错误
- 检查 DNS 配置是否正确,确保域名解析正常。
端口占用
- 检查是否有其他程序占用了 Clash 的默认端口(789)。
lsof :789
配置错误
- 检查 JSON 配置文件的语法是否正确,确保所有逗号和括号正确使用。
Clash 的进阶操作
导入外部 Git 仓库
- 从 Gitea、GitHub、GitLab 导入配置:
clash import https://gitea.example.com/username/repository.git
自定义规则组合
- 使用 运算符组合多个规则:
{ "domain": "example.com||another.com", "replace": "http://localhost:300" }- 这将匹配
example.com或another.com。
- 这将匹配
高级插件配置
- 配置 Bloomfilter:
clash config bloomfilter mem_limit 256M
根据需要调整内存限制。
Clash 的安全注意事项
- 防火墙设置:确保 Clash 的 listening 端口被防火墙允许。
- 使用强密码:避免设置弱密码或 Leaving Mode。
- 监控日志:定期查看 Clash 的日志,确保没有异常流量。
Clash 的常见错误与解决方法
-
Crash 错误
- 检查日志文件:
clash logs - 确保没有过多的请求压力。
- 检查日志文件:
-
DNS 解析错误
- 检查本地 DNS 配置是否正确。
- 尝试使用手动指定 DNS 服务器:
clash -D 8.8.8.8
-
端口占用
- 使用
sudo lsof检查占用情况。 - 杀死占用端口的进程:
sudo kill -9 <进程 PID>
- 使用









