
~/.ssh/config には次の設定があります。
match host devbox
compression yes
user hari
port 22
hostname 192.168.9.7
match originalhost devbox exec "~/.ssh/check_if_outside_home.sh"
hostname devbox.harisund.com
アイデアはこんな感じ -
- 常に192.168.8.15に接続してください(すでにホームネットワークに接続している場合は機能します)。
- 自宅のLANに接続していない場合は、devbox.harisund.comに接続してください。
ただし、詳細なロギングでは次のことがわかります。
1 OpenSSH_7.2p2 Ubuntu-4ubuntu2.2, OpenSSL 1.0.2g 1 Mar 2016
2 debug1: Reading configuration data /home/hsundararaja/.ssh/config
3 debug2: checking match for 'host devbox' host devbox originally devbox
4 debug3: /home/hsundararaja/.ssh/config line 734: matched 'host "devbox"'
5 debug2: match found
6 debug2: checking match for 'originalhost devbox exec "~/.ssh/check_if_outside_home.sh"' host 192.168.9.7 originally devbox
7 debug3: /home/hsundararaja/.ssh/config line 744: matched 'originalhost "devbox"'
8 debug1: Executing command: '~/.ssh/check_if_outside_home.sh'
9 debug1: permanently_drop_suid: 14741
10 debug3: command returned status 0
11 debug3: /home/hsundararaja/.ssh/config line 744: matched 'exec "~/.ssh/check_if_outside_home.sh"'
12 debug2: match found
13 debug1: /home/hsundararaja/.ssh/config line 839: Applying options for *
14 debug1: Reading configuration data /etc/ssh/ssh_config
15 debug1: /etc/ssh/ssh_config line 19: Applying options for *
16 debug2: resolving "192.168.9.7" port 22
17 debug2: ssh_connect_direct: needpriv 0
18 debug1: Connecting to 192.168.9.7 [192.168.9.7] port 22.
19 debug2: fd 3 setting O_NONBLOCK
20 debug1: connect to address 192.168.9.7 port 22: Connection timed out
21 ssh: connect to host 192.168.9.7 port 22: Connection timed out
行4では、~/.ssh/configの最初のセクションを確認してください。この時点で、ホスト名は192.168.9.7に変更されます。今まではそんなに良くなった。
行7では、2番目のスタンザに到達します。
8行目では、私たちが家の外にいることを確認して0を返します。予想通り。
12行目ではこれが一致すると言います。これは、ホスト名をdevbox.harisund.comに変更する必要があることを意味します。
ただし、行16では、設定されたローカルホスト名がまだ使用されていることがわかります。
なぜ?これが予想される動作ですか?
答え1
これは設計され文書化されているとおりですman 5 ssh_config
。
各パラメータに対して最初に取得された値が使用されます。構成ファイルには、ホスト仕様で区切られたセクションが含まれています。このセクションは、仕様で提供されているパターンの1つと一致するホストにのみ適用されます。一致するホスト名は通常、コマンドラインで指定されたホスト名です(例外についてはCanonicalizeHostnameオプションを参照)。
各パラメータで最初に取得した値を使用するため、ファイルの先頭にはより多くのホスト固有の宣言を提供し、最後に一般的なデフォルト値を提供する必要があります。
つまり、match originalhost
既存のアイテムを上書きすることはできず、hostname
順序を変更してもかまいません。