ssh 配置中有多个类似的条目

ssh 配置中有多个类似的条目

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

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

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

除了在我的文件中重复上述结构 30 次之外config,还有其他方法来定义机器范围吗?

答案1

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.

...

 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).

所以:

Host XXX1 XXX2 XXX3
  HostName %h.YYY.com

答案2

为了最小化设置,你可以有一个.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

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

答案3

只需使用*

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"

答案4

这对我有用:

CanonicalizeHostname 是
CanonicalDomains xxx.auckland.ac.nz yyy.auckland.ac.nz

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

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

bluebottle:~ user_one$ ssh itslogprd05
[电子邮件受保护]的密码: 

相关内容