Raspberry_Pi4_ _openvpn - yokohama/oreshic-record GitHub Wiki

Raspberry Pi4 + openvpn

Date: 2023-07-19 16:35:43

珟圚調査䞭のissue

vpnの䞭からブラりザでホヌムペヌゞを芋ようずするず、httpsが赀くなったり信頌されおいないず蚀われるこずがある。iptablesのnatが原因か調査䞭。その他は問題なし。

ネットワヌク構成図

物理構成は以䞋のような感じ。

䞊蚘の物理構成を倉えずにopenvpnを䜿甚しお、以䞋のような論理ネットワヌク仮想LANを远加で構成しおvpnclientのノヌトPCをLANネットワヌクにアクセス可胜にしたす。

今回䜿甚するVPNの接続方法は、opneVPNを䜿甚したす。他には、L2TPずいうものもありたす。openVPNはサヌバヌずクラむアントお互いに蚌明曞を䜿甚した認蚌が可胜ずなりたす。蚌明曞が必芁ずなるので蚌明曞の認蚌局(CA)も必芁ずなり、今回CAはopnevpnが開発しおいる、「easy-rsa」を䜿甚したす。

項目 OpenVPN L2TP
サマリヌ セキュアなリモヌトアクセスを提䟛するVPNプロトコル レむダヌ2トンネリングプロトコル
利甚シヌン むンタヌネット経由での安党なリモヌトアクセス ファむル共有、リモヌトアクセス、VPNアクセス
認蚌方法 ナヌザヌ名ずパスワヌド、蚌明曞、共有鍵 ナヌザヌ名ずパスワヌド、プリシェアヌドキヌ
セキュリティ匷床 高い 暙準
OSI 7階局のレむダヌ レむダヌ3ネットワヌク局 レむダヌ2デヌタリンク局
よく䞀緒に利甚されるプロトコル TLS (Transport Layer Security) IPsec (Internet Protocol Security)

配備するファむル

VPNサヌバヌずクラむアントずで以䞋のファむルを配備しお、VPN接続の認蚌を蚌明曞ベヌスでおこなう。

サヌバヌ偎のファむル

名前 ファむル名 説明
CA蚌明曞 ca.crt 接続しおきたクラむアントの蚌明曞の正圓性を蚌明しおくれる認蚌機関ずしお䜿甚する。サヌバヌずクラむアントで同じものを䜿甚する。
サヌバヌ蚌明曞 server.crt クラむアントがサヌバヌを確認するために䜿甚したす。䞭にはサヌバヌ公開鍵も含たれたす。
サヌバヌ秘密鍵 server.key サヌバヌサむドのみで䜿甚する秘密鍵です。クラむアントがサヌバヌ公開鍵で暗号したデヌタの耇合に䜿甚したす。
TSL認蚌共通鍵 ta.key クラむアントずサヌバヌで共通の鍵を持ちたす。TLSハンドシェむクにHMAC眲名を远加しおセキュリティを匷化したす。
DH認蚌秘密鍵 dh.pem サヌバヌ共通鍵ずクラむアント共通鍵のやり取りをする際に、Diffie-Hellman方匏を䜿甚したす。
サヌバヌ蚭定ファむル server.conf openvpnサヌバヌの蚭定ファむルです。

クラむアント偎のファむル

名前 ファむル名 説明
CA蚌明曞 ca.crt 接続するタヌバヌの蚌明曞の正圓性を蚌明しおくれる認蚌機関ずしお䜿甚する。サヌバヌずクラむアントで同じものを䜿甚する。

実際はファむルではなく、client.ovpnファむルの䞭に蚘述。
クラむアント蚌明曞 client.crt サヌバヌがクラむアントを確認するために䜿甚したす。䞭にはクラむアント公開鍵も含たれたす。

実際はファむルではなく、client.ovpnファむルの䞭に蚘述。
クラむアント秘密鍵 client.key クラむアントサむドのみで䜿甚する秘密鍵です。サヌバヌがクラむアント公開鍵で暗号したデヌタの耇合に䜿甚したす。

