7

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

2
  • Host machine* would work too, for common config where part of the name matches. Jan 2, 2018 at 15:20
  • 1
    @BoristheSpider See the last sentence of my answer.
    – dessert
    Jan 2, 2018 at 15:26

3 Answers 3

12

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.

10
  • 1
    +1 . . . or even use a ? or * wildcard such as machine? as described in the PATTERNS section of man ssh_config Jan 2, 2018 at 14:30
  • machine* does not work, there should be a briliant way like this, dont know how to do it
    – K.Wanter
    Jan 2, 2018 at 14:31
  • 3
    HostName %h is redundant, you don't need it if you're not going to change the hostname.
    – muru
    Jan 2, 2018 at 14:36
  • 1
    @K.Wanter SSH patterns don't understand ranges. You can use deep?? or deep* instead. (Or deep1? deep2? deep3? to be specific.)
    – muru
    Jan 2, 2018 at 14:37
  • 2
    @K.Wanter since I wasn't heard the first time, HostName %h is useless. Just skip the HostName line.
    – muru
    Jan 2, 2018 at 14:46
7

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.
-1
Host machine*
      User user
      ProxyCommand  ssh server nc %h %p 2> /dev/null

yes, the Hostname is redundant.

You must log in to answer this question.

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