簡介 · Clash文件

執行 Clash 服務

說明如何將 Clash 二進位檔與 config.yaml 部署為 systemd 服務,完成 daemon-reload、開機自動啟動、服務啟動與狀態檢查,並補充 Docker 映像檔的基本執行方式。

  • systemd
  • Docker
  • config.yaml
  • 守護處理程式
  • journalctl
簡介

Clash 需要在背景執行,但是目前 Golang 還沒有很好的守護處理程式實作,因此我們推薦使用第三方工具來建立 Clash 的守護處理程式。

systemd

使用下列命令將 Clash 二進位檔複製到 /usr/local/bin,並將設定檔複製到 /etc/clash

shell
cp clash /usr/local/bin
cp config.yaml /etc/clash/
cp Country.mmdb /etc/clash/

建立 systemd 設定檔 /etc/systemd/system/clash.service

ini
[Unit]
Description=Clash 守护进程, Go 语言实现的基于规则的代理.
After=network-online.target

[Service]
Type=simple
Restart=always
ExecStart=/usr/local/bin/clash -d /etc/clash

[Install]
WantedBy=multi-user.target

之後,您應該使用以下命令重新載入 systemd:

shell
systemctl daemon-reload

使用以下命令在系統啟動時啟動 Clash:

shell
systemctl enable clash

使用以下命令立即啟動 Clash:

shell
systemctl start clash

使用以下命令檢查 Clash 的執行狀況和日誌:

shell
systemctl status clash
journalctl -xe

本指南由 ktechmidas 貢獻(#754)。

Docker

本專案提供預先建置的 Clash 與 Clash Premium Docker 映像檔,因此在 Linux 上可使用 Docker Compose 部署 Clash。不過,在容器中執行 Clash Premium 仍是不被推薦的做法。

警告

由於 Mac 版 Docker 缺少主機網路和 TUN 支援,此設定無法在 macOS 上運作。

yaml
services:
  clash:
    image: ghcr.io/dreamacro/clash
    restart: always
    volumes:
      - ./config.yaml:/root/.config/clash/config.yaml:ro
      # - ./ui:/ui:ro # 仪表盘 Volume 映射
    ports:
      - "7890:7890"
      - "7891:7891"
      # - "8080:8080" # 外部控制 (RESTful API)
    network_mode: "bridge"
yaml
services:
  clash:
    image: ghcr.io/dreamacro/clash-premium
    restart: always
    volumes:
      - ./config.yaml:/root/.config/clash/config.yaml:ro
      # - ./ui:/ui:ro # 仪表盘 Volume 映射
    ports:
      - "7890:7890"
      - "7891:7891"
      # - "8080:8080" # 外部控制 (RESTful API)
    cap_add:
      - NET_ADMIN
    devices:
      - /dev/net/tun
    network_mode: "host"

將檔案儲存為 docker-compose.yaml,並把 config.yaml 放在同一個目錄中。

提示

在繼續操作之前,請參考您的平台關於時間同步的檔案——如果時間不同步,某些協議可能無法正常工作。

準備就緒後,執行以下命令以啟動 Clash:

shell
docker-compose up -d

您可以使用以下命令查看日誌:

shell
docker-compose logs

使用以下指令停止 Clash:

shell
docker-compose stop