安装 Clash
下载当前操作系统与 CPU 架构对应的包文件,我这儿是 X86_64 平台下 CentOS7 所以对应的使用 clash-linux-amd64-v1.6.5.gz 包即可
wget -O clash.gz https://github.com/Dreamacro/clash/releases/download/v1.6.5/clash-linux-amd64-v1.6.5.gz
下载好后解压安装包中 clash 到
/usr/local/bin/
目录下,并删除压缩包文件gzip -dc clash.gz > /usr/local/bin/clash chmod +x /usr/local/bin/clash rm -f clash.gz
创建配置文件目录,并下载 MMDB 文件
mkdir /etc/clash wget -O /etc/clash/Country.mmdb https://www.sub-speeder.com/client-download/Country.mmdb
创建
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
重载 systemctl daemon
systemctl daemon-reload
配置代理上网
导入已有的科学上网
订阅链接
,如果没有机场的话推荐使用WgetCloud实测用了一年稳的一逼wget -O /etc/clash/config.yaml [你的订阅链接]
设置系统代理,添加配置文件
/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"
重载
/etc/profile
配置source /etc/profile
启动
clash
服务,并设置为开机自动启systemctl start clash systemctl enable clash
测试 goolge.com 访问
# 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>
配置 web-ui
克隆 clash-dashboard 项目到本地
git clone -b gh-pages --depth 1 https://github.com/Dreamacro/clash-dashboard /opt/clash-dashboard
修改
clash
配置文件中external-ui
的值为/opt/clash-dashboard
sed -i "s/^#\{0,1\} \{0,1\}external-ui.*/external-ui: \/opt\/clash-dashboard/" /etc/clash/config.yaml
重启 clash 服务
systemctl restart clash
通过浏览器访问
localhost:9090/ui
,其中localhost
替换为 clash 部署服务器的 IP
配置定时更新订阅
使用如下脚本填写相关配置项目并放入 /etc/cron.weekly
目录下,每周自动更新订阅配置文件即可
#!/usr/bin/env bash
# 订阅链接地址
SUBSCRIBE=""
# web-ui存放目录,留空则保持默认不修改
WEB_UI=""
# 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
sed -i "s?^#\{0,1\} \{0,1\}external-ui.*?external-ui: ${WEB_UI}?" ${CLASH_CONFIG}
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 配置完成,可以愉快的上外网拉包了