侧边栏壁纸
博主头像
HSM's Blog博主等级

hahahhahahahahah

  • 累计撰写 13 篇文章
  • 累计创建 1 个标签
  • 累计收到 1 条评论

目 录CONTENT

文章目录

Linux 下使用 Clash 科学上网

HSM
HSM
2023-10-25 / 1 评论 / 2 点赞 / 134 阅读 / 7235 字

安装 Clash

1. 下载当前操作系统与 CPU 架构对应的包文件,我这儿是 X86_64 平台下 CentOS7 所以对应的使用 clash-linux-amd64-v1.6.5.gz 包即可

sudo wget -O clash.gz https://github.com/Dreamacro/clash/releases/download/v1.6.5/clash-linux-amd64-v1.6.5.gz

2. 下载好后解压安装包中 clash 到 /usr/local/bin/ 目录下,并删除压缩包文件

sudo gzip -dc clash.gz > /usr/local/bin/clash
sudo chmod +x /usr/local/bin/clash
sudo rm -f clash.gz

3. 创建配置文件目录,并下载 MMDB 文件

sudo mkdir /etc/clash
sudo wget -O /etc/clash/Country.mmdb https://github.com/Dreamacro/maxmind-geoip/releases/download/20220812/Country.mmdb

4. 创建 systemd 脚本,脚本文件路径为 /etc/systemd/system/clash.service,内容如下:

[Unit]
Description=clash daemon

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

[Install]
WantedBy=multi-user.target

5. 重载 systemctl daemon

sudo systemctl daemon-reload

配置代理上网

1. 导入已有的科学上网订阅链接

sudo wget -O /etc/clash/config.yaml [你的订阅链接]

2. 设置系统代理,添加配置文件 /etc/profile.d/proxy.sh 并在其中写入如下内容:

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

3. 重载 /etc/profile 配置

source /etc/profile

4. 启动 clash 服务,并设置为开机自动启

sudo systemctl start clash.service
sudo systemctl enable clash.service

5. 测试 goolge.com 访问

sd

# curl google.com
<HTML><HEAD><meta http-equiv="content-type" content="text/html;charset=utf-8">
<TITLE>301 Moved</TITLE></HEAD><BODY>
<H1>301 Moved</H1>
The document has moved
<A HREF="http://www.google.com/">here</A>.
</BODY></HTML>

1.png

配置 web-ui

1. 克隆 clash-dashboard 项目到本地

git clone -b gh-pages --depth 1 https://github.com/Dreamacro/clash-dashboard /opt/clash-dashboard复制

2. 修改 clash 配置文件中 external-ui 的值为 /opt/clash-dashboard

检查/etc/clash/config.yaml中是否存在external-ui: 0.0.0.0:9090,如果不存在就插入这一行。

2.png

3. 重启 clash 服务

systemctl restart clash

4. 通过浏览器访问 localhost:9090/ui,其中 localhost 替换为 clash 部署服务器的 IP

配置定时更新订阅

使用如下脚本填写相关配置项目并放入 /etc/cron.weekly 目录下,每周自动更新订阅配置文件即可

#!/usr/bin/env bash
# 订阅链接地址
SUBSCRIBE="https://xxxx?clash=1"
# web-ui存放目录,留空则保持默认不修改
WEB_UI="/opt/clash-dashboard"
# API 端口,留空则保持默认不修改
CONTROLLER_API_PROT=""
# API 口令,留空则保持默认不修改
SECRET=""
CLASH_CONFIG="/etc/clash/config.yaml"
if [ -z "${SUBSCRIBE}" ]; then
    echo "Subscription address cannot be empty"
    exit 1
fi
systemctl stop clash
wget --no-proxy -O ${CLASH_CONFIG} ${SUBSCRIBE}
if [ -n "${WEB_UI}" ]; then
	# 设置配置文件路径
	config_file="/etc/clash/config.yaml"

	# 检查配置文件是否存在external-ui参数
	grep -q "^external-ui:" "$config_file"

	if [[ $? -eq 0 ]]; then
  	# 如果存在external-ui参数,则修改为指定路径
  	sed -i 's/^external-ui:.*/external-ui: \/opt\/clash-dashboard/' "$config_file"
	else
  	# 如果不存在external-ui参数,则添加新的配置行
 	 echo "external-ui: /opt/clash-dashboard" >> "$config_file"
fi

fi
if [ -n "${CONTROLLER_API_PROT}" ]; then
sed -i "s?^external-controller.*?external-controller: '0.0.0.0:${CONTROLLER_API_PROT}'?" ${CLASH_CONFIG}
fi
if [ -n "${SECRET}" ]; then
sed -i "s?^secret.*?secret: '${SECRET}'?" ${CLASH_CONFIG}
fi
systemctl start clash

上述脚本写入 /etc/cron.weekly/clash.sh 并配置好相关变量后,保存退出并赋予可执行权限

chmod 0755 /etc/cron.weekly/clash.sh复制

至此,Linux 下 clash 配置完成,可以愉快的上外网拉包了

2

评论区