安装clash

安装最新的clash for linux最新版本页面

cd /usr/local
wget https://github.com/Dreamacro/clash/releases/download/v1.18.0/clash-linux-amd64-v1.18.0.gz

解压到当前目录

gzip -d clash-linux-amd64-v1.18.0.gz

重命名为clash

mv clash-linux-amd64-v1.18.0 clash

给与权限

chmod u+x clash

安装Country.mmdb (这个数据可以考虑下载最新的替换)

mkdir -p /root/.config/clash && cd /root/.config/clash
wget https://github.com/Dreamacro/maxmind-geoip/releases/download/20230812/Country.mmdb

配置文件

在目录/root/.config/clash下新建config.yaml
以下是示例,参考即可

# HTTP 代理端口
port: 7890

# SOCKS5 代理端口
socks-port: 7891

# Linux 和 macOS 的 redir 代理端口
redir-port: 7892

# 允许局域网的连接
allow-lan: true

# 规则模式:Rule(规则) / Global(全局代理)/ Direct(全局直连)
mode: rule

# 设置日志输出级别 (默认级别:silent,即不输出任何内容,以避免因日志内容过大而导致程序内存溢出)。
# 5 个级别:silent / info / warning / error / debug。级别越高日志输出量越大,越倾向于调试,若需要请自行开启。
log-level: silent
# Clash 的 RESTful API
external-controller: '0.0.0.0:9090'
secret: '123456'
external-ui: /root/clash-dashboard

# RESTful API 的口令
secret: '12345678' # 密码可以设置简单些,也可以不用设置

# 您可以将静态网页资源(如 clash-dashboard)放置在一个目录中,clash 将会服务于 `RESTful API/ui`
# 参数应填写配置目录的相对路径或绝对路径。
# external-ui: folder

设置外部控制ui

我们的clash安装在Linux,不是图形化操作界面,很难更换节点。配置好ui页面就可以通过外部网页访问并控制。

cd /root
git clone https://github.com/Dreamacro/clash-dashboard.git
cd clash-dashboard
git checkout -b gh-pages origin/gh-pages

网页访问http://主机ip:9090/ui,选择想要的节点

新开终端窗口,设置http代理

export https_proxy=http://127.0.0.1:7890 
export http_proxy=http://127.0.0.1:7890 
# 或
export  all_proxy=socks5://127.0.0.1:7890

测试
curl ifconfig.me

我的配置示例:

mixed-port: 7890
mode: rule
proxies:
  - name: "snell"
    type: snell
    server: 1.1.1.1
    port: 11111
    psk: 1233211234567
    obfs-opts:
        mode: tls
proxy-groups:
  - name: Proxy
    type: select
    proxies:
      - snell

rules:
  - DOMAIN-SUFFIX,github.com,Proxy
  - DOMAIN-SUFFIX,google.com,Proxy
  - DOMAIN-SUFFIX,openai.com,Proxy
  # 国内网站
  - DOMAIN-SUFFIX,cn,DIRECT
  - DOMAIN-KEYWORD,-cn,DIRECT
  # 最终规则
  - GEOIP,CN,DIRECT
  - MATCH,Proxy

配置开机启动

添加启动信息
sudo vi /etc/systemd/system/clash.service

加入以下内容

[Unit]
Description=Clash daemon, A rule-based proxy in Go.
[Service]
Type=simple
User=root
ExecStart=/root/clash/clash -d /root/.config/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
开机启动
sudo systemctl enable clash
禁用开机启动
sudo systemctl disable clash
日志
sudo journalctl -xe

永久设置代理

vi /etc/profile
export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7890

如果需临时取消代理
unset http_proxy https_proxy all_proxy

其它...

测试代理正常运行后,建议修改配置文件中的日志输出级别,即修改info为silent,表示不输出任何内容,防止日志内容过大导致程序内存溢出。
使用腾讯云主机跑clash等敏感程序,最好将内置的监听删除

bash /usr/local/qcloud/YunJing/uninst.sh
bash /usr/local/qcloud/stargate/admin/uninstall.sh
bash /usr/local/qcloud/monitor/barad/admin/uninstall.sh
systemctl stop tat_agent
systemctl disable tat_agent
rm -f /etc/systemd/system/tat_agent.service

# 自行删除 Cron 中残留的定时任务
crontab -e

# 检查
ps -ef | grep agent