LoginSignup
7
6

More than 5 years have passed since last update.

VPN接続したときに、所定のIPをVPN経由で接続するようにする

Last updated at Posted at 2017-04-06

社内からのみ接続できるサーバがある。自宅で作業するときに、今までは自宅のMacからVPNで会社のMacにsshでログインし、そこからサーバに接続していた。

ただ、会社のMacを立ち上げた状態にしておかなければならないなど少々不便に感じたので、社内からのみ接続できるサーバは、自宅のMacからVPN経由で直接接続できるようにした。

以下は例として、192.168.10.0/24へのアクセスをVPN経由になるようにする方法について。

VPNを接続するときは/etc/ppp/ip-upを、解除するときは/etc/ppp/ip-downを実行するので、routeコマンドを使って所定のIPを追加、削除するスクリプトを作る。

cat <<'EOT' | sudo tee /etc/ppp/ip-up > /dev/null
#!/bin/sh
set -eu

if [ "$1" = "ppp0" ]; then
    /sbin/route add -net 192.168.10.0/24 -interface ppp0
fi
EOT
cat <<'EOT' | sudo tee /etc/ppp/ip-down > /dev/null
#!/bin/sh
set -eu

if [ "$1" = "ppp0" ]; then
    /sbin/route delete -net 192.168.10.0/24
fi
EOT

そして、作成したスクリプトを実行可能にする。

sudo chmod 755 /etc/ppp/ip-up /etc/ppp/ip-down

実行可能にしたらVPN接続をしてルートが追加されていることを確認する。それが確認できたら接続解除して、ルートが削除されていることを確認する。確認にはnetstatコマンドを使用する。

$ netstat -rn
Routing tables

Internet:
Destination        Gateway            Flags        Refs      Use   Netif Expire
...
192.168.10         ppp0               USc             1        0    ppp0
...

Internet6:
Destination                             Gateway                         Flags         Netif Expire
...

あとは、sshコマンドで実際に接続できることを確認する。

参考

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