public_notes/content/20240211141946 Raspi4 Cloudflareでトンネルを作る.md
2024-02-12 23:20:09 +09:00

82 lines
3.1 KiB
Markdown
Executable file

# 20240211141946 Raspi4 Cloudflareでトンネルを作る
#raspi #cloudflare #server
## 注意
色々なサイトを参考にしたけど、変化が激しいので100%参考になることは無かった。おそらくこのメモもすぐに陳腐化すると思う。私はこの分野の専門ではないので、全然わからないで適当にやっている。
## トンネルとは
[トンネリングとは?| ネットワークにおけるトンネリング | Cloudflare](https://www.cloudflare.com/ja-jp/learning/network-layer/what-is-tunneling/)
## 前提
- ドメインを持っている
- ネームサーバーをcloudflareに移管できる
- ブラウザでがんばってやろうとしたが挫折。 cliの方法を試す [Create a locally-managed tunnel (CLI) · Cloudflare Zero Trust docs](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/get-started/create-local-tunnel/)
## cloudflareでアカウントを作る
[Cloudflare | Web Performance & Security](https://dash.cloudflare.com/sign-up)
ここからサインアップしてアカウントを作る。
## cloudflaredをインストールする
raspi4にcloudflaredをインストールする。
[Downloads · Cloudflare Zero Trust docs](https://developers.cloudflare.com/cloudflare-one/connections/connect-networks/downloads/) から、 raspi4ならARM64をダウンロードしてインストール
```
$ sudo dpkg -i coudflared-linux-amd64.deb
```
sshのproxycommandとして使うので、wsl2(のubuntu) amd64/x86-64をダウンロードしてそちらにもインストールした。
### 専用ユーザーを作成
raspiで
```
$ sudo groupadd --system cloudflare
$ sudo useradd --system \
--gid cloudflare \
--create-home \
--home-dir /var/lib/cloudflare \
--comment "Cloudflaire" \
cloudflare
$ 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をメモしておく [[20240212183159 Cloudflare トンネルのtoken]]