1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 1 year has passed since last update.

UbuntuでOpenVPNを構築してみた

1
Posted at

はじめに

OpenVPNを簡単に構築する方法として、有志の方が開発したインストールスクリプトがあります。

この方法は、簡単に設定・ovpnファイルの出力などを行ってくれるのですが、最近のインストーラではOpenVPNのサービスが起動できないといった不具合があり、解決するのに時間がかかったため備忘録として残しました。

2025/07/30日現在では、まだ修正されていません。また、このシェルスクリプトの質問はバージョンアップに伴い変更される場合があるためご注意ください。
変更されたタイミングで、こちらの記事の修正も行う予定です。

OpenVPNとは

OpenVPNはオープンソースのVPNソフトウェアで、簡単に構築できるのが特徴です。今回は、Ubuntu 22.04上でOpenVPNを構築していきます。また、接続する端末でOpenVPNサーバーに接続するにはOpenVPN Connectが必要ですのであらかじめダウンロードしておくと楽です。

openvpn-installとは

有志の方が開発した、OpenVPNのサーバー構成(設定ファイルの構成、サーバー証明書の発行、ovpnファイルの生成など)を一括で設定できるシェルスクリプトです。通常は、sudo apt install openvpn easy-rsaなどといったパッケージをインストールし、自分で証明書などを発行する必要がありましたが、このツールを実行して必要事項を設定することで簡単に構築することが可能です。

それでは、次のセクションから実際にOpenVPNを構築していきたいと思います。

インストーラのダウンロードと実行

まずは、シェルスクリプトをダウンロードします。ファイルは、自身のユーザーフォルダに配置します。

ユーザーフォルダに移動
cd ~/

その後、curlを使用してシェルスクリプト本体をダウンロードします。

シェルスクリプトをダウンロード
curl -O https://raw.githubusercontent.com/angristan/openvpn-install/master/openvpn-install.sh

このシェルスクリプトを実行します。

スクリプトを実行
sudo bash ./openvpn-install.sh

OpenVPNの設定

先ほどのシェルスクリプトを実行すると、いくつかサーバーの構成に必要な質問をされます。質問される内容としては、

  • IPアドレス
  • VPNで使用するプロトコル
  • IPv6のサポート
  • 開放するポート
  • DNSサーバーの選択

などで、ユーザーを作成するときはホストネーム(ユーザー名)とパスワードを設定するかどうか聞かれます。
最初は、IPアドレスを聞かれますが、こちらはデフォルトで現在のIPアドレスを参照して入力されているためそのままEnterでOKです。

以降「~を選択します」となっている場合は、その項目で聞かれている数字あるいは「y/n」のどちらかを入力してEnterを押してください。

設定例
I need to know the IPv4 address of the network interface you want OpenVPN listening to.
Unless your server is behind NAT, it should be your public IPv4 address.
IP address: xxx.xxx.xxx.xxx

It seems this server is behind NAT. What is its public IPv4 address or hostname?
We need it for the clients to connect to the server.
Public IPv4 address or hostname: xxx.xxx.xxx.xxx

次に、IPv6を有効にするか確認されますが、今回は有効にしないので、「n」を選択します。

設定例
Your host does not appear to have IPv6 connectivity.

Do you want to enable IPv6 support (NAT)? [y/n]: n

次に、開放するポートです。OpenVPNは既定ではUDP 1194ポートを開放しますが、使用するポートを変更することもできます。今回は443ポートを開放しますので、2) Customを選択するために2を選択します。その後、Custom Portを入力できるようになるので、443と入力してEnterを押します。

設定例
What port do you want OpenVPN to listen to?
   1) Default: 1194
   2) Custom
   3) Random [49152-65535]
Port choice [1-3]: 2
Custom port [1-65535]: 443

次に、プロトコルを選択します。UDPかTCPが利用できます。今回はTCPを利用します。

設定例
What protocol do you want OpenVPN to use?
UDP is faster. Unless it is not available, you shouldn't use TCP.
   1) UDP
   2) TCP
Protocol [1-2]: 2

