ラズパイをVPNサーバーにしよう

カフェや駅などで公衆wifiを利用するときセキリュティが不安!そんなときに家に転がっているラズパイを使えばVPNを構築することができます。

材料

  • ラズパイ
  • pivpn
  • mydns
  • ポート開放

GitHub - pivpn/pivpn: The Simplest VPN installer, designed for Raspberry Pi

下準備

DDNSを取得する

固定IPがない一般の家庭なのでDDNSを取得します

Free Dynamic DNS (DDNS) for Home Server and VPS etc | MyDNS.JP

ラズパイからMyDNSのサーバーにIPを知らせてあげる

今回はcrontabを用います。systemd timerを使ってもいいです。

crontab -e
0 1 * * * curl -u [masterID]:[password] https://ipv4.mydns.jp/login.html

pivpnのインストール

curl -L https://install.pivpn.dev | bash

・PiVPN Automated Installer
f:id:na2kaze:20200203034742j:plain

ウェルカム画面です。

・Static IP Needed
f:id:na2kaze:20200203034739j:plain

「The PiVPN is a SERVER so it needs a STATIC IP ADDRESS to function properly.」
「PiVPNはサーバーなので適切に機能させるためには静的なIPが必要です。」

「In the next section, you can choose to use your current network settings (DHCP) or to manually edit them.」
「次のセクションでは、現在のネットワーク設定(DHCP)を使用するか、手動で編集するかを選択できます。」

DHCP Reservation
f:id:na2kaze:20200203034746j:plain

「Are you Using DHCP Reservation on your Router/HDCP server?」
ルーター/ HDCPサーバーでDHCP予約を使用していますか?」

「These are your current Network Settings:」
「これらは現在のネットワーク設定です:」

「Yes: Keep using DHCP reservation」
「Yes: DHCP予約を使用し続けます」

「No: Setup static IP address」
「No: 静的IPアドレスを設定します」

「Don't Know what DHCP Reservation is ? Answer No.」
DHCP予約を知らない場合はNoを選択してください」

・Local Users
f:id:na2kaze:20200203034806j:plain

「Choose a local user that will hold your ovpn configurations.」
「ovpn構成を保持するローカルユーザーを選択します。」

・Choose A User
f:id:na2kaze:20200203034753j:plain

ユーザーを選択します。

・Installation Mode
f:id:na2kaze:20200203034803j:plain

「WireGuard is a new kind of VPN that provides near-istantaneous connection speed, high performance, modern cryptography.」
「WireGuardは、ほぼ瞬時の接続速度、高性能、最新の暗号化を提供する新しい種類のVPNです。」

「It's the recommended choice expecially if you use mobile devices where Wiregard is easier on battery than OpenVPN.」
「Wiregardのバッテリー消費がOpenVPNよりも簡単なモバイルデバイスを使用する場合は、特に推奨される選択肢です。」

OpenVPN is still available if you need the traditional, flexible, trussted VPN protocol. Or if you need features like TCP and custom search domain.」
OpenVPNは、従来の柔軟なトラステッドVPNプロトコルが必要な場合でも引き続き利用できます。 または、TCPやカスタム検索ドメインなどの機能が必要な場合。」

「Choose a VPN (press space to select)」
VPNを選択してください(スペースで選択)」

WireGuardは最近ホットなVPNみたいです。今回はOpenVPNを選択します。

・Protocol
f:id:na2kaze:20200203034756j:plainf:id:na2kaze:20200203034759j:plain

「Choose a protocol (press space to select). Please only choose TCP if you know why you need TCP
プロトコルを選択します(スペースにて選択)。 TCPが必要な理由がわかっている場合にのみ、TCPを選択してください。」

VPNUDPのほうがむいているのでUDPを選択。

・Default openvpn Port
f:id:na2kaze:20200203034749j:plain

「You can modify the default openvpn port.」
「デフォルトのopenvpnポートを変更できます。」

「Enter a new value or hit 'Enter' to retain the default.」
「新しい値を入力するか、「Enter」を押してデフォルトを保持します。」

セキリュティを優先する場合は違うポート番号に変更できます。

・Select the DNS Provider
[evernote:87b476ac36351586e487938a8119e077 アップロード中]

「Select the DNS Provider for your VPN Clients (press space to select). To use your own, select custom.」
VPNクライアントのDNSプロバイダーを選択します(スペースを押して選択)。 自身のものを使用するには、カスタムを選択します。」

「In case you have a local resolver running, i.e unbound, select "PiVPN-is-local-DNS" and make sure your resolver is listening on 」
「ローカルリゾルバが実行されている場合(バインドされていない場合)、「PiVPN-is-local-DNS」を選択し、リゾルバがリッスンしていることを確認します。」

無難にGoogleを選択します。

・CustomSearchDomain
f:id:na2kaze:20200203034812j:plain

