SSH – Could Not Resolve Hostname with ~/.ssh/config and Aliases

linux-networkingssh

I have a config file ~/.ssh/config that looks something like this:

Host *.texas
  User john

Host *.texas !gateway.texas
  HostName %h.unitedstates.com
  ProxyJump gateway.texas

Host gateway.texas
  HostName ssh.texas.unitedstates.com

Host dallas austin
  HostName %h.texas
  RemoteCommand zsh -l

In summary, I expect that ssh dallas resolves to something like ssh -J ssh.texas.unitedstates.com john@dallas.texas.unitedstates.com zsh -l while ssh houston.texas resolves to ssh -J john@ssh.texas.unitedstates.com john@houston.texas.unitedstates.com. The second example works as expected but not the first one (unresolved hostname). I suspect that it has something to do with the %h.texas which should in turn be resolved as %h.unitedstates.com

Could anyone help?

Thanks!

Best Answer

As mentioned in the comments, by default recursive hostname resolution is not possible, but several options can help, including Match and CanonicalDomains/CanonicalHostName. In the end, I turned the problem around with something like this:

CanonicalDomains texas.unitedstates.com
CanonicalizeFallbackLocal no
CanonicalizeHostname yes

Host *.texas.unitedstates.com
  User john
  IdentityFile ~/.ssh/texas

Host *.texas.unitedstates.com !ssh.texas.unitedstates.com
  HostName %h
  ProxyJump ssh.texas.unitedstates.com

Host dallas austin
  HostName %h.texas.unitedstates.com
  RequestTTY yes
  RemoteCommand tmux new -As remote

More information on Canonicalization and on Match.