sshopensshssh-config

Is there a way to use a previously specified ssh-config entry in specifying `Hostname`?


edit: I have seen cases where this works and cases where this doe not and I'm not sure I follow when/why it does.

Suppose I have a complicated enough entry where I specify multiple parameters to get to thathost:

Host thathost
    ControlMaster auto
    ServerAliveInterval 8
    ConnectTimeout 8
    Hostname 192.168.5.1
    User mememe
    ProxyJump thejumpbox

I want to re-use this definition in creating additional entries that provide different functionality by adding or overriding some configs. Specifically specifying an alternate port (no, I don't want it on the command-line).

Ideally I'd be able to do something like

Host theirhost
    Hostname thathost
    User themthem

or

Host my-remote-serialport
    Hostname thathost
    RequestTTY yes
    RemoteCommand some-script-that-launches-kermit-on-a-specific-dev-tty

or

Host my-remote-serialport
    Hostname thathost
    Port 3004

I'm strictly looking to specify one host in terms of another existing one, I'm not looking to modify my Host entries to match some pattern "tricks".

Obviously I can utilize ProxyCommand ssh -q nc thathost... or ProxyJump thathost +Hostname localhost followed by all the other overrides (well for port override would pass that to nc) but that's both ugly and wasteful (an extra session) - please don't answer with that.

For me this has been the missing feature of ssh-config, but maybe I did not look hard enough.


Solution

  • It can't be used in the way you asked, like reuse a hostname definition, but the provided solution of ssh can solve much more problems.

    A host rule can span multiple hosts.

    Host thathost theirhost my-remote-serialport
        ControlMaster auto
        ServerAliveInterval 8
        ConnectTimeout 8
        Hostname 192.168.5.1
        User mememe
        ProxyJump thejumpbox
    

    But obviously this doesn't solve your problem with modifying some properties.
    The trick is, that the ssh config uses the first wins strategy for properties.

    In your case you just has to add the modifications in front of the main config

    Host theirhost
        Hostname thathost
        User themthem
    
    Host my-remote-serialport
        Hostname thathost
        Port 3004
    
    Host thathost theirhost my-remote-serialport
        ControlMaster auto
        ServerAliveInterval 8
        ConnectTimeout 8
        Hostname 192.168.5.1
        User mememe
        ProxyJump thejumpbox
    

    theirhost is defined at two places, the properties Hostname and User are taken from the first definition, all other properties from the second definition.

    The host part also accepts wildcards, example for a jumpbox with multiple reverse ssh endpoints:

    HOST my_1
       port 2001
    
    HOST my_2
       port 2002
    
    HOST my_3
       port 2003
    
    HOST my_*
       user pi
       hostname localhost
       ProxyJump thejumpbox