次に、DNSサービスを選択します。規定ではAdGuard DNSですが、Anycast: worldwideとなっているDNSであればどれを選んでも大丈夫です。個人的には、Cloudflare DNSをおすすめします。

設定例
What DNS resolvers do you want to use with the VPN?
   1) Current system resolvers (from /etc/resolv.conf)
   2) Self-hosted DNS Resolver (Unbound)
   3) Cloudflare (Anycast: worldwide)
   4) Quad9 (Anycast: worldwide)
   5) Quad9 uncensored (Anycast: worldwide)
   6) FDN (France)
   7) DNS.WATCH (Germany)
   8) OpenDNS (Anycast: worldwide)
   9) Google (Anycast: worldwide)
   10) Yandex Basic (Russia)
   11) AdGuard DNS (Anycast: worldwide)
   12) NextDNS (Anycast: worldwide)
   13) Custom
DNS [1-12]: 3

次に、通信のデータ圧縮を行うかどうか聞かれます。データ圧縮はデータ量を削減できますが、その反面VORACLE攻撃に弱いため規定では無効になっているようです。なので今回は「n」を選択します。

設定例
Do you want to use compression? It is not recommended since the VORACLE attack makes use of it.
Enable compression? [y/n]: n

次に、暗号化の設定をカスタマイズするか聞かれます。今回は特にカスタマイズせずそのまま進めます。

