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?

【macOS】 VPN接続時に特定のホストへのルーティングを自動で追加する方法

0
Last updated at Posted at 2026-02-27

はじめに

研究室や社内サーバーにVPN接続するとき、ローカルネットワークとVPN側の経路がぶつかって、特定ホストに届かないことがあります。

毎回 sudo route add ... を手で打つのは面倒なので、macOSのPPPフック(/etc/ppp/ip-up)でVPN接続時に自動追加する設定にします。

VPNで自分に割り当てられるIPはターゲットサーバーと末尾以外同じ(例: 192.168.xx.*)という前提です。

1. 自動ルーティングを設定する

/etc/ppp/ip-up を作成・編集します。

sudo mkdir -p /etc/ppp
sudo nano /etc/ppp/ip-up

以下を保存します(TARGET_SERVER は自分の環境に合わせて変更)。

貼り付け後、Ctrl+OEnter で保存し、Ctrl+X で終了します。

#!/bin/sh

# $1: インターフェース名 (ppp0 など)
# $4: VPN内で自分に割り当てられたIP

TARGET_SERVER="192.168.xx.y"

case "$4" in
    192.168.xx.*)
        /sbin/route add -host "$TARGET_SERVER" -interface "$1"
        ;;
esac

実行権限を付与します。

sudo chmod 755 /etc/ppp/ip-up

2. 動作確認

  1. VPNを切断して再接続
  2. ルーティングテーブルを確認
netstat -rn -f inet

192.168.xx.yppp0(または該当PPPインターフェース)に向いていれば成功です。

まとめ

.zshrcsudo を書かず、OS標準の ip-up を使うことで以下を実現できます。

  • 接続時に自動でルート追加
  • 手動実行や毎回のパスワード入力が不要
  • ネットワーク設定をシェル設定から分離できる
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?