如何将具有相同配置的多台机器添加到 ~/.ssh/config?

K.W*_*ter 7 ssh command-line

说,我~/.ssh/config有几行:

Host machine1
      User user
      HostName machine1
      ProxyCommand  ssh server nc %h %p 2> /dev/null
Run Code Online (Sandbox Code Playgroud)

这工作正常,但问题是我有很多machinesmachine1, machine2, machine3, ...那么如何在不手动复制相同类型的行的情况下设置所有这些行

des*_*ert 12

你可以这样做:

Host machine1 machine2 machine3
      User user
      ProxyCommand ssh server nc %h %p 2>/dev/null
Run Code Online (Sandbox Code Playgroud)

您只需要列出该Host行中的主机,以空格分隔,HostName如果它与您在该Host行中给出的名称不同,则可以省略。请参阅ssh 配置中的多个类似条目 · U&L

为了进一步简化它,有通配符*?具有通常的含义,因此Host machine?对于您的示例来说也是可能的。

  • `HostName %h` 是多余的,如果您不打算更改主机名,则不需要它。 (3认同)
  • @K.Wanter 因为我 [第一次] 没有被听到(https://askubuntu.com/questions/991523/how-do-i-add-multiple-machines-with-the-same-configuration-to- ssh-config/991534#comment1598995_991527),`HostName %h` 没用。只需跳过`HostName` 行。 (2认同)

mur*_*uru 7

如果您的主机名符合某个模式,您可以使用 SSH 的模式:

您可以在~/.ssh/config. 来自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.?
Run Code Online (Sandbox Code Playgroud)

因此,如果您想代理 中的所有内容*.example.com,则在您的~/.ssh/config中放置:

Host *.example.com
    User user
    ProxyCommand ssh server nc %h %p 2> /dev/null
Run Code Online (Sandbox Code Playgroud)

或者,使用ssh自己的选项,您可以避免使用 netcat:

Host *.example.com
    User user
    ProxyCommand ssh -qW %h:%p server
Run Code Online (Sandbox Code Playgroud)

来自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.
Run Code Online (Sandbox Code Playgroud)