0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

Rocky Linux 9でOpenConnect VPNサーバを作る

Posted at

この記事は、2019年に投稿した、「CentOS 7でOpenConnect VPNサーバを作る」をもとに、Rocky Linux 9用に修正したものです。(内容はあまり変わっていません)

はじめに

広く知られているように、中国のインターネットは規制されています。LINEやGoogleのサービスへのアクセスは基本的にアクセスできません。こういった規制は昔からあり、当時からVPNで回避していました。かつてはPPTPでも簡単に塀超えできたものです。(遠い目)

年を追うごとにVPNの規制が厳しくなり、(地域やキャリア/プロバイダによる差はあるものの)VPNでの塀超えがだんだん難しくなってきました。ここで紹介する、OpenConnectは、2025年現在でも比較的安定して接続できるVPNです。

OpenConnectは、Cisco AnyConnectと互換性のあるVPNソフトウエアのオープンソース実装です。

OpenConnectで作ったVPNサーバには、Cisco AnyConnectで接続することができます。(iOS用、Android用、Windows用、macOS用などがあります)

OpenConnectは、PPTP, L2TPなどの他のVPNプロトコルに比べて規制に強い(あるいはまだ規制が及んでない)ようなので、CentOS 7にOpenConnectをインストールして、塀超え目的のVPNサーバを作ってみます。

今回、サイト証明書は、Let's Encryptで取得しました。

この記事の作業は、ポート80やポート443を使うHTTPサーバを立ち上げていない状況で行っています。もし、HTTPサーバでポート443を使っている場合は、OpenConnectのポートを10443など別のポートに割り当てるなどして対処してください。

作業

大まかに次のような手順で進めていきます。

  1. EPELを使えるようにする
  2. Let's EncryptでSSL証明書を作る
  3. OpenConnectをインストールする

EPEL

EPELを使えるようにします。

# dnf -y install epel-release

Let's Encryptで証明書を得る

certbotをインストールします。

# dnf -y install certbot

ポート80を開けておきます。

# firewall-cmd --add-service=http --permanent
# firewall-cmd --reload

certbotコマンドで証明書を作成します。

# certbot certonly --standalone --preferred-challenges http --agree-tos --email foo@example.com -d vpn.example.com
Saving debug log to /var/log/letsencrypt/letsencrypt.log

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
Would you be willing, once your first certificate is successfully issued, to
share your email address with the Electronic Frontier Foundation, a founding
partner of the Let's Encrypt project and the non-profit organization that
develops Certbot? We'd like to send you email about our work encrypting the web,
EFF news, campaigns, and ways to support digital freedom.
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
(Y)es/(N)o: N
Account registered.
Requesting a certificate for vpn.example.com

Successfully received certificate.
Certificate is saved at: /etc/letsencrypt/live/vpn.example.com/fullchain.pem
Key is saved at:         /etc/letsencrypt/live/vpn.example.com/privkey.pem
This certificate expires on 2025-05-01.
These files will be updated when the certificate renews.
Certbot has set up a scheduled task to automatically renew this certificate in the background.

- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
If you like Certbot, please consider supporting our work by:
 * Donating to ISRG / Let's Encrypt:   https://letsencrypt.org/donate
 * Donating to EFF:                    https://eff.org/donate-le
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -

うまくいかないときは、ポート80を使っているWebサーバ(Apacheなど)を止めて試してみてください。

OpenConnectサーバをインストール

OpenConnect Server (ocserv)をインストールします。

# dnf -y install ocserv

firewalldを設定します。

# firewall-cmd --add-port=443/tcp --permanent
# firewall-cmd --add-port=443/udp --permanent
# firewall-cmd --permanent --add-masquerade
# firewall-cmd --reload

OpenConnectの設定ファイルを編集します。(/etc/ocserv/ocserv.conf)

  • auth = "pam"をコメントアウト。
  • auth = "plain[passwd=/etc/ocserv/ocpasswd]" を追加。
#auth = "pam"                                                      
#auth = "pam[gid-min=1000]"
#auth = "plain[passwd=./sample.passwd,otp=./sample.otp]"
#auth = "certificate"
#auth = "radius[config=/etc/radiusclient/radiusclient.conf,groupconfig=true]"
auth = "plain[passwd=/etc/ocserv/ocpasswd]"
  • server-certserver-keyにLet's Encryptで作ったファイルのパスを入れる
server-cert = /etc/letsencrypt/live/vpn.example.com/fullchain.pem
server-key = /etc/letsencrypt/live/vpn.example.com/privkey.pem
  • default-domainを書き換える
# The default domain to be advertised
default-domain = vpn.example.com
  • ipv4-networkを書き換える
# An alternative way of specifying the network:
ipv4-network = 192.168.1.0/24
  • dnsを設定する
dns = 8.8.8.8
dns = 8.8.4.4

OpenConnectサーバの起動

ocservを有効化して起動します。

# systemctl enable ocserv
# systemctl start ocserv

ユーザー作成

OpenConnectに接続するときに使うユーザーを作成します。

# ocpasswd -c /etc/ocserv/ocpasswd nakamura
Enter password: 
Re-enter password: 

これでインストールは完了です。
クライアントから接続してみましょう。

参考

次の情報を参考にしました。

おまけ

私は、中国旅行に行く前にOpenConnectサーバを立ち上げています。自宅のサーバでやると、金盾に目をつけられて自宅のIPへのアクセスが塞がれる恐れがあるため、安価で時間課金式のVPNサーバを借りてVPNサーバを立ち上げています。(帰国したら、お金の節約のため、サーバをすぐに削除)

どういう時に塞がれるのかなどは、私にはあまり経験がなく、よくわかりません。渡航中の接続に安定性を求めるのであれば、自分で立てたVPNサーバだけに頼るのではなく、実績と信頼のあるVPN業者のサービスを契約したほうがいいのではないかと思います。

0
0
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
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?