ssh 配置中的多个类似条目

Ame*_*ina 255 ssh

假设我想ssh在我的.ssh config文件中使用相同的设置为 30 个服务器配置我的选项:

host XXX
     HostName XXX.YYY.com
     User my_username
     Compression yes
     Ciphers arcfour,blowfish-cbc
     Protocol 2
     ControlMaster auto
     ControlPath ~/.ssh/%r@%h:%p
     IdentityFile ~/.ssh/YYY/id_rsa
Run Code Online (Sandbox Code Playgroud)

这 30 台机器之间唯一变化的地方是XXX.

除了在我的config文件中重复上述结构 30 次之外,还有另一种方法来定义一系列机器吗?

Ign*_*ams 294

ssh_config(5)手册页:

 Host    Restricts the following declarations (up to the next Host key?
         word) to be only for those hosts that match one of the patterns
         given after the keyword.  If more than one pattern is provided,
         they should be separated by whitespace.
Run Code Online (Sandbox Code Playgroud)

...

 HostName
         Specifies the real host name to log into.  This can be used to
         specify nicknames or abbreviations for hosts.  If the hostname
         contains the character sequence ‘%h’, then this will be replaced
         with the host name specified on the commandline (this is useful
         for manipulating unqualified names).
Run Code Online (Sandbox Code Playgroud)

所以:

Host XXX1 XXX2 XXX3
  HostName %h.YYY.com
Run Code Online (Sandbox Code Playgroud)

  • 看来`%h` 功能出现在[OpenSSH 5.6 版](http://www.openssh.com/txt/release-5.6) 中。我想知道为什么我以前没有见过它 - Debian Squeeze 中的版本是 5.5。 (12认同)
  • 如果您使用的是旧操作系统或需要 `config` 不支持的规则,您总是可以编写一个简单的脚本来生成您的 `config`。 (2认同)

Gui*_*ent 130

为了最小化设置,你可以有一个.ssh/config这样的

Host X01
    HostName X01.YYY.com

Host X02
    HostName X02.YYY.com

...

Host X01 X02 ...
     User my_username
     Compression yes
     Ciphers arcfour,blowfish-cbc
     Protocol 2
     ControlMaster auto
     ControlPath ~/.ssh/%r@%h:%p
     IdentityFile ~/.ssh/YYY/id_rsa
Run Code Online (Sandbox Code Playgroud)

Host X01 X02 ...Host *如果每个主机都具有以下配置,则可以替换为

  • 这似乎是唯一真正帮助 OP(和我自己)的答案。 (5认同)
  • 来自 ssh_config 手册:由于使用了每个参数的第一个获取值,因此应在文件开头附近给出更多特定于主机的声明,并在末尾给出一般默认值。 (5认同)
  • `Host X01 X02 ...`可以替换为`*.YYY.com`吗?如果可行的话,这似乎更容易管理。 (2认同)

H.-*_*itt 63

只需使用 *

man ssh_config

模式 模式由零个或多个非空白字符、“*”(匹配零个或多个字符的通配符)或“?”组成。(恰好匹配一个字符的通配符)。例如,要为“.co.uk”域集中的任何主机指定一组声明,可以使用以下模式:

       Host *.co.uk

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

       Host 192.168.0.?

 A pattern-list is a comma-separated list of patterns.  Patterns within pattern-lists may be negated by preceding them with an
 exclamation mark (‘!’).  For example, to allow a key to be used from anywhere within an organisation except from the “dialup”
 pool, the following entry (in authorized_keys) could be used:

       from="!*.dialup.example.com,*.example.com"
Run Code Online (Sandbox Code Playgroud)

  • @user27915816 是的,你是对的,据我所知,没有办法做“模板”。您能做的最好的事情是将常量行分离成一个单一的 `Host *` 条目,并为每个 `Host XXX` 设置一个单独的条目,该条目仅包含不同的部分(即 `Hostname XXX.YYY.ZZZ`线)。 (6认同)
  • 唔。我认为模式与我需要的目的不同。它们将多个查询重定向到同一个 `config` 条目,但主机的参数是固定的(即模式不能用于 *模板* 参数)。我错了吗? (3认同)

小智 14

从 Ignacio Vazquez-Abrams 和 H.-Dirk Schmitt 的回答中,可以将以下内容添加到 .ssh/config

HOST XXX*
    HostName %h.YYY.com
    User myname
Run Code Online (Sandbox Code Playgroud)

然后,例如,您可以通过以下方式以 myname@XXX2.YYY.com 登录

ssh XXX2
Run Code Online (Sandbox Code Playgroud)


小智 9

这对我有用:

规范化主机名 是
规范域 xxx.auckland.ac.nz yyy.auckland.ac.nz

主机 *.xxx.auckland.ac.nz
   用户 myuser
主机 *.yyy.auckland.ac.nz
   用户 myuser

这允许人们使用域内的名称并更改用户名:

蓝瓶:~ user_one$ ssh itslogprd05
myuser@itslogprd05.xxx.auckland.ac.nz 的密码: