SSH - Too Many Authentication Failures 解决办法

我的电脑上有连到公司各个环境和我自己的云主机的一堆ssh key, 通过ssh-add统一管理, 刚开始用起来很方便, 但是随着ssh key个数的增加, 开始出现:

Received disconnect from 10.10.10.10: 2: Too many authentication failures for spike

必须打开一个新终端再次执行 ssh, 重试 2~3 次也是常有的.

WHY

查找了一下原因, 是因为 ssh 每次连接的时候会尝试使用每一个ssh key, 直到找到一个能用的, 尝试的顺序也是随机的. 但是这个重试次数是有限制的(默认值是6, 可以通过MaxAuthTries配置修改)

HOW

第一个方案:

所以有了第一个解决方案, 当你的ssh key很多的话, 调大MaxAuthTries就可以了. 弊端, 不灵活 & 浪费可耻.

最优方案

通过某些配置可以明确标明哪个 ssh key 对应着哪些主机, 下面来看一下如何配置:
编辑 ~/.ssh/config, 如果没有则创建, 内容如下:

Host *.example.com
  IdentitiesOnly yes
  IdentityFile ~/.ssh/example_rsa
Host secure.example.com
  IdentitiesOnly yes
  IdentityFile ~/.ssh/secure_rsa
Host *.other.domain
  IdentitiesOnly yes
  IdentityFile ~/.ssh/other_rsa

当通配的主机需要用到多个 ssh key 的时候, IdentityFile也可以配多个

Host *.example.com
  IdentitiesOnly yes
  IdentityFile ~/.ssh/example_rsa
  IdentityFile ~/.ssh/example_dsa

也可以使用命令行:

ssh -o IdentitiesOnly=yes -i ~/.ssh/example_rsa foo.example.com
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容