実際はファむルではなく、client.ovpnファむルの䞭に蚘述。
TSL認蚌共通鍵 ta.key クラむアントずサヌバヌで共通の鍵を持ちたす。TLSハンドシェむクにHMAC眲名を远加しおセキュリティを匷化したす。

実際はファむルではなく、client.ovpnファむルの䞭に蚘述。
クラむアント蚭定ファむル client.ovpn クラむアントのopenvpnの接続蚭定ファむル。

各皮ファむルず通信手順

easy-rsaの導入

openvpnが開発する「easy-rsa」ずいうPKI基盀を䜿甚しお、各皮蚌明曞の発行ず管理を行ないたす。easy-rsaで発行した、クラむアントずサヌバヌで䜿甚する各皮の蚌明曞ファむル等を、最終的にはopenvpnサヌバヌで䜿甚できるように、openvpnの適切なフォルダに栌玍したす。

easy-rsaのむンストヌル

$ sudo apt install easy-rsa

# 䜕が入ったか探しおみる
$ find / -name 'easy-*' 2>/dev/null
/var/lib/dpkg/info/easy-rsa.list
/var/lib/dpkg/info/easy-rsa.md5sums
/etc/openvpn.back/easy-rsa
/etc/openvpn/easy-rsa
/usr/share/doc/easy-rsa
/usr/share/easy-rsa

# /usr/share/easy-rsaの䞭身を芋おみる
$ sudo tree /usr/share/easy-rsa 
/usr/share/easy-rsa
├── easyrsa
├── openssl-easyrsa.cnf
├── vars.example
└── x509-types
    ├── ca
    ├── client
    ├── code-signing
    ├── COMMON
    ├── email
    ├── kdc
    ├── server
    └── serverClient

# make-cadirも䞀緒にむンストヌルされおいる
$ which make-cadir
/usr/bin/make-cadir

easy-rsaのむンストヌル時に、「make-cadir」も同時にむンストヌルされたす。これはopenvpn偎で蚌明曞等を管理する際の必芁なファむルを管理生成するために䜿甚するディレクトリを䜜成しおくれる䟿利ツヌルです。埌ほど䜿甚したす。

蚌明曞管理easy-rsa甚のナヌザヌの䜜成

蚌明曞の管理甚にナヌザヌを䜜成したす。以降の䜜業は党おこのナヌザヌでおこないたす。rootでの盎接の䜜業はさけたしょう。

$ sudo adduser ca-admin

/etc/sudoers.dの䞋のファむルを線集しお、「ca-admin」を远加しおおく。

OpenVPN Server

openvpnのむンストヌル

$ sudo apt install openvpn

# 初期の状態を芋おみる
$ sudo tree /etc/openvpn/
/etc/openvpn/
├── client
├── server
└── update-resolv-con

今埌は以䞋のディレクトリで䜜業を進めるので移動したす。

$ cd /etc/openvpn

easy-rsaのむントヌル時にむンストヌルされた「make-cadir」を䜿甚しお、openvpnの䞭に蚌明曞関連のディレクトリやファむルを䜜成したす。

ca-admin$ cd /etc/openvpn

ca-admin$ sudo make-cadir easy-rsa

### ca-adminが䜜業できるように、䜜成された「easy-rsa」ディレクトリのオヌナヌを倉曎。
ca-admin $ sudo su -
root# chown ca-admin.ca-admin easy-rsa
root# exit 

### 䜕が出来たか確認
ca-admin$ sudo tree .
.
├── client
├── easy-rsa
│   ├── easyrsa -> /usr/share/easy-rsa/easyrsa
│   ├── openssl-easyrsa.cnf
│   ├── vars
│   └── x509-types -> /usr/share/easy-rsa/x509-types
├── server
└── update-resolv-conf

蚌明曞䜜成に䜿甚される環境倉数を定矩する

䞊蚘で䜜成された「vars」ファむルを線集したす。最初は䜕かたくさん曞いおある。

# Easy-RSA 3 parameter settings

# NOTE: If you installed Easy-RSA from your distro's package manager, don't edit
# this file in place -- instead, you should copy the entire easy-rsa directory
# to another location so future upgrades don't wipe out your changes.

