服务器部署 clash + Yacd


命令行版

这是为了没有gui界面的linux服务器所写的教程,因此全部使用命令行。

源码安装

安装

选择版本

基于不可抗力因素,请自行进行寻找镜像 fork ,后然后进行编译

使用 uname -mhttps://github.com/Dreamacro/clash/releases 中选择查看对应的服务器版本

安装 clash

//注意自己改文件名
sudo gzip -d clash-linux-armv7-v1.13.0
sudo mkdir /opt/clash
sudo mv clash-linux-armv7-v1.13.0 /opt/clash/clash
cd /opt/clash
chmod +x /opt/clash/clash

//下载配置信息
sudo mkdir -p ~/.config/clash && cd ~/.config/clash
sudo wget -O config.yaml [订阅链接]
sudo wget -O Country.mmdb http://www.sub-speeder.com/client-download/Country.mmdb

clash的配置文件在~/.config/clash/config.yaml,打开后添加基本配置(如果有就不用添加了)

port: 7890
socks-port: 7891
allow-lan: true
mode: Rule
log-level: info
secert: 123456 // 增加这一行, 如果你希望你的clash web要密码访问可以在这块配置密码, 如果不需要直接注释掉即可
#此处必须是0.0.0.0 才可以通过局域网访问,也就是说要想通过web管理,必须填0.0.0.0
external-controller: 0.0.0.0:9090

启动

cd /opt/clash
./clash -d ~/.config/clash .

在本地测试服务器端口是否可达,如果访问不通,排查云主机是否开放了端口。

telnet [服务器IP] 7890

如果在配置文件中使用的是 mixed-port,则 socks5 和 http 代理共用 7980 端口,代理地址都为

username:passwd@[服务器IP]:7890

设置系统代理

sudo nano /etc/environment

加入以下三行

export http_proxy="http://127.0.0.1:7890"
export https_proxy="http://127.0.0.1:7890"
export no_proxy="localhost, 127.0.0.1"

修改sudo文件

sudo visudo

在此处加入

Defaults env_keep+="http_proxy https_proxy no_proxy"

重启

reboot

设置clash开机启动

将配置文件移动到/etc

sudo mv ~/.config/clash /etc

添加启动信息

sudo nano /etc/systemd/system/clash.service

输入以下内容,clash -d的意思是指定配置文件路径,这里已经改成了/etc/clash

[Unit]
Description=clash daemon

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

[Install]
WantedBy=multi-user.target

重新加载systemctl daemon

sudo systemctl daemon-reload

启动Clash

sudo systemctl start clash.service

设置Clash开机自启动

sudo systemctl enable clash.service

Clash相关的管理命令

#启动Clash
sudo systemctl start clash.service
#重启Clash
sudo systemctl restart clash.service
#查看Clash运行状态
sudo systemctl status clash.service
#禁用全局代理
unset http_proxy
unset https_proxy
#启用全局代理
set http_proxy
set https_proxy

配置定时更新订阅:

先撸个脚本,别忘了设可执行权限

#!/bin/bash

# 设置config路径
config_path="/etc/clash"
config_file="$config_path/config.yaml"
backup_file="$config_path/configbackup.yaml"
sub_url="【此处填入自己的订阅】"

# 停止clash
systemctl stop clash.service

# 取消代理
unset https_proxy
unset http_proxy

# 如果配置文件存在,备份
if [ -e "$config_file" ]; then
    mv "$config_file" "$backup_file"
    echo "已备份现有配置文件。"
fi

# 下载新配置文件
if wget -O "$config_file" "$sub_url"; then
    # 配置文件下载成功,重启clash
    systemctl restart clash.service

    # 重设代理
    export http_proxy="http://127.0.0.1:7890"
    export https_proxy="http://127.0.0.1:7890"
else
    # 配置文件下载失败,输出错误信息
    echo "配置文件下载失败,请检查网络连接或配置文件地址。"

    # 如果备份文件存在,恢复备份
    if [ -e "$backup_file" ]; then
        mv "$backup_file" "$config_file"
        echo "已恢复备份配置文件。"
    fi
fi

设置定时任务:

sudo crontab -e

填入以下内容

//每月1号和15号的4点30分开始更新
30 4 1,15 * * sh [脚本目录]/[脚本名称]

重启crontab,使配置生效

sudo systemctl restart cron.service

查看代理是否正常工作:

curl www.google.com

Cash Web 面板(Yacd)

当然这个有在线版,其实在线版就够用了 http://clash.razord.top/#/proxies

Yach 是 Clash Web 版的面板,可以查看服务状态,上传下载的流量统计信息。

sudo docker run -p 1234:80 -d --name yacd ghcr.io/haishanh/yacd:master
sudo ufw allow 7890

