I have the following set up in my ~/.ssh/config

match host devbox 
 compression yes 
 user hari 
 port 22 

match originalhost devbox exec "~/.ssh/check_if_outside_home.sh" 
 hostname devbox.harisund.com 

The idea is this -

  • Always connect to (this will work if I am already at the home network)
  • Connect instead to devbox.harisund.com , if I am not within the home LAN

However, with verbose logging, I see this -

  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 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 "" port 22 
 17 debug2: ssh_connect_direct: needpriv 0 
 18 debug1: Connecting to [] port 22. 
 19 debug2: fd 3 setting O_NONBLOCK 
 20 debug1: connect to address port 22: Connection timed out 
 21 ssh: connect to host port 22: Connection timed out 

In line 4, it detects the first stanza in ~/.ssh/config. At this point, hostname gets changed to All good so far.

In line 7, it reaches the second stanza.

In line 8, it checks if we are outside home, and it returns 0. As expected.

Line 12 says it's a match, which means we should change our hostname to devbox.harisund.com

However, in line 16, we see it is still using the local hostname as it was set.

Why ? Is this expected behavior?

1 Answer 1


That's just as designed and documented in man 5 ssh_config:

For each parameter, the first obtained value will be used. The configuration files contain sections separated by Host specifications, and that section is only applied for hosts that match one of the patterns given in the specification. The matched host name is usually the one given on the command line (see the CanonicalizeHostname option for exceptions).

Since the first obtained value for each parameter is used, more host-specific declarations should be given near the beginning of the file, and general defaults at the end.

So in short, your match originalhost can't overwrite a pre-existing hostname, swap the order and you should be fine.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .