LoginSignup
1
1

ssh config でマシンが所属するネットワークを判断して使用する設定を切り替える

Posted at

特定のホストにSSHする際に、LAN内からかインターネットからかで使用するIPやポート番号などが異なるのはよくあることかと思う。Linux限定だが、所属ネットワークを自動判別して接続する設定例を下に示す。内部的には ip コマンドでネットワークを判断している。判定時に外部アクセスしない点がよいと思っている。

なお、ホスト名は example アドレスは 192.168.0.5/24 であり、それが外部からだと 203.0.113.10 で接続できるものとする。

Match host example exec "test $(ip address show up to 192.168.0.0/24 | wc -c) != 0"
  Hostname 192.168.0.5
Host example
  Hostname 203.0.113.10

いたってシンプルである。

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