我的.ssh文件夹中可以有多个ssh密钥吗?


30

我可以创建多个ssh密钥,并将其重命名为用户友好名称,以便知道哪个密钥用于哪个网站等。

这样安全吗?

例如:

github_id_rsa
github_id_rsa.pub
..
...

连接时如何知道要检查哪个键?

现在在我的计算机上,当我查看known_hosts时,它们似乎在主机名之后都具有相同的密钥?



另外,已知的主机密钥肯定不相同,只需查看每个主机的行尾即可。
slhck 2011年

Answers:



44

您可以修改〜/ .ssh / config文件以将不同的标识文件用于不同的服务器。在您喜欢的编辑器中编辑〜/ .ssh / config,并添加一个适合您的情况的条目,如下所示:

Host *
IdentityFile ~/.ssh/id_rsa

Host *.github.*
IdentityFile ~/.ssh/github_id.rsa

Host *.someother.com
IdentityFile ~/.ssh/someother_id.rsa

上面的第一部分为所有主机设置了默认值,其他部分覆盖了应为每个与模式匹配的主机使用的默认值。如果每个主机的用户名都不同,则可以在该部分的遥控器上添加一个用户键,然后添加用户名。


3
无论目的地是GitHub还是其他站点,这都是对该问题最有用的答案。
皮埃尔

如果您有多个bitbucket帐户,并且不幸的是每个帐户都需要具有不同的密钥,则此方法不起作用。
John Little

chmod 400 ~/.ssh/id_rsachmod 400 ~/.ssh/foo_id.rsa可能是必要的。
T.Woody

@JohnLittle看看我的答案,该答案可以解决同一域的多用户帐户。
Sathishkumar Rakkiasamy,

4

您可以为具有多个用户帐户的任何站点设置多个ssh密钥

以下是我在GitHub.com开发中遵循的示例

配置文件示例

#Personal account
 Host github.com-<personal-account-name>
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_personal
 IdentitiesOnly yes



#Organization account
 Host github.com-<organization-name>
 HostName github.com
 User git
 IdentityFile ~/.ssh/id_rsa_work
 IdentitiesOnly yes

在添加新来源时

对于个人帐户

git remote add origin git@github.com-<personal-account-name>:<personal-account-name>/<repo-name>.git

对于组织帐户

git remote add origin git@github.com-<organization-name>:<organization-name>/<repo-name>.git

希望能帮助到你。

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.