Clash部署Linux服务器

为了能在墙内的服务器上用上ChatGPT,需要在服务器上订阅机场,一直是在PC电脑上用客户端,自己在服务器上搭建clash的时候也费了一些心思和时间,但是最终也是成功,所以记录下方便以后再使用。

准备工作

  • 创建目录

    1
    mkdir /home/$USER/clash
  • 获取源码

    1
    2
    3
    4
    5
    6
    7
    8
    # 进入指定目录
    cd /tmp
    # 获取文件
    wget https://github.com/Dreamacro/clash/releases/download/v1.14.0/clash-linux-amd64-v1.14.0.gz
    # 解压文件
    gzip -zxvf clash-linux-amd64-v1.14.0.gz
    # 重命名
    mv clash-linux-amd64-v1.14.0.gz /home/$USER/clash
  • 下载配置文件

    1
    wget -O config.yaml "配置文件的url"
  • 启动clash

    1
    2
    3
    cd /home/$USER/clash
    chmod +x clash
    ./clash -d .

UI控制页面

设置代理

  • 临时代理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    # 启用临时代理
    export http_proxy=127.0.0.1:7890
    export http_proxys=127.0.0.1:7890

    # 可以通过cat $http_proxy查看是否启用了临时代理

    # 删除临时代理
    unset http_proxy
    unset http_proxys
  • 永久代理

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    # 为了不每次都要设置临时代理
    vi ~/.bashrc

    # 加入如下内容
    export https_proxy=http://127.0.0.1:7890 http_proxy=http://127.0.0.1:7890 all_proxy=socks5://127.0.0.1:7891

    # 更新
    source ~/.bashrc

    # 临时取消代理
    unset http_proxy https_proxy all_proxy

    设置开机自启动

  • 新增系统服务:/etc/systemd/system/clash.service

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    [Unit]
    Description=Clash service
    After=network.target

    [Service]
    Type=simple
    User=root
    ExecStart=/home/lemonwan/clash/clash -d /home/lemonwan/clash/
    Restart=on-failure
    RestartPreventExitStatus=23

    [Install]
    WantedBy=multi-user.target
  • 重载服务

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    # 重新加载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

    其他文件

  • 腾讯云主机:需要将内置的监听删除

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    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

    参考文档

    安装clash-for-linux并订阅节点教程