# HOW TO USE THIS FILE
#
# vars.example contains built-in examples to Easy-RSA settings. You MUST name
# this file 'vars' if you want it to be used as a configuration file. If you do
# not, it WILL NOT be automatically read when you call easyrsa commands.
#
# It is not necessary to use this config file unless you wish to change
# operational defaults. These defaults should be fine for many uses without the
# need to copy and edit the 'vars' file.
#
# All of the editable settings are shown commented and start with the command
# 'set_var' -- this means any set_var command that is uncommented has been
# modified by the user. If you're happy with a default, there is no need to
# define the value to its default.

# NOTES FOR WINDOWS USERS
#
# Paths for Windows  *MUST* use forward slashes, or optionally double-escaped
# backslashes (single forward slashes are recommended.) This means your path to
# the openssl binary might look like this:
# "C:/Program Files/OpenSSL-Win32/bin/openssl.exe"

# A little housekeeping: DON'T EDIT THIS SECTION
# 
# Easy-RSA 3.x doesn't source into the environment directly.
# Complain if a user tries to do this:
if [ -z "$EASYRSA_CALLER" ]; then
    echo "You appear to be sourcing an Easy-RSA 'vars' file." >&2
    echo "This is no longer necessary and is disallowed. See the section called" >&2
    echo "'How to use this file' near the top comments for more details." >&2
    return 1
fi
## (以䞋、省略##

党郚消しお、以䞋の行だけに線集する。この蚭定は蚌明曞を䜜成する際の暗号アルゎリズムずハッシュ関数を指定するものです。

  • set_var EASYRSA_ALGO "ec": 蚌明曞の鍵ペア生成に䜿甚する暗号アルゎリズムを指定したす。この蚭定では、楕円曲線暗号elliptic curve cryptography, ECCを䜿甚しおいたす。ECCは埓来のRSA暗号よりも効率的であり、同じセキュリティレベルを実珟するために短い鍵長が必芁です。
  • set_var EASYRSA_DIGEST "sha512": 蚌明曞のハッシュ関数を指定したす。この蚭定では、SHA-512ハッシュ関数を䜿甚しおいたす。SHA-512は安党性が高く、より匷力なハッシュ倀を生成したす。
ca-admin$ cat easy-rsa/vars 

set_var EASYRSA_ALGO "ec"
set_var EASYRSA_DIGEST "sha512"

openvpnで䜿甚する認蚌局を䜜成する

ココからの䜜業は、./easy-rsaに移動したす。

ca-admin$ cd easy-rsa

どんなファむルがあるかを確認。

ca-admin$ tree .
.
├── easyrsa -> /usr/share/easy-rsa/easyrsa
├── openssl-easyrsa.cnf
├── vars
└── x509-types -> /usr/share/easy-rsa/x509-types

PKIPublic Key Infrastractur、公開キヌ基盀のフォルダを䜜成する。以䞋のコマンドで認蚌局を初期化したす。認蚌局の蚭眮時に床だけ行ないたす。

easyrsaずいうコマンドがコピヌされおいるので、コレを䜿いたす。
ca-admin$ sudo ./easyrsa init-pki

init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki

どうなったか確認しおみる。pkiずいうディレクトリが䜜成されおいる。

ca-admin$ sudo tree .
.
├── easyrsa -> /usr/share/easy-rsa/easyrsa
├── openssl-easyrsa.cnf
├── pki
│   ├── openssl-easyrsa.cnf
│   ├── private
│   ├── reqs
│   └── safessl-easyrsa.cnf
├── vars
└── x509-types -> /usr/share/easy-rsa/x509-typeses

認蚌局を䜜成する。「名前なんにする」っお床だけ聞かれるけど、デフォルトのたたで゚ンタヌ。

ca-admin$ sudo ./easyrsa build-ca nopass
Using SSL: openssl OpenSSL 1.1.1n  15 Mar 2022
Generating RSA private key, 2048 bit long modulus (2 primes)
...................................................................................+++++
...............................+++++
e is 65537 (0x010001)
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [Easy-RSA CA]:

CA creation complete and you may now import and sign cert requests.
Your new CA certificate file for publishing is at:
/etc/openvpn/easy-rsa/pki/ca.crt

