概要 · 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