20240211141946 Cloudflareでトンネルを作る

raspi4raspi5cloudflareserver

Raspberry Pi 4, 5, N100サーバーなどで実施。

注意

色々なサイトを参考にしたけど、変化が激しいので100%参考になることは無かった。おそらくこのメモもすぐに陳腐化すると思う。私はこの分野の専門ではないので、全然わからないで適当にやっている。

トンネルとは

トンネリングとは?| ネットワークにおけるトンネリング | Cloudflare

前提

cloudflareでアカウントを作る

Cloudflare | Web Performance & Security ここからサインアップしてアカウントを作る。

cloudflaredをインストールする

raspi4にcloudflaredをインストールする。 Downloads · Cloudflare Zero Trust docs から、 raspiならARM64をダウンロードしてインストール。intelでubuntuやdebianの場合 amd64 / x86-64 の方の.deb

$ sudo dpkg -i coudflared-linux-amd64.deb

sshのproxycommandとして使うので、wsl2(のubuntu) amd64/x86-64をダウンロードしてそちらにもインストールした。

専用ユーザーを作成

$ sudo groupadd --system cloudflare

$ sudo useradd --system \
--gid cloudflare \
--create-home \
--home-dir /var/lib/cloudflare \
--comment "Cloudflaire" \
cloudflare

sudo useradd --system \
--gid suzueli \
--create-home \
--home-dir /home/suzueli \
--comment "suzueli" \
suzueli

$ sudo passwd cloudflare
New password:
Retype new password:
passwd: password updated successfully

$ sudo visudo

cloudflare ALL=(ALL:ALL) ALL
を追加

kazbo@saipi4:~ $ su cloudflare
Password:
cloudflare@saipi4:/home/kazbo $ cd
cloudflare@saipi4:~ $ pwd
/var/lib/cloudflare

以降作業はこのユーザー(cloudflare)で実施

ログイン

cloudflare@saipi4:~ $ cloudflared login
Please open the following URL and log in with your Cloudflare account:

https://dash.cloudflare.com/argotunnel?aud=&callback=https%3A%2F%2Flogin.cloudflareaccess.org%...

Leave cloudflared running to download the cert automatically.
[URLをクリックして、使いたいドメインをクリック、承認を押す]
You have successfully logged in.
If you wish to copy your credentials to a server, they have been saved to:
/var/lib/cloudflare/.cloudflared/cert.pem

トンネルの作成

cloudflare@saipi4:~ $ cloudflared tunnel create saipi4(ここは自分が設定したい名前)
Tunnel credentials written to /var/lib/cloudflare/.cloudflared/******.json. cloudflared chose this file based on where your origin certificate was found. Keep this file secret. To revoke these credentials, delete the tunnel.

Created tunnel saipi4 with id *****

***** の所のtokenをメモしておく Cloudflare トンネルのtoken(private)

以上でトンネル作成の準備が完了。具体的な、何をトンネル経由でやりとりするか? の設定は、 ~/.cloudflared/config.yaml に書いていく。 例は以下