䜕ができたか芋おみる。

ca-admin$ sudo tree .
.
├── easyrsa -> /usr/share/easy-rsa/easyrsa
├── openssl-easyrsa.cnf
├── pki
│   ├── ca.crt
│   ├── certs_by_serial
│   ├── index.txt
│   ├── index.txt.attr
│   ├── issued
│   ├── openssl-easyrsa.cnf
│   ├── private
│   │   └── ca.key
│   ├── renewed
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── reqs
│   ├── revoked
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── safessl-easyrsa.cnf
│   └── serial
├── vars
└── x509-types -> /usr/share/easy-rsa/x509-types

ここでは、以䞋が䜜成されおいればOKです。

  • pki/ca.crtCA蚌明曞
  • pki/private/ca.keyCA秘密鍵

サヌバヌ秘密鍵ず蚌明曞眲名芁求を䜜成する

※「server」の郚分は任意。秘密鍵ず蚌明曞眲名芁求ファむルの名前に䜿われる。

「蚌明察象のサヌバヌの名前はなんにする」っお聞いおくるが、コマンドのオプションでしおいしたデフォルトserverのたたで゚ンタヌ。

ca-admin$ sudo ./easyrsa gen-req server nopass
Using SSL: openssl OpenSSL 1.1.1n  15 Mar 2022
Generating a RSA private key
...........................................+++++
..............................................+++++
writing new private key to '/etc/openvpn/easy-rsa/pki/easy-rsa-1693.iCGDZm/tmp.pa6uG0'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [server]:

Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/server.req
key: /etc/openvpn/easy-rsa/pki/private/server.key

なにができたか芋おみる。

ca-admin$ sudo tree .
.
├── easyrsa -> /usr/share/easy-rsa/easyrsa
├── openssl-easyrsa.cnf
├── pki
│   ├── ca.crt
│   ├── certs_by_serial
│   ├── index.txt
│   ├── index.txt.attr
│   ├── issued
│   ├── openssl-easyrsa.cnf
│   ├── private
│   │   ├── ca.key
│   │   └── server.key
│   ├── renewed
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── reqs
│   │   └── server.req
│   ├── revoked
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── safessl-easyrsa.cnf
│   └── serial
├── vars
└── x509-types -> /usr/share/easy-rsa/x509-types

ここでは以䞋のファむルが䜜成されおいればOKです。

  • pki/private/server.keyサヌバヌ秘密鍵
  • pki/reqs/server.req

サヌバ蚌明曞の䜜成

サヌバ蚌明曞に眲名する際は、匕数を先ほど䜜成した蚌明曞眲名芁求「server.req」を指定する拡匵子は䞍芁。芁求を受け入れるかの確認をしおくるので、「yes」ずタむプ。

ca-admin$ sudo ./easyrsa sign-req server server
Using SSL: openssl OpenSSL 1.1.1n  15 Mar 2022


You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a server certificate for 825 days:

subject=
    commonName                = server


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
...............................+++++
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-1737.yuvuQ4/tmp.epovku
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'server'
Certificate is to be certified until Oct 17 09:29:28 2025 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/openvpn/easy-rsa/pki/issued/server.crt

なにができたか確認

# tree .
.
├── easyrsa -> /usr/share/easy-rsa/easyrsa
├── openssl-easyrsa.cnf
├── pki
│   ├── ca.crt
│   ├── certs_by_serial
│   │   └── 9403D6EBE1885C65C36D2DFD8753769E.pem
│   ├── index.txt
│   ├── index.txt.attr
│   ├── index.txt.attr.old
│   ├── index.txt.old
│   ├── issued
│   │   └── server.crt
│   ├── openssl-easyrsa.cnf
│   ├── private
│   │   ├── ca.key
│   │   └── server.key
│   ├── renewed
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── reqs
│   │   └── server.req
│   ├── revoked
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── safessl-easyrsa.cnf
│   ├── serial
│   └── serial.old
├── vars
└── x509-types -> /usr/share/easy-rsa/x509-types

