ssh是否自动加载`.ssh`中的所有内容?

根据以下文章 ,似乎当你ssh到一个盒子,默认情况下,它会加载~/.ssh所有密钥(公共和私人)在我连接的服务器上进行身份validation。

这是真的? 还是你必须configuration你的~/.ssh/config文件并将IdentifyFiles映射到服务器?

我想了解身份validation如何与您的本地计算机上的多个私钥。

我假设你的意思是你想要在客户端上使用多个私钥,而不是在服务器上。 默认情况下,ssh客户端加载:

对于协议版本1

 ~/.ssh/identity 

对于协议版本2:

 ~/.ssh/id_dsa ~/.ssh/id_ecdsa ~/.ssh/id_ed25519 ~/.ssh/id_rsa 

它会尝试所有这些键(直到第一个成功)。

如果你想添加其他键,你有两个select:

1.通过〜/ .ssh / config添加每个主机select

你已经提到过这个,所以我假设你很熟悉它。 总之它看起来像这样:

 Host host1 HostName host1.example.com IdentityFile ~/.ssh/id_rsa_host1 Host host2 HostName host2.example.com IdentityFile ~/.ssh/id_rsa_host2 

2.使用ssh-agent

configurationssh-agent(在google上有大量的文档,例如http://mah.everybody.org/docs/ssh),OpenSSH默认使用它。 您可以添加通过

 ssh-add -i $KEY_FILE 

每个私钥。

3.手动做

用-i选项启动ssh客户端来select你想要使用的密钥。

 ssh -i $KEY_FILE 

我会build议使用选项2(ssh-agent),除此之外,它还会给你一些好处,但这一切都取决于你的情况。

根据手册:

  ~/.ssh/identity ~/.ssh/id_dsa ~/.ssh/id_ecdsa ~/.ssh/id_ed25519 ~/.ssh/id_rsa Contains the private key for authentication. These files contain sensitive data and should be readable by the user but not acces‐ sible by others (read/write/execute). ssh will simply ignore a private key file if it is accessible by others. It is possible to specify a passphrase when generating the key which will be used to encrypt the sensitive part of this file using 3DES. 

所以是的,SSH将加载所有这些键。 如果你有一台服务器需要连接一个特定的密钥,你应该在〜/ .ssh / config中configuration该主机

请注意,不需要拥有多个密钥对。 您的公钥是公开的,可以安全地附加到所有连接到的服务器的〜/ .ssh / authorized_keys文件中。 没有你的私钥,任何人都不能做任何事情。

唯一的情况下,您可能需要一个服务器的不同的密钥是,如果你没有访问您的authorized_keys文件(因为它是一个SFTP或SCP或仅rsync的服务器),你想重新使用已经configuration的密钥对。