How do I add multiple machines with the same configuration to ~/.ssh/config?

say, my ~/.ssh/config has some line:

Host machine1
      User user
      HostName machine1
      ProxyCommand  ssh server nc %h %p 2> /dev/null

and this works properly, but the problem is that I have lot of machines: machine1, machine2, machine3, ... so how can I set all of them without manually copy same kind of lines


You can do that this way:

Host machine1 machine2 machine3
      User user
      ProxyCommand ssh server nc %h %p 2>/dev/null

You just need to list the hosts in the Host line, separated by whitespace, and you can omit HostName if it's not different from the name you gave in the Host line. See Multiple similar entries in ssh config · U&L.

To simplify it even more there are the wildcards * and ? available with their usual meaning, so Host machine? would be evenly possible for your example.


If your hostnames fit a pattern, you can use SSH's patterns:

You can use patterns in ~/.ssh/config. From man ssh_config:

PATTERNS
     A pattern consists of zero or more non-whitespace characters, ‘*’ (a
     wildcard that matches zero or more characters), or ‘?’ (a wildcard that
     matches exactly one character).  For example, to specify a set of
     declarations for any host in the “.co.uk” set of domains, the following
     pattern could be used:

           Host *.co.uk

     The following pattern would match any host in the 192.168.0.[0-9] network
     range:

           Host 192.168.0.?

So, if you want to proxy everything in *.example.com, then in your ~/.ssh/config, put:

Host *.example.com
    User user
    ProxyCommand ssh server nc %h %p 2> /dev/null

Or, using ssh's own options, you can avoid netcat:

Host *.example.com
    User user
    ProxyCommand ssh -qW %h:%p server

From man ssh:

-W host:port
     Requests that standard input and output on the client be
     forwarded to host on port over the secure channel.  Implies -N,
     -T, ExitOnForwardFailure and ClearAllForwardings.