設定例
Do you want to customize encryption settings?
Unless you know what you're doing, you should stick with the default parameters provided by the script.
Note that whatever you choose, all the choices presented in the script are safe (unlike OpenVPN's defaults).
See https://github.com/angristan/openvpn-install#security-and-encryption to learn more.

Customize encryption settings? [y/n]: n

ここまで来たら、あとは設定と必要なパッケージをインストールするのみです。何か適当なキーを押すと自動でインストールから設定まで進めてくれます。

ユーザーの作成

インストール後、hostnameとpasswordをどうするか質問されます。hostnameは後々生成されるovpnファイルの名前になります。基本はユーザーの名前でよいです。
パスワードは、パスワードレス(秘密鍵に設定する)か、パスワードを設定するか選択できますが、パスワードレスにします。
ここまで設定すると、現在のディレクトリに<hostname>.ovpnファイルが生成されます。scpコマンドを使用してローカルにダウンロードするか、Visual Studio CodeやWinSCPなどといったソフトウェアでダウンロードしてください。
最初にダウンロードしたOpenVPN Clientをインストールし、ovpnファイルを開くと接続できるようになります。

(任意) 設定の変更

OpenVPNのセットアップスクリプトには絶対パスで設定すべき部分を相対パスで生成しているため、OpenVPNのサービスが起動せず、VPN接続できないという致命的な欠点があります。
まずは、設定を変更すべきかどうかを、OpenVPNのサービスの起動状態で見てみます。

サービスの状態確認
sudo systemctl status openvpn-server@server.service

実行後、Error opening configuration file: server.confとなっていた場合は変更する必要があります。
変更する箇所は次の通りです。

  • /etc/openvpn/server.conf
  • openvpn-server@server.service

上から順番に変更していきます。

設定ファイルの変更

まずは、手ごろなエディタでserver.confを開いてみます。開く際はsudo権限が必要です。

ファイルを開く(nano)
sudo nano /etc/openvpn/server.conf
server.conf
port 443
proto tcp
dev tun
user nobody
group nogroup
persist-key
persist-tun
keepalive 10 120
topology subnet
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist ipp.txt
push "dhcp-option DNS xxx.xxx.xxx.xxx"
push "dhcp-option DNS xxx.xxx.xxx.xxx"
push "redirect-gateway def1 bypass-dhcp"
dh none
ecdh-curve prime256v1
tls-crypt tls-crypt.key
crl-verify crl.pem
ca ca.crt
cert server_xxxx.crt
key server_xxxx.key
auth SHA256
cipher AES-128-GCM
ncp-ciphers AES-128-GCM
tls-server
tls-version-min 1.2
tls-cipher TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
client-config-dir /ccd
status /var/log/openvpn/status.log
verb 3

server.confを開いて、次の項目のファイルの前に/etc/openvpn/をつけてください。

  • ifconfig-pool-persist
  • tls-crypt
  • ca
  • cert
  • key
  • client-config-dir

変更後、このようになればOKです。

server.conf(変更後)
port 443
proto tcp
dev tun
user nobody
group nogroup
persist-key
persist-tun
keepalive 10 120
topology subnet
server 10.8.0.0 255.255.255.0
ifconfig-pool-persist /etc/openvpn/ipp.txt
push "dhcp-option DNS xxx.xxx.xxx.xxx"
push "dhcp-option DNS xxx.xxx.xxx.xxx"
push "redirect-gateway def1 bypass-dhcp"
dh none
ecdh-curve prime256v1
tls-crypt /etc/openvpn/tls-crypt.key
crl-verify /etc/openvpn/crl.pem
ca /etc/openvpn/ca.crt
cert /etc/openvpn/server_xxxx.crt
key /etc/openvpn/server_xxxx.key
auth SHA256
cipher AES-128-GCM
ncp-ciphers AES-128-GCM
tls-server
tls-version-min 1.2
tls-cipher TLS-ECDHE-ECDSA-WITH-AES-128-GCM-SHA256
client-config-dir /etc/openvpn/ccd
status /var/log/openvpn/status.log
verb 3

設定ファイルが正しく動作しているか確認するために、OpenVPNを起動させてみます。

設定ファイル読み込み
openvpn --config /etc/openvpn/server.conf

問題がなければ、Ctrl + Cで処理を停止します。

サービスの変更

Ubuntuのサービスでも同様のことが起こっているため、server.confがどこにあるかわからない状態になっています。systemctl editを使用してファイルを変更していきます。

systemctl edit
sudo systemctl edit --full openvpn-server@server.service

開いて、赤い部分を緑の部分に置き換えてください。

openvpn-server@server.service
[Unit]
Description=OpenVPN service for %I
After=network-online.target
Wants=network-online.target
Documentation=man:openvpn(8)
Documentation=https://community.openvpn.net/openvpn/wiki/Openvpn24ManPage
Documentation=https://community.openvpn.net/openvpn/wiki/HOWTO

[Service]
Type=notify
PrivateTmp=true
WorkingDirectory=/etc/openvpn/server
- ExecStart=/usr/sbin/openvpn --status %t/openvpn-server/status-%i.log --status-version 2 --suppress-timestamps --config %i.conf
+ ExecStart=/usr/sbin/openvpn --status %t/openvpn-server/status-%i.log --status-version 2 --suppress-timestamps --config /etc/openvpn/%i.conf
CapabilityBoundingSet=CAP_IPC_LOCK CAP_NET_ADMIN CAP_NET_BIND_SERVICE CAP_NET_RAW CAP_SETGID CAP_SETUID CAP_SYS_CHROOT CAP_DAC_OVERRIDE CAP_AUDIT_WRITE
LimitNPROC=10
DeviceAllow=/dev/null rw
DeviceAllow=/dev/net/tun rw
ProtectSystem=true
ProtectHome=true
KillMode=process
RestartSec=5s
Restart=on-failure

[Install]
WantedBy=multi-user.target

保存して、sudo systemctl status openvpn-server@server.serviceを見てみます。(保存したタイミングでサービスは自動で再起動します)
Activeとなっていたら成功です。お疲れさまでした!

補足: ポートが重複していた場合

openvpn-server@server.serviceがinactive(dead)にもかかわらず、443ポートが使われていた場合は次のコマンドでどのプログラムが使用しているか確認できます。

ポートチェック
sudo ss -lptun | grep 443

使われている場合は次のように表示されます。

結果
tcp   LISTEN 0      32           0.0.0.0:443       0.0.0.0:*    users:(("openvpn",pid=12791,fd=6))

killコマンドで、pid=の数字を指定してプロセスを終了し、sudo systemctl restart openvpn-server@server.serviceを実行すると正しく起動できると思います。

プロセスキル
sudo kill <pid>

おわりに

ここまでOpenVPNのインストールと修正を行いましたが、はやくIssueが解決されることを祈るばかりです。もし、修正されていたり誤りがあった場合はコメントにてお知らせいただきますと助かります。

1
4
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
4

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?