0

I want to setup a ssh connection with different configurations depending on some conditions. Much of the parameters in the aliases are the same. Is there a way to avoid repeating them in each alias?

In the example below, x is variable and y = 1 is constant. Ideally, y should be defined once.

Match host foo exec c1
    x = 1
    y = 1

Match host foo exec c2
    x = 2
    y = 1

# Some more `Match host foo` for foo on different conditions

Host foo
    x = 42
    y = 1
0

1 Answer 1

0

In the example you posted, you can place

Host foo
    y = 1

before the whole snippet. See Are host configurations in the SSH config merged? You can still have Host foo at the end, to pass x = 42 for cases where x is not defined by all the previous content. The point is the first relevant occurrence is used.

Well, the first relevant occurrence is used, unless noted otherwise in the manual. We don't know what your y is. If y is a parameter for which multiple values can be specified then the "first one wins" rule does not apply.

I assume the rule does apply for y. In the example you posted, removing y = 1 from everywhere but under Host foo should also work. I mean if none of Match host foo exec … defines y, placing y = 1 under Host foo will be the first relevant occurrence for foo, regardless if it's placed before, in between or after the other Match sections.

A useful general hint is:

Don't think of Host/Match as separate "sections", in general – think of it as a single large config where some settings are filtered.

(Credit to this answer.)


If you really need y = 1 in many places of the config (maybe because y is a parameter for which "first one wins" does not apply and thus the above part of the answer is not helpful), put this line in a separate regular file and Include the file from the main config in all the places.

Include
Include the specified configuration file(s). Multiple pathnames may be specified and each pathname may contain glob(7) wildcards and, for user configurations, shell-like ~ references to user home directories. Wildcards will be expanded and processed in lexical order. Files without absolute paths are assumed to be in ~/.ssh if included in a user configuration file or /etc/ssh if included from the system configuration file. Include directive may appear inside a Match or Host block to perform conditional inclusion.

(Source: man 5 ssh_config.)

Example:

Match host foo exec c1
    x = 1
    Include default-y

Match host foo exec c2
    x = 2
    Include default-y

# Some more `Match host foo` for foo on different conditions

Host foo
    x = 42
    Include default-y

where default-y is either ~/.ssh/default-y or /etc/ssh/default-y, depending on if the snippet is in a user configuration file (like ~/.ssh/config) or in the global configuration file (like /etc/ssh/ssh_config).

1
  • I like the second option more. My problem with the first is defining the same alias in multiple places - that will not end well.
    – rkochar
    Commented Apr 28 at 16:05

You must log in to answer this question.

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