访问 http://[服务器IP]:1234 进入页面,填写 Clash 地址,本例为 http://[服务器IP]:9090,secret 填写 config.yaml 中配置的 secret 值,添加后点击新增的链接进入页面。

yacd panel

clash 及 yacd 的部署使用到这里就记录完成了。

ShellClash

功能简介:

  • 通过管理脚本在Shell环境下便捷使用Clash
  • 支持在Shell环境下管理Clash各种功能
  • 支持在线导入Clash支持的分享、订阅及配置链接
  • 支持配置定时任务,支持配置文件定时更新
  • 支持在线安装及使用本地网页面板管理内置规则
  • 支持路由模式、本机模式等多种模式切换
  • 支持在线更新

在线安装:

如无法连接或出现SSL连接错误,请尝试更换各种不同的安装源!

~标准Linux设备安装:

sudo -i #切换到root用户,如果需要密码,请输入密码
bash #如已处于bash环境可跳过
export url='https://fastly.jsdelivr.net/gh/juewuy/ShellClash@master' && wget -q --no-check-certificate -O /tmp/install.sh $url/install.sh  && bash /tmp/install.sh && source /etc/profile &> /dev/null

或者

sudo -i #切换到root用户,如果需要密码,请输入密码
bash #如已处于bash环境可跳过
export url='https://gh.jwsc.eu.org/master' && bash -c "$(curl -kfsSl $url/install.sh)" && source /etc/profile &> /dev/null

~路由设备使用curl安装

#GitHub源(可能需要代理)
export url='https://raw.githubusercontent.com/juewuy/ShellClash/master' && sh -c "$(curl -kfsSl $url/install.sh)" && source /etc/profile &> /dev/null

或者

#jsDelivrCDN源
export url='https://fastly.jsdelivr.net/gh/juewuy/ShellClash@master' && sh -c "$(curl -kfsSl $url/install.sh)" && source /etc/profile &> /dev/null

或者

#作者私人源
export url='https://gh.jwsc.eu.org/master' && sh -c "$(curl -kfsSl $url/install.sh)" && source /etc/profile &> /dev/null

~路由设备使用wget安装

#GitHub源(可能需要代理)
export url='https://raw.githubusercontent.com/juewuy/ShellClash/master' && wget -q --no-check-certificate -O /tmp/install.sh $url/install.sh  && sh /tmp/install.sh && source /etc/profile &> /dev/null

或者

#jsDelivrCDN源
export url='https://fastly.jsdelivr.net/gh/juewuy/ShellClash@master' && wget -q --no-check-certificate -O /tmp/install.sh $url/install.sh  && sh /tmp/install.sh && source /etc/profile &> /dev/null

~老旧设备使用低版本wge安装

#作者私人http内测源
export url='http://t.jwsc.eu.org' && wget -q -O /tmp/install.sh $url/install.sh  && sh /tmp/install.sh && source /etc/profile &> /dev/null

~DOCKER环境下安装:

请参考 ShellClash_docker 一键脚本和镜像

本地安装:

如使用在线安装出现问题,请参考:本地安装ShellClash的教程 | Juewuy’s Blog 使用本地安装!

使用脚本:

安装完成管理脚本后,执行如下命令使用~

clash 		#进入对话脚本
clash -h 	#脚本帮助及说明
clash -u 	#卸载脚本
clash -t 	#测试模式运行
clash -s start 	#启动服务
clash -s stop 	#停止服务

运行时的额外依赖

大部分的设备/系统都已经预装了以下的大部分依赖,使用时如无影响可以无视之

curl/wget		必须		全部缺少时无法在线安装及更新,无法使用节点保存功能
iptables/nftables	重要		缺少时只能使用纯净模式
crontab			较低		缺少时无法启用定时任务功能
net-tools		极低		缺少时无法正常检测端口占用
ubus/iproute-doc	极低		缺少时无法正常获取本机host地址

GUI 版本

安装 clash for windows(其实应该叫 Clash GUI 的)

https://github.com/Fndroid/clash_for_windows_pkg/releases/

下载 Linux 版本的压缩包

wget https://github.com/Fndroid/clash_for_windows_pkg/releases/download/0.20.22/Clash.for.Windows-0.20.22-x64-linux.tar.gz

解压压缩包

//文件名自己换
tar -xvf Clash.for.Windows-0.20.22-x64-linux.tar.gz

重命名与移动

mv Clash\ for\ Windows-0.20.22-x64-linux clash
sudo mv clash /opt
cd /opt/clash

恰饭广告

虽然也没恰多少,bkcloud,有兴趣可以看看。

参考资料


如果本文帮助到了你,帮我点个广告可以咩(o′┏▽┓`o)


文章作者: Anubis
版权声明: 本博客所有文章除特別声明外,均采用 CC BY 4.0 许可协议。转载请注明来源 Anubis !
评论
  目录