Ccmmutty logo
Commutty IT
9 min read

Ubuntu 22.04 LTS上でopenvpnサーバーを立ち上げる流れ

https://cdn.magicode.io/media/notebox/609cddb5-cf6f-4e00-a0be-563d8ec8df41.jpeg

目次

概要

openVPNサーバーの立ち上げについての解説です。基本は公式のHow TOに沿って進めますが、特に自分が躓いたところを補足する感じで解説しています。

参考

本記事を書いている途中に見つけてしまった記事やHowtoを読んでいた時に参考になった記事です。 https://qiita.com/makisyu/items/2ac579c0e169970b1609 https://atmarkit.itmedia.co.jp/ait/articles/1603/18/news009_2.html

目標

openVPNサーバーの目指す設定ですが、以下が実行できるようにしていきます。 こうすれば少なくとも個人使用の内は、vpnに登録しているクライアント(自宅PC、モバイル、サーバー)同士で所属ネットワーク問わず通信することができますね。
  • 公開鍵認証
  • ルーティング
  • tcp接続
  • クライアントへ固定IPの振り分け
  • クライアント同士の接続 (client1, client2, server)
  • クライアントのサブネットの設定

インストール

オススメは各OSのパッケージ管理ツール(apt,yumなど)でインストールすると楽です。バイナリは日本語コミュニティOPENVPNのサイトから取得できます。
ビルド時の./configureで
configure: error: lzo enabled but missing
など足りていないモジュールが提示されるのでそれぞれ調べて入れます。上記のライブラリは以下のパッケージのインストールで解決しました。
sudo apt-get install libssl-dev liblzo2-dev libpam0g-dev
そして、パッケージ管理ツールとビルドインストールとでドキュメントや設定ファイルの場所が変わるので注意です。 とくに設定ファイルを入れる/etc/openvpn/が存在しているかチェックすべきです。(パッケージ管理ツール経由だと確認)

easy-rsaを用いたPKI(公開鍵基盤)の構成

公開鍵認証でVPNサーバーに接続するためにはPKIを設定する必要がありますが、ルート証明書はオレオレ認証局のものにします。そのためにopenvpnのHowtoで利用されているのがeasy-rsaです。
しかしHowtoにはビルドファイルの中にあるとか、/usr/share/doc/packages/openvpnか/usr/share/doc/openvpn-2.0にあるとか書いてありますがUbuntu22.04のaptでインストールしたopenvpnではそのような場所にはeasy-rsaは存在しませんでした。
本記事ではeasy-rsaを別にインストールして認証局を立ち上げます。

easy-rsaのダウンロード

easy-rsaからリポジトリをクローンすれば easy-rsaはシェルで構成されています。 今回使うのはeasy-rsa/easy-rsa3/easy-rsaというシェルスクリプトです。

証明書系手順

  1. 認証局の初期化&ルート証明書の作成
  2. サーバー証明書の作成
  3. クライアント証明書の作成
  4. DH鍵の作成
  5. キーペアの確認
  6. キーペアの移送
https://github.com/OpenVPN/easy-rsa/blob/master/README.quickstart.md に沿って実行していくべきです。
肝心なことは同一の認証局のキーペアからサーバーやクライアントの証明書を発行していくことでPKIが成り立つということです。
エンティティのキーペアの作成には複数のコマンドがありますが、本記事の目的を達成する上では
build-client-full <file_name_base> [ cmd-opts ]
build-server-full <file_name_base> [ cmd-opts ]
この2つを使うのが簡単です。

認証局の初期化(コマンド例)

今回は簡易的にするため、鍵ファイルにパスフレーズは付与しませんのでnopassオプションをつけます。
./easyrsa init-pki
./easyrsa build-ca nopass
証明書がpki/ca.crt 秘密鍵がpki/private/ca.keyに書き込まれているはずです。

サーバー証明書の作成(コマンド例)

./easyrsa build-server-full server1 nopass
証明書がpki/issued/server1.crt 秘密鍵がpki/private/server1.keyに書き込まれているはずです。 server1.crtとserver1.keyがvpnサーバーの/etc/openvpn/server以下に配置されます。

クライアント証明書の作成(コマンド例)

./easyrsa build-server-full client1 nopass
証明書がpki/issued/client1.crt 秘密鍵がpki/private/server1.keyに書き込まれているはずです。 client1.crtとclient1.keyがvpnサーバーの/etc/openvpn/client以下に配置されます。 今回はクライアント間通信もしたいので、client2という名目で証明書をもう一つ作成します。

DH鍵の作成(コマンド例)

Diffie Hellman鍵共有のための鍵です。サーバー側に配置します。
./easyrsa gen-dh
pki/dh.pemというファイルが生成されます。

TLS鍵の作成 (コマンド例)

TLSの共有鍵認証のための鍵です。サーバーとクライアントの両方に配置します。
openvpn --genkey tls-auth PATHTO/ta.key
任意の場所にta.keyが生成されます。

作成したキーペアの場所