「Would you like to add a custom search domain?(This is only for advanced user who have their own domain)」
「カスタム検索ドメインを追加しますか?(これは、独自のドメインを持つ上級ユーザー専用です)」

いいえを選択。

・Public IP or DNS
f:id:na2kaze:20200203034810j:plain

「Will clients use a public IP or DNSname to connect to your server ? (press space to select)」
「クライアントはパブリックIPまたはDNS名どちらを使用してサーバーに接続しますか? (スペースを押して選択)」

固定IPを持ってないのでDNS Entryを選択します。

・PiVPN Setup
f:id:na2kaze:20200203034820j:plain

「What is the public DNS name of this Server?」
「 このサーバーのパブリックDNS名は何ですか」

MyDNSなどで取得しココに記載します。
例:abcde123.mydns.jp

・Installation mode
f:id:na2kaze:20200203034816j:plain

OpenVPN 2.4 can take advantage of elliptic Curves to provide higher connection speed and improved security over RSA, while keeping smaller certificates.」
OpenVPN 2.4は楕円曲線を利用して、RSAよりも高速な接続速度とセキュリティの向上を提供しながら、より小さい証明書を保持できます。」

「Moreover, the 'tls-crypt' directive encrypts the certificates being used while authenticating, increasing privacy.」
「さらに、「tls-crypt」ディレクティブは、認証中に使用されている証明書を暗号化し、プライバシーを向上させます。」

「If your clients do run OpenVPN 2.4 or later you can enable these features, otherwise choose 'No' for best compatibility.」
「クライアントがOpenVPN 2.4以降を実行している場合、これらの機能を有効にすることができます。そうでない場合は、互換性を高めるために「いいえ」を選択します。」

セキリュティを高めてくれそうです。古いバージョンを利用していないので「はい」を選択します。

・ECDSA certificate size
f:id:na2kaze:20200203034826j:plain

「Choose the desired size of your certificate (press space to select):」
「証明書の希望のサイズを選択します(スペースを押して選択します):」

「This is an certificate that will be generated on your system.」
「これは、システムで生成される証明書です。」

「The larger the certificate, the more time this will take.」
「証明書が大きいほど、時間がかかります。」

「For most applications, it is recommended to use 256 bits.」
「ほとんどのアプリケーションでは、256ビットを使用することをお勧めします。」

「You can increase the number of bits if you care about, however, consider that 256 bits are already as secure as 3072 bit RSA.」
「気にするならビット数を増やすことができますが、256ビットは3072ビットRSAと同じくらい安全であると考えてください。」

おすすめの256を選択します。

・ServerInfomation
f:id:na2kaze:20200203034824j:plain

「The server key and HMAC key will now be generated」
「サーバーキーとHMACキーが生成されます」

・Unattended Upgrades
f:id:na2kaze:20200203034832j:plain

「Since this server will have at least one port open to the internet, it is recommended you enable unattended-upgrades.」
「このサーバーにはインターネットに対して少なくとも1つのポートが開いているため、無人アップグレードを有効にすることをお勧めします。」

「This feature will check daily for security package updates only and apply them ehen necessary.」
「この機能は、セキュリティパッケージの更新のみを毎日チェックし、必要に応じて適用します。」

「It wll NOT automatically reboot the server so to fully applysome updates you should periodically reboot.」
「サーバーを自動的に再起動することはないため、更新を完全に適用するには定期的に再起動する必要があります。」

f:id:na2kaze:20200203034839j:plain

「Do you want to enable unattended upgrades of security patches to this server?」
「このサーバーへのセキュリティパッチの無人アップグレードを有効にしますか?」

定期的に更新をかけられるような律儀な人間でないので有効にしておきます。

・InstallationComplete!
f:id:na2kaze:20200203034829j:plain

「Now run 'pivpn add' to create the ovpn profiles.」
「「pivpn add」を実行して、ovpnプロファイルを作成します。」

「Run 'pivpn help' to see what else you can do!」
「「pivpn help」を実行して、他にできることを確認してください!」

「If you run into any issue, please read all our documentation carefully.」
「問題が発生した場合は、すべてのドキュメントを注意深くお読みください。」

「All incomplete posts or bug reports will be ignored or deleted.」
「不完全な投稿またはバグレポートはすべて無視または削除されます。」

「Thank you for using PiVPN.」
「PiVPNをご利用いただきありがとうございます。」

・Reboot
f:id:na2kaze:20200203034836j:plain

「It is strongly recommended you reboot after installation. Would you like to reboot now?」
「インストール後に再起動することを強くお勧めします。 今すぐ再起動しますか?」

リブートしておきましょう。「はい」を選択します。

ポート開放

設定したポート(デフォルトはUDP1194)を開放する。
NECの場合はポートマッピング設定からできます。

仕上げ

後はpyvpn addをして生成されたファイルを使いたいデバイスに送ってopenvpnに読み込ませれば接続できるはず。