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?

OpenVPNサーバの構築① ~OSインストール~

Posted at

どういう構成か

OpenVPN.jpg

  • パブリッククラウド上にOpenVPNサーバを構築して、必要なリソースにしかアクセスできないようにする

なぜ必要か

  • 正直、SaaSを利用した方が運用管理コストは低いしセキュリティもサービスベンダーに依存すればいい
  • コストを抑えたい、外部サービスから社内へ通信を許可したくない、IP制限があるなどの場合は有効

スペック

CPU メモリ OS ミドルウェア
1core 1GHz 1GB Ubuntu OepnVPN

サーバ設定

OSアップデート

apt update && apt upgrade

pingインストール

apt install iputils-ping

NW設定ファイルのバックアップ

mv /etc/netplan/01-netcfg.yaml /etc/netplan/01-netcfg.ymal.org

新規でNW設定ファイルを作成

vi /etc/netplan/99-netcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens160:
      addresses: [xx.xx.xx.xx/xx]  # サーバのIPアドレス
      nameservers:
        addresses: 
          - xx.xx.xx.xx      # DNSサーバ
      routes:
        - to: default
          via: xx.xx.xx.xx    # DGW
    ens192:
      addresses: [xx.xx.xx.xx/xx]      # 複数NICがある場合
      nameservers:
        addresses: [xx.xx.xx.xx]         # 内部DNSサーバ
      routes:
        - to: xx.xx.xx.xx            # 独自のルーティング設定がある場合
          via: xx.xx.xx.xx/xx

権限設定

chmod 600 /etc/netplan/99-netcfg.yaml

以下のコマンド発行後にエンターを押して設定を反映

※10秒以内にエンターが押されなければ設定が元に戻る

netplan try --timeout 10

DNSにバグがあるので、以下の設定を行う

スタブリゾルバを使用しない

sed -i"" -e "s/.*DNSStubListener=yes/DNSStubListener=no/" /etc/systemd/resolved.conf

リゾルバファイルのシンボリックリンク先を変更

cd /etc
ln -sf ../run/systemd/resolve/resolv.conf resolv.conf

再起動して反映

systemctl restart systemd-resolved.service

パケット転送を有効化

vi /etc/sysctl.conf

#28行目のコメントを解除
net.ipv4.ip_forward = 1

設定反映

sysctl -p

IPフォワーディングが有効か確認

sysctl net.ipv4.ip_forward

iptablesを起動する

systemctl start iptables

自動起動を有効にする

systemctl enable iptables

IPマスカレードの設定を行う

iptables -t nat -D POSTROUTING -s 172.20.0.0/16 -o ens160 -j MASQUERADE
iptables -t nat -D POSTROUTING -s 172.20.0.0/16 -o ens190 -j MASQUERADE

※172.20.0.0/16はOpenVPNのNWセグメント

設定を保存する

iptables-save > /etc/iptables/rules.v4
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?