# Ubuntu18.04 OpenVPNのインストール

**URL:** <https://forum.ficusonline.com/t/topic/223>\
**Category:** Server\
**Created:** [2018 年 7 月 15 日午前 7:42 UTC](https://forum.ficusonline.com/t/topic/223 "2018-07-15T07:42:34Z")\
**Posts on this page:** 1\
**Page:** 1

<div class="post-metadata">

**Author:** ![tk-fuse](https://forum.ficusonline.com/user_avatar/forum.ficusonline.com/tk-fuse/32/255_2.png) [@tk-fuse](https://forum.ficusonline.com/u/tk-fuse)\
**Post date:** [2018 年 7 月 15 日午前 7:42 UTC](https://forum.ficusonline.com/t/topic/223/1 "2018-07-15T07:42:34Z")

</div>

グローバルIPアドレスが割り当てられていない端末に外部からアクセス出来るようにするためOpenVPNサーバを利用します。OpenVPNはSSLで接続できるオープンソースのVPNサーバです。SSL接続のための認証ファイルの作成などインストールが少し面倒ですが、Ubuntu16.04用にシェルスクリプトが配布されているため、面倒な作業は全てこのスクリプトが担ってくれます。

\<参照サイト\>

[https://www.cyberciti.biz/faq/howto-setup-openvpn-server-on-ubuntu-linux-14-04-or-16-04-lts/](https://www.cyberciti.biz/faq/howto-setup-openvpn-server-on-ubuntu-linux-14-04-or-16-04-lts/)

インストール手順は次の通りです。

1. インストール先端末のグローバルIPアドレス、またはグローバルIPと紐付けされたドメインネームを確認します。
2. シェルスクリプトopenvpn-install.shをダウンロードします。
3. openvpn-install.shを起動し、OpenVPNサーバをインストールします。
4. クライアントとなる端末に上記3で作成された設定ファイルをコピーします。
5. 設定ファイルを指定してOpenVPNのクライアントを起動し、接続できるかどうか確認します。

**1.グローバルIPアドレスの確認**

> $ lshw

上記コマンドでnetwork項目のロジカル名をチェックします。eth0の場合は、

> $ ip addr show eth0

と入力することでアドレスを確認できます。

但しローカルアドレスを示す場合には、[この筑波大学のVPNサイト](http://www.vpngate.net/ja/)にアクセスすることでも自身のグローバルIPが確認できます。

**2.シェルスクリプトopenvpn-install.shのダウンロード**

> $ wget [https://git.io/vpn](https://git.io/vpn) -O openvpn-install.sh

アウトプットは以下のようになります。

```
--2016-06-27 07:30:25-- https://git.io/vpn
Resolving git.io (git.io)... 23.23.173.104, 54.243.161.116, 23.23.111.66
Connecting to git.io (git.io)|23.23.173.104|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://raw.github.com/Nyr/openvpn-install/master/openvpn-install.sh [following]
--2016-06-27 07:30:26-- https://raw.github.com/Nyr/openvpn-install/master/openvpn-install.sh
Resolving raw.github.com (raw.github.com)... 151.101.100.133
Connecting to raw.github.com (raw.github.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 301 Moved Permanently
Location: https://raw.githubusercontent.com/Nyr/openvpn-install/master/openvpn-install.sh [following]
--2016-06-27 07:30:27-- https://raw.githubusercontent.com/Nyr/openvpn-install/master/openvpn-install.sh
Resolving raw.githubusercontent.com (raw.githubusercontent.com)... 151.101.100.133
Connecting to raw.githubusercontent.com (raw.githubusercontent.com)|151.101.100.133|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 13340 (13K) [text/plain]
Saving to: â€˜openvpn-install.shâ€™
 
openvpn-install.sh 100%[==============================================>] 13.03K 80.9KB/s in 0.2s    
 
2016-06-27 07:30:28 (80.9 KB/s) - â€˜openvpn-install.shâ€™ saved [13340/13340]

```

**3.OpenVPNサーバのインストール**

上記シェルスクリプトを実行します。

> $ sudo bash openvpn-install.sh

グローバルIPアドレスxx.xx.xx.xx、TCPまたはUDP（推奨）での接続、ポート番号1194、DNSサーバ（Google:8.8.8.8,8.8.4.4, APNICとCloudflare: 1.1.1.1, 1.0.0.1）、クライアント名称：client（任意）の入力を促す表示がされるので、各々入力します。入力後OpenVPNのインストールとSSL認証に関する処理が行われます。

```
Okay, that was all I needed. We are ready to setup your OpenVPN server now
Press any key to continue...
Get:1 http://security.ubuntu.com 
......
...
..
--2016-06-27 17:10:38-- https://github.com/OpenVPN/easy-rsa/releases/download/3.0.1/EasyRSA-3.0.1.tgz
Resolving github.com (github.com)... 192.30.252.120
Connecting to github.com (github.com)|192.30.252.120|:443... connected.
HTTP request sent, awaiting response... 302 Found
Location: https://github-cloud.s3.amazonaws.com/releases/4519663/9dab10e8-7b6a-11e5-91af-0660987e9192.tgz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAISTNZFOVBIJMK3TQ%2F20160627%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20160627T114040Z&X-Amz-Expires=300&X-Amz-Signature=717ae4f606d1999b4c7c164ae06d163c494197f04aafffa9f760a8e0bf136136&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3DEasyRSA-3.0.1.tgz&response-content-type=application%2Foctet-stream [following]
--2016-06-27 17:10:40-- https://github-cloud.s3.amazonaws.com/releases/4519663/9dab10e8-7b6a-11e5-91af-0660987e9192.tgz?X-Amz-Algorithm=AWS4-HMAC-SHA256&X-Amz-Credential=AKIAISTNZFOVBIJMK3TQ%2F20160627%2Fus-east-1%2Fs3%2Faws4_request&X-Amz-Date=20160627T114040Z&X-Amz-Expires=300&X-Amz-Signature=717ae4f606d1999b4c7c164ae06d163c494197f04aafffa9f760a8e0bf136136&X-Amz-SignedHeaders=host&actor_id=0&response-content-disposition=attachment%3B%20filename%3DEasyRSA-3.0.1.tgz&response-content-type=application%2Foctet-stream
Resolving github-cloud.s3.amazonaws.com (github-cloud.s3.amazonaws.com)... 54.231.72.3
Connecting to github-cloud.s3.amazonaws.com (github-cloud.s3.amazonaws.com)|54.231.72.3|:443... connected.
HTTP request sent, awaiting response... 200 OK
Length: 40960 (40K) [application/octet-stream]
Saving to: â€˜/root/EasyRSA-3.0.1.tgzâ€™
 
/root/EasyRSA-3.0.1.tgz 100%[================================================>] 40.00K 38.8KB/s in 1.0s   
 
2016-06-27 17:10:43 (38.8 KB/s) - â€˜/root/EasyRSA-3.0.1.tgzâ€™ saved [40960/40960]
 
 
init-pki complete; you may now create a CA or requests.
Your newly created PKI dir is: /etc/openvpn/easy-rsa/pki
Generating a 2048 bit RSA private key
........+++
...............................................................................................+++
writing new private key to '/etc/openvpn/easy-rsa/pki/private/ca.key.BjRh5frdDd'
-----
Generating DH parameters, 2048 bit long safe prime, generator 2
This is going to take a long time
....+.....+................................................................................+..................................................................................................................................................................+......................................
...
..
.................................................................................................................+........................................................................................................................................+.................................+......................................................+...++*++*
 
DH parameters of size 2048 created at /etc/openvpn/easy-rsa/pki/dh.pem
 
Generating a 2048 bit RSA private key
.......................................................................+++
..................................................+++
writing new private key to '/etc/openvpn/easy-rsa/pki/private/server.key.9ieuluTC2R'
-----
Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.cnf
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 Jun 25 11:55:48 2026 GMT (3650 days)
 
Write out database with 1 new entries
Data Base Updated
Generating a 2048 bit RSA private key
.........+++
.........+++
writing new private key to '/etc/openvpn/easy-rsa/pki/private/iphone.key.lokNfOiobc'
-----
Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.cnf
Check that the request matches the signature
Signature ok
The Subject's Distinguished Name is as follows
commonName :ASN.1 12:'iphone'
Certificate is to be certified until Jun 25 11:55:48 2026 GMT (3650 days)
 
Write out database with 1 new entries
Data Base Updated
Using configuration from /etc/openvpn/easy-rsa/openssl-1.0.cnf
 
An updated CRL has been created.
CRL file: /etc/openvpn/easy-rsa/pki/crl.pem
 
244
 
Looks like your server is behind a NAT!
 
If your server is NATed (e.g. LowEndSpirit), I need to know the external IP
If that's not the case, just ignore this and leave the next field blank
External IP:

```

インストール後、起動時のiptables実行ファイル **/etc/rc.local** により、指定ポート1194にファイアウォール除外ルール、OpenVPNによるIPアドレスと外部IPとの紐付け等が設定されます。

> $ cat /etc/rc.local

出力結果は、

```
iptables -I FORWARD -m state --state RELATED,ESTABLISHED -j ACCEPT
iptables -I FORWARD -s 10.8.0.0/24 -j ACCEPT
iptables -I INPUT -p udp --dport 1194 -j ACCEPT
iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -j SNAT --to 139.59.1.155 （ここはルータの設定でポートフォワードしたローカルIPアドレスでも可）

```

IPフォワーディングについては、デフォルトでOpenVPNによるネットワーク10.8.0.0/24に外部からのパケットが通されるため、 **/etc/sysctl.conf** 内で **net.ipv4.ip\_forward=1** をアンコメントする必要はありません。

> **[Check Linux IP Forwarding for Access Server Routing](https://openvpn.net/as-docs/faq-ip-forwarding-on-linux.html)**

OpenVPNサーバの設定内容の確認は、

> $ sudo vi /etc/openvpn/server.conf

内容は、

```
port 1194
proto udp
dev tun
sndbuf 0
rcvbuf 0
ca ca.crt
cert server.crt
key server.key
dh dh.pem
tls-auth ta.key 0
topology subnet
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "redirect-gateway def1 bypass-dhcp"
push "dhcp-option DNS 8.8.8.8"
push "dhcp-option DNS 8.8.4.4"
keepalive 10 120
cipher AES-128-CBC
comp-lzo
user nobody
group nogroup
persist-key
persist-tun
status openvpn-status.log
verb 3
crl-verify crl.pem

```

**4.クライアント端末に上記3で作成された設定ファイルをコピー**

クライアント端末の任意のディレクトリに3で作成された設定ファイル：client.ovpnをコピーします。

**5.クライアント端末からアクセス**

設定ファイルを指定してOpenVPNクライアントを起動します。（クライアントにもOpenVPNをインストール）

> $ sudo openvpn --client --config client.ovpn

ダウンロードしたシェルスクリプトには、以下のメニューで示された機能が備わっています。

```
Looks like OpenVPN is already installed

What do you want to do?
   1) Add a cert for a new user クライアントの追加認証
   2) Revoke existing user cert 認証取り消し
   3) Remove OpenVPN OpenVPNのアンインストール
   4) Exit 終了
Select an option [1-4]:

```

**OpenVPNシステム設定**

最後にsystemctlコマンドでOpenVPNの起動設定などを確認します。@serverのserverはopenvpnのconfファイル名となります。

ユニットの状態を表示(動いているかどうかなど):

> $ systemctl status openvpn@server

有効化（起動時に自動で実行するよう設定）されているかどうか表示:

> $ systemctl is-enabled openvpn@server

起動時に実行されるように有効化する:

> $ sudo systemctl enable openvpn@server

ユニットを有効化して今すぐ起動する:

> $ sudo systemctl enable --now openvpn@server

システム起動時に実行されないように無効化する:

> $ sudo systemctl disable openvpn@server