こんな風に配置されています。
USERNAME:/PATHTO/easy-rsa/easyrsa3$ ls pki/issued/
client1.crt  client2.crt  server1.crt
USERNAME:/PATHTO/easy-rsa/easyrsa3$ ls pki/private/
ca.key  client1.key  client2.key  server1.key
USERNAME:PATHTO/easy-rsa/easyrsa3$ ls pki/
... ca.cart dh.pem

移送

安全な方法で鍵をサーバーやクライアントに配置してください。 これに関してはHow Toの表がわかりやすいです。
サーバーとクライアントにopenvpn自体がインストールされていることを前提として
サーバーの場合は/etc/openvpn/server
クライアントの場合は/etc/openvpn/client
の場所に表に従ってファイルを配置します。
サーバーマシンの/etc/openvpn/serverの例
├── client
├── server
│   ├── certs
│   │   ├── ca.crt
│   │   ├── dh.pem
│   │   ├── server.crt
│   │   ├── server.key
│   │   └── ta.key
│   └── server.conf
└── update-resolv-conf

クライアントマシンの/etc/openvpn/clientの例
├── client
│   ├── ca.crt
│   ├── client.conf
│   ├── client1.crt
│   └── client1.key
│   └── ta.key
├── server
└── update-resolv-conf
*.confは後から配置します。

設定ファイルの作成

こちらのserver.confとclient.confを参考にしてください。

サーバー側

server.confを編集します。 {}で囲った所を配置した鍵のパスに変えてください。絶対パスのほうがおすすめです。
鍵・証明書の設定
# SSL/TLS root certificate (ca), certificate
# (cert), and private key (key).  Each client
# and the server must have their own cert and
# key file.  The server and all clients will
# use the same ca file.
#
# See the "easy-rsa" directory for a series
# of scripts for generating RSA certificates
# and private keys.  Remember to use
# a unique Common Name for the server
# and each of the client certificates.
#
# Any X509 key management system can be used.
# OpenVPN can also use a PKCS #12 formatted key file
# (see "pkcs12" directive in man page).
ca {ca.crt}
cert {server.crt}
key {server.key}  # This file should be kept secret

# Diffie hellman parameters.
# Generate your own with:
#   openssl dhparam -out dh2048.pem 2048
dh {dh2048.pem}
ta.keyの設定 サーバーは第2引数が0
# For extra security beyond that provided
# by SSL/TLS, create an "HMAC firewall"
# to help block DoS attacks and UDP port flooding.
#
# Generate with:
#   openvpn --genkey tls-auth ta.key
#
# The server and each client must have
# a copy of this key.
# The second parameter should be '0'
# on the server and '1' on the clients.
tls-auth {ta.key} 0 # This file is secret

クライアント側

client.confを編集します。my-server-1をホスト名かipアドレスに変えてください。
ホストの設定
# The hostname/IP and port of the server.
# You can have multiple remote entries
# to load balance between the servers.
remote {my-server-1} 1194
{}内を配置したキーペアの相対パスに指定してください。
鍵・証明書の設定
# SSL/TLS parms.
# See the server config file for more
# description.  It's best to use
# a separate .crt/.key file pair
# for each client.  A single ca
# file can be used for all clients.
ca {ca.crt}
cert {client.crt}
key {client.key}
ta.keyの設定 クライアントは第2引数が1
# Verify server certificate by checking that the
# certificate has the correct key usage set.
# This is an important precaution to protect against
# a potential attack discussed here:
#  http://openvpn.net/howto.html#mitm
#
# To use this feature, you will need to generate
# your server certificates with the keyUsage set to
#   digitalSignature, keyEncipherment
# and the extendedKeyUsage to
#   serverAuth
# EasyRSA can do this for you.
remote-cert-tls server

# If a tls-auth key is used on the server
# then every client must also have the key.
tls-auth {ta.key} 1

openvpnの起動

サーバー側

sudo openvpn PATHTO/server.conf

クライアント側

sudo openvpn PATHTO/client.conf

接続の検証

サブネットの設定が以下のようになっている場合、サーバーには10.8.0.1でアクセスできます。 読者の使用ネットワークでサブネットに10.8.0.0/24を使用していると接続できないので、そこは適宜設定を変更してください。
server 10.8.0.0 255.255.255.0
接続例
USERNAME:~$ ping 10.8.0.1
PING 10.8.0.1 (10.8.0.1) 56(84) bytes of data.
64 bytes from 10.8.0.1: icmp_seq=1 ttl=64 time=9.53 ms
64 bytes from 10.8.0.1: icmp_seq=2 ttl=64 time=9.98 ms
64 bytes from 10.8.0.1: icmp_seq=3 ttl=64 time=9.98 ms
64 bytes from 10.8.0.1: icmp_seq=4 ttl=64 time=10.2 ms
64 bytes from 10.8.0.1: icmp_seq=5 ttl=64 time=10.2 ms

ルーティングは後日

サービス化は後日

クライアントは続編

Discussion

コメントにはログインが必要です。