ここでは以䞋のファむルが䜜成されおいればOKです。

  • pki/issued/server.crtサヌバヌ蚌明曞

DHパラメヌタの生成

Diffie-Hellman éµäº€æ›ã¯ã€æš—号鍵を安党に転送するために䜿甚される方法です。

コマンド実行するず結構たちたす。。。

# ./easyrsa gen-dh

以䞋のファむルが䜜成されたす。

  • pki/dh.pemファむルが䜜成される。

TLS-Authキヌの生成

openvpnでは、セキュリティを向䞊させるために TLS-Authキヌ(ta.key)を実装しおいたす。党おのTLSハンドシェむクにHMAC眲名を远加するように、クラむアントずサヌバヌで䜿甚する共通鍵を䜜成したす。

# openvpn --genkey secret ta.key

以䞋のファむルが䜜成されたす。

  • ta.key

OpenVPNサヌバの構築に必芁なファむルを移動する

必芁なファむルは以䞋のものになる。

  • ca.crt
  • server.crt
  • server.key
  • dh2048.pem
  • ta.key

䞊蚘のファむルを「/etc/openvpn/server/」ディレクトリの配䞋にコピヌしたす。

ca-admin$ sudo cp pki/ca.crt ../server/ 
ca-admin$ sudo cp pki/issued/server.crt ../server/
ca-admin$ sudo cp pki/private/server.key ../server/
ca-admin$ sudo cp pki/dh.pem ../server/
ca-admin$ sudo cp ta.key ../server/
ca-admin$ ls ../server/
ca.crt  dh.pem  server.crt  server.key  ta.key

OpenVPNサヌバの構成ファむルをコピヌする

デフォルトのたただず、openvpnサヌバの構成ファむルは「/etc/openvpn」フォルダには存圚しないため、openvpnパッケヌゞに付属しおいる構成ファむルのテンプレヌトをコピヌしおくる必芁がありたす。

ca-admin$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/server.conf ../server/
ca-admin$ ls ../server/
ca.crt  dh.pem  server.conf  server.crt  server.key  ta.key

OpenVPNサヌバの構成ファむルを線集したす。以䞋の行を線集・远蚘したす。

78 ca ca.crt #そのたた
79 cert server.crt #そのたた
80 key server.key #そのたた
85 dh dh.pem #dh_2048.pemをdh.pemに倉曎
142 push "route 192.168.1.0 255.255.255.0" #コメントアりト。自分のLANのアドレスに倉曎。
244 tls-auth ta.key 0 #そのたた

OpenVPN Client

クラむアント秘密鍵ず蚌明曞眲名芁求を䜜成する

接続しおくるクラむアントを識別できるように、名前を聞かれる。今回は「test」ずいれた。

ca-admin$ sudo ./easyrsa gen-req client nopass
Using SSL: openssl OpenSSL 1.1.1n  15 Mar 2022
Generating a RSA private key
...+++++
.......................................................+++++
writing new private key to '/etc/openvpn/easy-rsa/pki/easy-rsa-2156.tty8u8/tmp.96r0wc'
-----
You are about to be asked to enter information that will be incorporated
into your certificate request.
What you are about to enter is what is called a Distinguished Name or a DN.
There are quite a few fields but you can leave some blank
For some fields there will be a default value,
If you enter '.', the field will be left blank.
-----
Common Name (eg: your user, host, or server name) [client]:test

Keypair and certificate request completed. Your files are:
req: /etc/openvpn/easy-rsa/pki/reqs/client.req
key: /etc/openvpn/easy-rsa/pki/private/client.key

なにができたか

ca-admin$ sudo tree .
.
├── easyrsa -> /usr/share/easy-rsa/easyrsa
├── openssl-easyrsa.cnf
├── pki
│   ├── ca.crt
│   ├── certs_by_serial
│   │   └── 9403D6EBE1885C65C36D2DFD8753769E.pem
│   ├── dh.pem
│   ├── index.txt
│   ├── index.txt.attr
│   ├── index.txt.attr.old
│   ├── index.txt.old
│   ├── issued
│   │   └── server.crt
│   ├── openssl-easyrsa.cnf
│   ├── private
│   │   ├── ca.key
│   │   ├── client.key
│   │   └── server.key
│   ├── renewed
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── reqs
│   │   ├── client.req
│   │   └── server.req
│   ├── revoked
│   │   ├── certs_by_serial
│   │   ├── private_by_serial
│   │   └── reqs_by_serial
│   ├── safessl-easyrsa.cnf
│   ├── serial
│   └── serial.old
├── ta.key
├── vars
└── x509-types -> /usr/share/easy-rsa/x509-types

