OpenConnect はCisco製のVPN接続方式です。
Ubuntu 16.04にOpenConnect をインストールする手順を紹介します。
タッチでジャンプできる目次
コンソールでOpenConnect をインストール
- Ubuntu16.04
- 初期設定状態からインストール
- Freenomなどでドメインを取得済み
SSH
SSH" data-lang="CSS">$ su
apt update && apt upgrade -y
apt install ocserv
systemctl status ocserv
systemctl start ocserv
Certbot でLet’s Encryptより SSL証明書を取得します。
無料のドメインをFreenom で取得してAレコードを対象サーバーに向けている状態にします。
SSH
apt install software-properties-common
add-apt-repository ppa:certbot/certbot
apt update
apt install certbot
以下のコマンドでSSL証明書を作成します。
メールアドレスとドメイン名を自分のものに変更してください。
SSH
certbot certonly --standalone --preferred-challenges http --agree-tos --email [email protected] -d your-domain.com
証明書が作成できない場合は、80番ポートが空いているか、ドメインのDNSがサーバーに向いているかを確認しましょう。
OpenConnect の設定
編集が必要な箇所を探して置き換えます。
SSH
nano /etc/ocserv/ocserv.conf
ocserv.conf
auth = "plain[passwd=/etc/ocserv/ocpasswd]"
server-cert = /etc/letsencrypt/live/your-domain.com/fullchain.pem
server-key = /etc/letsencrypt/live/your-domain.com/privkey.pem
default-domain = your-domain.com
ipv4-network = 10.10.10.0
dns = 8.8.8.8
こちらをコメントアウト (行先頭に#をつける)
ocserv.conf
#route = 10.10.10.0/255.255.255.0
#route = 192.168.0.0/255.255.0.0
#route = fef4:db8:1000:1001::/64
#no-route = 192.168.5.0/255.255.255.0
OpenConnect 再起動
SSH
systemctl restart ocserv
cp /lib/systemd/system/ocserv.service /etc/systemd/system/ocserv.service
nano /etc/systemd/system/ocserv.service
ocserv.service
# コメントアウトする
#Requires=ocserv.socket
#Also=ocserv.socket
SSH
systemctl daemon-reload
systemctl stop ocserv.socket
systemctl disable ocserv.socket
systemctl restart ocserv.service
systemctl status ocserv
ユーザー追加
SSH
ocpasswd -c /etc/ocserv/ocpasswd [ユーザー名]
パスワードを設定
IPフォワーディングを有効に
SSH
nano /etc/sysctl.conf
# アンコメント (#を外す)
sysctl.conf
net.ipv4.ip_forward = 1
SSH
sudo sysctl -p
iptablesの編集
ifconfig で ens3やeth0を確認
SSH
iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
iptables -t nat -L POSTROUTING
iptables -I INPUT -p tcp --dport 443 -j ACCEPT
iptables -I INPUT -p udp --dport 443 -j ACCEPT
iptables-save > /etc/iptables.rules
SSH
nano /etc/systemd/system/iptables-restore.service
新規でファイルを作成します。
iptables-restore.service
[Unit]
Description=Packet Filtering Framework
Before=network-pre.target
Wants=network-pre.target
[Service]
Type=oneshot
ExecStart=/sbin/iptables-restore /etc/iptables.rules
ExecReload=/sbin/iptables-restore /etc/iptables.rules
RemainAfterExit=yes
[Install]
WantedBy=multi-user.target
SSH
sudo systemctl daemon-reload
sudo systemctl enable iptables-restore
スマホ / PCでOpenConnectへ接続
iPhone / Android


参考リンク
Set up OpenConnect VPN Server (ocserv) on Ubuntu 16.04/18.04 with Let’s Encrypt