# clash部署至服务器,为设备提供“上网”服务

# 动机:

订阅了clash一年的服务,手机和电脑客户端进行科学上网非常方便。但是clash没有上架APPStore,导致ios用户无法使用,根据官网介绍需要换非国区账号并花费$2.99下载rocket。奈何囊中羞涩。根据vpn原理:本地流量转发到vpn运行监听的端口 ,再转发到代理服务器上。由此突发奇想将clash部署到linux服务器,然后开放clash监听端口,本地在网络设置添加代理服务器ip和端口就可以实现“科学上网”。这样的话移动设备就不必下载客户端,ipad实现科学上网也变得很方便。使用别人设备需要加速服务也将变得便捷。

# 安装clash for linux到服务器

# 下载clash for linux二进制文件

Github clash最新版本 (opens new window)

image-20230310185155509)

一般PC端使用的就是X64架构,选择amd64

# 配置

## 安装目录根据个人习惯 我习惯放在/opt目录下
cd /opt
## 下载压缩包(若通过浏览器下载可忽略这一步) 需安装有wget 若没有可以先下载 sudo apt install wget -y
sudo wget https://github.com/Dreamacro/clash/releases/download/v1.13.0/clash-linux-amd64-v1.13.0.gz
## 解压
sudo gzip -d clash-linux-amd64-v1.13.0.gz
## 重命名
sudo mv clash-linux-amd64-v1.13.0.gz clash
## 赋予二进制文件可执行权限
sudo chmod a+x clash
# 执行二进制文件会在~/.config下初始化生成文件夹clash(cache.db config.yaml Country.mmdb) 
./clash
1
2
3
4
5
6
7
8
9
10
11
12

这里执行完后成功后,会在~/.config(一般在root:/root/.config/clash或者home:/home/lighthouse/.config文件夹下)文件下自动生成calsh目录并下载config.yaml配置文件和Country.mmdb文件。一般运行完./calsh界面会报错或者一直显示download。这是因为你配置文件不是根据订阅链接下载的。

这时我们需要删除config.yaml文件和Country.mmdb文件,找到自己clash账户给的订阅地址,那个就是下载配置文件的地址。

sudo wget -O config,yaml 你的订阅地址
1

若果下载的问件里面是一大串的英文字母,在订阅连接后面添加 &flag=clash即可。

image-20230310185155509)

这样设置后在执行./clash就会自动下载Country.mmdb。

[root@VM-4-12-centos ~]# cd /opt
[root@VM-4-12-centos opt]# ./clash
INFO[0000] Can't find MMDB, start download    
1
2
3

它会一直卡在这里,实际文件已经下载好了,关闭终端重新运行就行了。

image-20230310204651050显示如上就证明服务开启成功。

如果Country.mmdb文件不删除的话会报错。

[root@devops clash]# ./clash -d .
WARN[0000] MMDB invalid, remove and download 
1
2

就是因为Country.mmdb文件受损或者不完全导致的。

# 添加UI管理界面

ui 控制面板,目前有两款,分别是 clash 原作者的 clash-dashboard (opens new window) 以及另一位大佬的 yacd-dashboard (opens new window) ,大家自选其一即可,我常用的是 clash-dashboard

git clone https://github.com/Dreamacro/clash-dashboard.git
1

问了方便管理,我将ui和clash发在一起:

image-20230310205446574

同时配置config.yaml

image-20230310205743361

external-controller: '0.0.0.0:9090'
# ui控制界面端口
secret: ******
#访问密码
external-ui: /opt/clashui
# ui静态文件目录位置
1
2
3
4
5
6

配置好后重新加载clash服务即可,记得关闭端口防火墙。

访问http://ip:9090/ui

image-20230310210438080

API Base URL一栏中输入:http://IP:9090 (opens new window) ,在Secret(optional)一栏中输入config.yaml的Secret。

点击Add并选择刚刚输入的管理界面地址,之后便可在浏览器上进行一些配置。切换节点或者代理模式。

image-20230310210830921

image-20230310210837259

image-20230310210844017

ui控制界面可以添加nginx反向代理使用二级域名访问

image-20230310213616534

# 注册systemctl服务

首先将clash注册为systemctl服务 /usr/lib/systemd/system下新建一个文件clash.service

[Unit]
Description=Clash Service
[Service]
Type=simple
User=root
ExecStart=/opt/clash
[Install]
WantedBy=multi-user.target
1
2
3
4
5
6
7
8
# 重新加载配置文件 
sudo systemctl daemon-reload
1
2
systemctl start clash.service
#开启
systemctl stop clash.service
#关闭
systemctl restart clash.service
#重启
systemctl status clash.service
#查询状态
1
2
3
4
5
6
7
8

image-20230310211653961)

# 移动端配置

windows:

image-20230310212537605

地址为服务器ip,端口为clash监听的端口。

ipad:

image-20230310212938223

phone:

image-20230310213032230

这样移动或者pc端就不用下载代理应用就可以实现网上冲浪了!

但是这样因为代理走的你服务器的带宽,所以相对来说网速会慢一点,但是github等网文还是很快的。

如有问题发送邮件2196728181@qq.com欢迎交流!