ここでは以䞋のファむルが䜜成されおいればOKです。

  • pki/private/client.keyクラむアント秘密鍵
  • pki/reqs/client.req

クラむアント蚌明曞を䜜成する

クラむアント蚌明曞に眲名する際は、匕数を先ほど䜜成した蚌明曞眲名芁求「client_test.req」を指定する。拡匵子は䞍芁。芁求を受け入れるか確認しおくるので、「yes」ず入力。

ca-admin$ sudo ./easyrsa sign-req client client
Using SSL: openssl OpenSSL 1.1.1n  15 Mar 2022


You are about to sign the following certificate.
Please check over the details shown below for accuracy. Note that this request
has not been cryptographically verified. Please be sure it came from a trusted
source or that you have verified the request checksum with the sender.

Request subject, to be signed as a client certificate for 825 days:

subject=
    commonName                = test


Type the word 'yes' to continue, or any other input to abort.
  Confirm request details: yes
Using configuration from /etc/openvpn/easy-rsa/pki/easy-rsa-2209.1K7Pp7/tmp.MNAwKK
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName            :ASN.1 12:'test'
Certificate is to be certified until Oct 17 09:52:56 2025 GMT (825 days)

Write out database with 1 new entries
Data Base Updated

Certificate created at: /etc/openvpn/easy-rsa/pki/issued/client.crt

ここでは以䞋のファむルが䜜成されたす。

  • pik/issued/client_test.crtクラむアント蚌明曞

OpenVPNクラむアントの構成ファむルをコピヌする

デフォルトのたただず、openvpnクラむアントの構成ファむルは「/etc/openvpn」フォルダには存圚しないため、openvpnパッケヌゞに付属しおいる構成ファむルのテンプレヌトをコピヌしおくる必芁がある。

ca-admin$ sudo cp /usr/share/doc/openvpn/examples/sample-config-files/client.conf ../client/

OpenVPNクラむアントの構成ファむルを線集する

以䞋の行を線集したす。

42 remote xx.xx.xx.xx 1194 # ルヌタヌWAN偎IPアドレス
61 user nobody # コメントの「;」を消す
62 group nogroup # コメントの「;」を消す
88行目の、ca ca.crtCA蚌明曞をコメントしお、以䞋の内容を盎接蚘述。
<ca>
# pki/ca.crtファむルの内容をそのたたコピペ
</ca>
89行目の、cert client.crtクラむアント蚌明曞をコメントしお、以䞋の内容を盎接蚘述
<cert>
# pki/issued/client.certファむルの内容をそのたたコピペ
</cert>
90行名のkey client.keyクラむアント秘密キヌをコメントしお、以䞋の内容を盎接蚘述。
<key>
# pki/private/client.keyファむルの内容をそのたたコピペ
</key>

以䞋を䞀番最埌あたりに、远蚘

key-direction 1

曎に、䞀番䞋に、TLS認蚌の共通キヌを蚘入

<tls-auth>
# ta.keyの内容をコピペ
</tls-auth>

OpenVPNクラむアントの構成ファむルの拡匵子を倉曎する

最埌にクラむアントに配垃するように、client.confの拡匵子を倉曎したす。

ca-admin$ sudo mv ../client/client.conf ../client/client.ovpn

自動起動の蚭定

systemctlを぀かっお蚭定をおこないたす。

# OS起動時に起動する
ca-admin$ sudo systemctl -f enable [email protected]

# 起動
ca-admin$ systemctl start [email protected]

起動の蚭定ファむルの䞭身を確認したい堎合は、「/etc/systemd/system/multi-user.target.wants/[email protected]」を参照。

起動しおいるかの確認

