【问题标题】:How to manage multiple ssh keys in the ~/.ssh directory如何管理 ~/.ssh 目录中的多个 ssh 密钥
【发布时间】:2014-07-08 05:34:55
【问题描述】:

我相信我们都会不时收到此错误:

$ git push origin master
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

典型的补救措施是简单地创建一个公钥/私钥对并与您的 git 主机共享(在我的例子中是 bitbucket,与他们的instructions)

但问题是,我有很多帐户需要我有一个公钥/私钥对(例如,我需要保存一个密钥才能连接到 AWS.. 等等).. 所以我要做的是我创建这些密钥并将它们保存在单独的目录中,即

~/.ssh $ find .
./awskeys
./awskeys/id_rsa
./awskeys/id_rsa.pub
./bitbucket
./bitbucket/id_rsa
./bitbucket/id_rsa.pub

但是这个错误时不时地弹出..要解决它,我必须将相关键移回根目录〜/ .ssh。这对我来说似乎不对。我怎样才能可靠地做到这一点?

【问题讨论】:

  • 您可以对所有这些服务使用相同的密钥对(只要它们都不知道您的私钥)。
  • 我的 .ssh 文件夹中有多个密钥,当我推送 git 提交时,它会询问要使用哪一个。只是给他们不同的名字。
  • @Havenard 很不方便

标签: git github ssh bitbucket


【解决方案1】:

你可以在任何你想要的地方拥有它们,但是它们的权限和父文件夹的权限需要严格:

  • 父文件夹没有可写访问权限(对于其他人和所有)
  • 644 表示公钥
  • 600 为私钥。

那么你:

  • 在~/.ssh/config (example here) 中声明这些不同的键
  • 更改远程 url 以使用 ~/.ssh/config 文件的适当条目,该文件描述了要使用的正确 ssh 密钥。

这意味着像这样的条目:

Host mygithub
    User           git
    IdentityFile   ~/.ssh/mypath/mykey # wherever your "new" key lives
    IdentitiesOnly yes

允许您将 git@github.com:username/repo 之类的 url 替换为:

git remote set-url origin mygithub:username/repo

【讨论】:

  • 嗯很有趣..我得看看这个
  • @abbood 我已经添加了一个例子。
  • @abbood 这真的是管理多个 ssh 密钥的标准方式。
  • 好东西.. 我更新了标题以更好地反映这个讨论
猜你喜欢
  • 2020-04-09
  • 1970-01-01
  • 2012-07-09
  • 1970-01-01
  • 2021-11-03
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2013-02-25
相关资源
最近更新 更多