我的电脑上有连到公司各个环境和我自己的云主机的一堆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
网友评论