port1194がUDPでオヌプンしおいればOK。

ca-admin$ ss -antu | grep 1194
udp   UNCONN 0      0            0.0.0.0:1194       0.0.0.0:*                             

iptablesの蚭定

この図のずおり、vpnサヌバヌは「192.168.1.0/24」のネットワヌクず、「10.8.0.0/30」のネットワヌクに所属しおいる。クラむアントからLANに接続するには、vpnサヌバヌでIP転送の蚭定をする必芁がある。

カヌネルのip_forwardの蚭定

「/etc/sysctd.conf」を線集。以䞋の行を远加。もしくは既に存圚する堎合は倀を「1」に修正。

net.ipv4.ip_forward=1

蚭定を反映させる。

ca-admin$ sudo sysctl -p

iptablesの蚭定

natを蚭定する。10.8.0.0/24のネットワヌクから来たパケットで、eth0192.168.1.0/24に出おいくパケットをMASQUERADE。

ca-admin$ sudo iptables -t nat -A POSTROUTING -o eth0 -s 10.8.0.0/24 -j MASQUERADE

確認。

ca-admin$ sudo iptables -t nat -L -n -v
Chain PREROUTING (policy ACCEPT 60084 packets, 15M bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain INPUT (policy ACCEPT 8300 packets, 1574K bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain OUTPUT (policy ACCEPT 5 packets, 616 bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain POSTROUTING (policy ACCEPT 19 packets, 2758 bytes)
 pkts bytes target     prot opt in     out     source               destination         
  884  125K MASQUERADE  all  --  *      eth0    10.8.0.0/24          0.0.0.0/0

パケット転送を蚱可。

ca-admin$ sudo iptables -A FORWARD -i tun0 -o eth0 -j ACCEPT
ca-admin$ sudo iptables -A FORWARD -i eth0 -o tun0 -m state --state RELATED,ESTABLISHED -j ACCEPT

確認。

ca-admin$ iptables -L -n -v
Chain INPUT (policy ACCEPT 172K packets, 38M bytes)
 pkts bytes target     prot opt in     out     source               destination         

Chain FORWARD (policy ACCEPT 112 packets, 22353 bytes)
 pkts bytes target     prot opt in     out     source               destination         
28943 3272K ACCEPT     all  --  tun0   eth0    0.0.0.0/0            0.0.0.0/0           
 111K  143M ACCEPT     all  --  eth0   tun0    0.0.0.0/0            0.0.0.0/0            state RELATED,ESTABLISHED

Chain OUTPUT (policy ACCEPT 142K packets, 152M bytes)
 pkts bytes target     prot opt in     out     source               destination

䜜成したiptablesのルヌルを保存する必芁が有りたす。iptables-persistentを䜿うずコマンドで指定したルヌルが、/etc/iptables/rules.v4に保存しお、サヌバヌ再起動時にも同じ蚭定を反映させるこずができたす。

ca-admin$ apt install iptabels-persistent

むンストヌル䞭に、「既にiptablesのルヌルがありたす。保存したすか」ず聞いおいるので、yesを遞択するず、保存される。

サヌバヌ起動時に/etc/iptables/rules.v4の反映

保存したルヌルファむルをサヌバヌ起動時に反映させる蚭定をしたす。aptでむンストヌルするパケヌゞは「iptables-persistent」だけど、systemctlから呌び出すのは「netfilter-persistent」なので、ややこしいです。

# 起動時に有効化
ca-admin$ systemctl enable netfilter-persistent.service

# 今スグ反映
ca-admin$ systemctl start netfilter-persistent.service

ルヌタヌのポヌト転送の蚭定

openvpnのポヌト1194は、LANの䞭で開いおいるため、ルヌタヌのWAN偎にも同じポヌトを開攟しお、WAN偎の1194ぞのUDPアクセスは、LAN内の「192.168.1.96」の「udp: 1194」に転送する蚭定をおこないたす。

最埌に

先皋䜜成した、「client.ovpn」ファむルをクラむアントに配垃しお、クラむアントでopenvpnを䜿甚しお接続できれば完了です。

⚠ **GitHub.com Fallback** ⚠