メインメニューを開く

差分

OpenVPNのインストール

6,356 バイト追加, 2019年12月27日 (金) 11:24
== VPNサーバー ==<syntaxhighlight lang="bash">pacman -S openvpnRaspberry piのArchlinux化をもくろみ、ArchlinuxでVPNサーバーを立ち上げようかと思っています。<br/syntaxhighlight>そこで、まずはVirtualBoxでOpenVPNを使用して、VPNサーバーを立ち上げてみました。
== 認証局サーバー ==
<syntaxhighlight lang="bash">pacman -S easy-rsa</syntaxhighlight>
VPNサーバーと認証局サーバーを分離している前提で、手順を上げています。<syntaxhighlight lang="bash"br/>cd /etc/easy-rsaeasyrsa init-pkiもし、一緒にしているなら上手いこと読み替えてください。
init-pki complete; you may now create a CA or requests.== 鍵の準備 ==Your newly created PKI dir is: /etc/easy-rsa/pkiVPNを使用するにあたり、認証局の設立やら共通鍵の作成やら鍵ペアの作成やら色々していきます。
=== 認証局の設立(認証局サーバー) ===
認証局にするサーバーに以下のコマンドを実行して「Easy-RSA」をインストールします。
{{bc|# pacman -S easy-rsa}}
easyrsa buildインストールしたら「easy-carsa」に移動しておきます。Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019{{bc|# cd /etc/easy-rsa}}
認証局の構築をしていきます。<br/>以下のコマンドで認証局の初期化を行います。{{hc|# easyrsa init-pki|init-pki complete; you may now create a CA or requests.Your newly created PKI dir is: /etc/easy-rsa/pki}} 完了したら、認証局の鍵ペアを生成します。<pre style="margin-bottom: 0; border-bottom:none; padding-bottom:0.8em;"># easyrsa build-ca</pre><pre style="margin-top: 0; border-top-style:dashed; padding-top: 0.8em;">Using SSL: opensslOpenSSL 1.1.1d 10 Sep 2019 Enter New CA Key Passphrase: [任意のパスフレーズを入力]Re-Enter New CA Key Passphrase: [任意のパスフレーズを入力]
Generating RSA private key, 2048 bit long modulus (2 primes)
...........................+++++
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:xxxxxxxx[任意の名前を入力] 
CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/etc/easy-rsa/pki/ca.crt</syntaxhighlight> CAの公開鍵をVPNサーバーに移行<syntaxhighlight lang="bash">cp /etc/easy-rsa/pki/ca.crt /tmpchown foo /tmp/ca.crtscp /tmp/ca.crt foo@hostname-of-openvpn-server:/tmp</syntaxhighlightpre>
== VPNサーバー ==作成された認証局の証明書をVPNサーバーに移します。<br/>証明書なのでまぁ適当に移せば良いかと思います。<syntaxhighlight lang="bash"br/>mv 下の例ではSCPを使用していますが…私はSFTPを使用しました。{{bc|# cp /etc/tmpeasy-rsa/pki/ca.crt /etc/openvpn/server/tmp# chown root:root foo /etctmp/openvpnca.crt$ scp /servertmp/ca.crt<foo@hostname-of-openvpn-server:/syntaxhighlight>tmp}}
<syntaxhighlight lang="bash">== VPN用の鍵の生成(VPNサーバー) ===まずは、ファイルの移動も考えて、以下のコマンドでOpenVPNをインストールします。{{bc|# pacman -S easy-rsa</syntaxhighlight>openvpn}}
<syntaxhighlight lang="bash">cd インストールが完了したら、認証局から受け取った証明書を適切な場所に移動させます。{{bc|# mv /tmp/ca.crt /etc/easy-rsaopenvpn/server/easyrsa init-pki# chown root:root /etc/openvpn/server/ca.crt}}
initVPNサーバー側にも「Easy-pki complete; you may now create a CA or requests.RSA」をインストールして、ディレクトリを移動します。Your newly created PKI dir is: {{bc|# pacman -S easy-rsa# cd /etc/easy-rsa/pki}}
認証局と同様に初期化します。
{{hc|easyrsa init-pki|init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/easy-rsa/pki}}
==== VPNサーバー用鍵ペアの生成 ====以下のコマンドで、VPNサーバー用の鍵ペアを生成します。<pre style="margin-bottom: 0; border-bottom:none; padding-bottom:0.8em;">easyrsa gen-req [サーバー名?] server nopass</pre><pre style="margin-top: 0; border-top-style:dashed; padding-top: 0.8em;">Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
Can't load /etc/easy-rsa/pki/.rnd into RNG
139780419302656:error:2406F079:random number generator:RAND_load_file:Cannot open file:crypto/rand/randfile.c:98:Filename=/etc/easy-rsa/pki/.rnd
..................................................+++++
.................................................................+++++
writing new private key to '/etc/easy-rsa/pki/private/servernameserver.key.TUQccMn1qp'
-----
You are about to be asked to enter information that will be incorporated
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [servernameserver]:yyyyyyyy[任意の名前を入力]
Keypair and certificate request completed. Your files are:
req: /etc/easy-rsa/pki/reqs/servernameserver.reqkey: /etc/easy-rsa/pki/private/servernameserver.key</pre>
生成されたファイルのうち、秘密鍵?を適切なディレクトリにコピーします。{{bc|# cp /etc/easy-rsa/pki/private/servernameserver.key /etc/openvpn/server/</syntaxhighlight>}}
<syntaxhighlight lang="bash">以下のコマンドで暗号通信用のパラメータファイルを生成します。{{bc|# openssl dhparam -out /etc/openvpn/server/dh.pem 2048</syntaxhighlight>}}
<syntaxhighlight lang="bash">以下のコマンドでHMAC共通鍵を生成します。{{bc|# openvpn --genkey --secret /etc/openvpn/server/ta.key</syntaxhighlight>}}
クライアントファイルの作成==== VPNクライアント用鍵ペアの作成 ====今度は、VPNクライアントで使用する鍵ペアを生成します。<br/>初期化してなかったら…VPNクライアント用の鍵ペアを生成するマシンはなんでも良いのですが…<syntaxhighlight lang="bash"br/>もし、別のマシンで初めて作成する場合は初期化を忘れない様にしてください。{{bc|# cd /etc/easy-rsa# easyrsa init-pki<# cd /etc/syntaxhighlight>easy-rsa}}
<syntaxhighlight lang="bash"># cd /etc/easy-rsarsa」ディレクトリにいることを確認して、以下のコマンドを実行します。<pre style="margin-bottom: 0; border-bottom:none; padding-bottom:0.8em;"># easyrsa gen-req client1 client nopass</pre><pre style="margin-top: 0; border-top-style:dashed; padding-top: 0.8em;">Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
Can't load /etc/easy-rsa/pki/.rnd into RNG
139780419302656:error:2406F079:random number generator:RAND_load_file:Cannot open file:crypto/rand/randfile.c:98:Filename=/etc/easy-rsa/pki/.rnd
..................................................+++++
.................................................................+++++
writing new private key to '/etc/easy-rsa/pki/private/client1client.key.TUQccMn1qp'
-----
You are about to be asked to enter information that will be incorporated
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [client1client]:yyyyyyyy[任意の名前を入力]
Keypair and certificate request completed. Your files are:
req: /etc/easy-rsa/pki/reqs/client1client.reqkey: /etc/easy-rsa/pki/private/client1client.key</syntaxhighlightpre生成されたファイルのうち、秘密鍵?を適切なディレクトリにコピーします。{{bc|# cp /etc/easy-rsa/pki/private/client.key /etc/openvpn/client/}}
<syntaxhighlight lang="bash">証明書署名要求ファイルを認証局サーバーに移動させます。前出同様にscpを使用していますが、SFTPなどでもOKです。{{bc|# cp /etc/easy-rsa/pki/reqs/*.req /tmp
# chown foo /tmp/*.req
$ scp /tmp/*.req foo@hostname-of-CA:/tmp</syntaxhighlight>}}
== 認証局サーバー = 公開鍵に署名する(認証局サーバー) ===VPNサーバー&クライアントのreqファイルに認証局の証明書で署名していきます。<syntaxhighlight lang="bash"br/># cd /etc/easy-rsa# easyrsa import-req /tmp/servername.req servernameUsing SSL: openssl OpenSSL 1.1.1d 10 Sep 2019(…上の文言、間違ってるかも…)
The request has been successfully imported with a short name of: servernameまずは以下のコマンドでディレクトリを移動します。You may now use this name to perform signing operations on this request.{{bc|# cd /etc/easy-rsa}}
==== VPNサーバー用の公開鍵に署名 ====以下のコマンドを実行して、reqファイルをインポートします。{{hc|# easyrsa import-req /tmp/client1server.req client1server|Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
The request has been successfully imported with a short name of: client1serverYou may now use this name to perform signing operations on this request.}}
次に署名済みの公開鍵を取り出します。<pre style="margin-bottom: 0; border-bottom:none; padding-bottom:0.8em;"># easyrsa sign-req server servernameserver</pre><pre style="margin-top: 0; border-top-style:dashed; padding-top: 0.8em;">Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
subject=
commonName = xxxxxxxx[鍵ペアを生成したときに設定した名前が表示されます]
Data Base Updated
Certificate created at: /etc/easy-rsa/pki/issued/servernameserver.crt</pre> ==== VPNクライアント用の公開鍵に署名 ====VPNサーバーと同様にクライアント用の公開鍵に署名していきます。{{hc|# easyrsa import-req /tmp/client.req client|Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019 The request has been successfully imported with a short name of: clientYou may now use this name to perform signing operations on this request.}}
<pre style="margin-bottom: 0; border-bottom:none; padding-bottom:0.8em;"># easyrsa sign-req client client1client</pre><pre style="margin-top: 0; border-top-style:dashed; padding-top: 0.8em;">Using SSL: openssl OpenSSL 1.1.1d 10 Sep 2019
subject=
commonName = yyyyyyyy[鍵ペアを生成したときに設定した名前が表示されます]
Data Base Updated
Certificate created at: /etc/easy-rsa/pki/issued/client1client.crt</syntaxhighlightpre>
認証局からVPNサーバーとクライアントに返却認証局から署名済みの公開鍵をVPNサーバーに返却します。<br/>まぁ適当に移してください。<syntaxhighlight lang="bash"br/>クライアント用も同様にVPNサーバーに移します。<br/>後で、クライアント用の設定ファイルを作成するので…{{bc|# cp /etc/easy-rsa/pki/issued/*.crt /tmp
# chown foo /tmp/*.crt
$ scp /tmp/*.crt foo@hostname-of-openvpn_server:/tmp</syntaxhighlight> == VPNサーバー ==<syntaxhighlight lang="bash"># mv /tmp/servername.crt /etc/openvpn/server/# chown root:root /etc/openvpn/server/servername.crt</syntaxhighlight>}}
== VPNクライアント ==ぶっちゃけどこでも構わないこれで、鍵関連の作業は終了です。<syntaxhighlight lang="bash"br/># mkdir /etc/easy-rsa/pki/signed# mv /tmp/client1.crt /etc/easy-rsa/pki/signed</syntaxhighlight>ここから、VPNの設定をしていきます。
== VPNサーバー設定ファイル VPNの設定 ==<syntaxhighlight lang="bash">cp == VPNサーバー ===以下のコマンドで、署名済み公開鍵を適切なディレクトリに移動します。{{bc|# mv /tmp/usrserver.crt /shareetc/openvpn/examplesserver/server.conf # chown root:root /etc/openvpn/server/server.conf<crt# mv /tmp/client.crt /etc/openvpn/client/# chown root:root /etc/openvpn/client/syntaxhighlight>client.crt}}
<syntaxhighlight lang="bash"># nano これまでの手順で以下のファイルが作成されているかと思います。{{bc|/etc/openvpn/server/ca.crt/etc/openvpn/server/dh.pem/etc/openvpn/server/server.crt/etc/openvpn/server/server.conf<key/etc/openvpn/client/client.crt/etc/syntaxhighlight>openvpn/client/client.key/etc/openvpn/server/ta.key}}
ポート番号<syntaxhighlight lang="text">port 1194</syntaxhighlight>== VPNサーバー設定ファイルの作成 ===以下のコマンドでサンプルファイルをコピーします<syntaxhighlight lang="text">port xxxx<{{bc|# cp /usr/share/openvpn/examples/server.conf /etc/openvpn/server/syntaxhighlight>server.conf}}
プロトコル(基本udpだけど、proxy越えしたい場合はtcp)以下のコマンドで設定ファイルを開いて編集していきます。<syntaxhighlight lang="text">;proto tcp{{hc|# nano /etc/openvpn/server/server.conf|proto udp</syntaxhighlight>port 1194 ←ポート番号を変更したい場合<syntaxhighlight lang="text">proto tcptcp ←プロトコルを変更したい場合(プロキシ認証越えの場合はtcp。ipv6の場合はudp6/tcp6);proto udp</syntaxhighlight>;explicit-exit-notify 1 ←プロトコルをtcpにした場合はこの行をコメントアウトca ca.crt ←認証局の証明書(ファイル名を変えている場合は修正)cert server.crt ←VPNサーバーの公開鍵(ファイル名を変えている場合は修正)key server.key # This file should be kept secret ←VPNサーバーの秘密鍵(ファイル名を変えている場合は修正)dh dh.pem ←暗号通信用のパラメータファイル(ファイル名を変えている場合は修正)tls-auth ta.key 0 ←HMAC共通鍵(ファイル名を変えている場合は修正)user nobody ←プロセスのユーザー:コメントアウトを外すgroup nobody ←プロセスのユーザーグループ:コメントアウトを外すcipher AES-256-CBC ←良くわからないけど、書いたほうが良いらしい…(デフォルトで有効になってるけど…)auth SHA512 ←追記tls-version-min 1.2 ←追記tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA:TLS-DHE-RSA-WITH-AES-128-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA ←追記
認証局の公開鍵(変える必要ないはず)push "dhcp-option DNS 8.8.8.8" ←追記:VPNクライアントで使用してほしいDNSサーバーのアドレスを指定<syntaxhighlight lang=push "textdhcp-option DNS 8.8.4.4" ←追記:VPNクライアントで使用してほしいDNSサーバーのアドレスを指定push ">ca caroute 192.168.56.0 255.255.255.crt</syntaxhighlight>0" ←追記:VPNクライアントがアクセスしたいローカルネットワークのアドレスとサブネットマスクを指定push "block-outside-dns" ←追記:DNSリーク対策<syntaxhighlight lang=push "textredirect-gateway def1">ca ca.crt</syntaxhighlight> ←追記:すべての通信をVPNサーバー経由で行う場合は設定client-to-client ←追記:VPNクライアントどうして通信したい場合はコメントアウトを外す
サーバーの公開鍵
<syntaxhighlight lang="text">cert server.crt</syntaxhighlight>
<syntaxhighlight lang="text">cert servername.crt</syntaxhighlight>
サーバーの(署名された)秘密鍵<syntaxhighlight lang="text">key server.key # This file should be kept secret</syntaxhighlight><syntaxhighlight lang="text">key servername.key # This file should be kept secret</syntaxhighlight>}}
暗号通信用のパラメータファイル設定が完了したら、以下のコマンドでサービスを起動します。<syntaxhighlight lang="text">dh dh2048.pem</syntaxhighlight>{{bc|# systemctl start openvpn-server@server}}<syntaxhighlight lang="text">dh dh.pem</syntaxhighlight>因みに…@の後ろの「server」は設定ファイル名です。
HMAC共通鍵(変えなくてもいいはず)=== VPNクライアント設定ファイル ===引き続き、VPNクライアント用の設定ファイルを作成していきます。<syntaxhighlight lang="text">tls-auth ta.key 0<br/syntaxhighlight><syntaxhighlight lang="text">tls-auth ta.key 0</syntaxhighlight>ただ…あ~だこ~だファイルを移すのは面倒なので、すべてを包含した設定ファイルを作成します。
プロセスのユーザーとグループ==== 「ovpngen」のインストール ====AURに「ovpngen」という、便利なツールがあります。<syntaxhighlight lang="text"br/>;user nobody;group nobodyこれを使用してVPNクライアント用の設定ファイルを作成していきます。<br/syntaxhighlight>まずは、以下のコマンドを使用してツールをゲットします。<syntaxhighlight lang="text"br/>user nobodygroup nobody<(バージョンは適宜最新のものに読み替えてください){{bc|# cd /etc/openvpn/client# wget https://github.com/graysky2/ovpngen/archive/syntaxhighlight>v1.28.tar.gz# tar -xvf v1.28.tar.gz# cd ovpngen-1.28# chmod +x ovpngen}}このツールはスクリプトなので、これでインストール完了です。
よくわからん!(変えなくてもいいはず)==== VPNクライアント用設定ファイルの生成 ====「ovpngen」を使用して、設定ファイルを作成します。<syntaxhighlight lang="text">cipher AES-256-CBC<br/syntaxhighlight>(ポート番号、プロトコルは適宜読み替えてください)<syntaxhighlight lang="text">cipher AES-256-CBC<{{bc|./ovpngen [VPNサーバーのIPアドレスまたはURL] /etc/openvpn/server/ca.crt /etc/openvpn/client/client.crt /etc/openvpn/client/client.key /etc/openvpn/server/syntaxhighlightta.key 1194 tcp >client.ovpn}}
以下追記<syntaxhighlight lang="text">auth SHA512tls-version-min 1.2tls-cipher TLS-DHE-RSA-WITH-AES-256-GCM-SHA384:TLS-DHE-RSA-WITH-AES-128-GCM-SHA256:TLS-DHE-RSA-WITH-AES-256-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-256-CBC-SHA:TLS-DHE-RSA-WITH-AES-128-CBC-SHA:TLS-DHE-RSA-WITH-CAMELLIA-128-CBC-SHA</syntaxhighlight>このファイルをVPNクライアントに持って行って、OpenVPNクライアントアプリに読み込ませればOKです。
ただ…ここで作成した「client.ovpn」ファイルの以下の部分は編集した方が良いかもです…。<br/>
(自信なし…)
== VPNクライアント設定ファイル ===== 「ovpngen」のインストール ===<syntaxhighlight lang="bash">cd ~</syntaxhighlight><syntaxhighlight lang="bash">wget https://github.com{{hc|# nano /graysky2etc/ovpngenopenvpn/archiveclient/v1client.28.tar.gz</syntaxhighlight>ovpn|<syntaxhighlight lang="bash">tar cipher AES-xvf v1.28.tar.gz</syntaxhighlight>256-CBC ←コメントアウトを外す<syntaxhighlight lang="bash">cd ovpngen-1.28</syntaxhighlight>auth SHA512 ←コメントアウトを外す<syntaxhighlight lang="bash">chmod +x ovpngen</syntaxhighlight>proto xxxx ←VPNサーバーの設定に合わせる}}
=== VPNクライアント用設定ファイルの生成 =参考サイト ==<syntaxhighlight lang="bash">.[https:/ovpngen [VPNサーバーのIPアドレスまたはURL] /etcwiki.archlinux.jp/openvpnindex.php/server/caOpenVPN#.E3.82.AF.E3.83.A9.E3.82.A4.E3.82.A2.E3.83.B3.E3.83.88.E3.81.A8.E3.82.AF.E3.83.A9.E3.82.A4.E3.82.A2.E3.83.B3.E3.83.88.E3.81.AE_LAN_.E3.82.92.E6.8E.A5.E7.B6.crt /etc/easy9A OpenVPN -rsaArchWiki]<br/pki>[https:/signed/client1wiki.archlinux.crt jp/etcindex.php/easyEasy-rsaRSA Easy-RSA - ArchWiki]<br/pki>[https:/private/client1github.key com/etcgraysky2/openvpnovpngen GitHub - graysky2/server/ta.key 1194 tcp > client.conf</syntaxhighlight>ovpngen: Generate an OpenVPN Connect private tunnel profile in the unified format]
このファイルをクライアントに持って行って接続???[[Category:ArchLinux]][[Category:OpenVPN]]