我想使用多个私钥连接到不同的服务器或同一服务器的不同部分(我的用途是服务器的系统管理,Git的管理,以及同一服务器内的正常Git使用) . 我试过简单地堆叠 id_rsa 文件中的密钥无济于事 .
id_rsa
显然,直接的方法是使用该命令
ssh -i <key location> login@server.example.com
这非常麻烦 .
关于如何更轻松地做这件事的任何建议?
您可以指示ssh在连接时连续尝试多个键 . 这是如何做:
$ cat ~/.ssh/config IdentityFile ~/.ssh/id_rsa IdentityFile ~/.ssh/id_rsa_old IdentityFile ~/.ssh/id_ed25519 # ... and so on $ ssh server.example.com -v .... debug1: Next authentication method: publickey debug1: Trying private key: /home/example/.ssh/id_rsa debug1: read PEM private key done: type RSA debug1: Authentications that can continue: publickey debug1: Trying private key: /home/example/.ssh/id_rsa_old debug1: read PEM private key done: type RSA .... [server ~]$
这样您就不必指定哪个键可以与哪个服务器一起使用 . 它只会使用第一个工作密钥 .
如果给定服务器愿意接受密钥,您也只能输入密码 . 如上所示,即使有一个密码,ssh也没有尝试为 .ssh/id_rsa 请求密码 .
.ssh/id_rsa
当然,它不像其他答案那样超出每服务器配置,但至少你不必为你连接的所有服务器添加配置!
我曾经遇到过这个问题,当时我有两个Bitbucket帐户并且想要为两者存储单独的SSH密钥 . 这对我有用 .
我创建了两个单独的ssh配置,如下所示 .
Host personal.bitbucket.org HostName bitbucket.org User git IdentityFile /Users/username/.ssh/personal Host work.bitbucket.org HostName bitbucket.org User git IdentityFile /Users/username/.ssh/work
现在,当我不得不从我的工作帐户克隆存储库时 - 命令如下 .
git clone git@bitbucket.org:teamname/project.git
我不得不将此命令修改为:
git clone git@**work**.bitbucket.org:teamname/project.git
同样,我的个人帐户中的克隆命令必须修改为
git clone git@personal.bitbucket.org:name / personalproject.git
有关更多信息,请参阅this link .
在运行OpenSSH_5.3p1,OpenSSL 1.0.1e-fips的Centos 6.5上,我通过重命名我的密钥文件解决了这个问题,因此没有一个具有默认名称 . 我的.ssh目录包含id_rsa_foo和id_rsa_bar,但没有id_rsa等 .
来自我的 .ssh/config :
.ssh/config
Host myshortname realname.example.com HostName realname.example.com IdentityFile ~/.ssh/realname_rsa # private key for realname User remoteusername Host myother realname2.example.org HostName realname2.example.org IdentityFile ~/.ssh/realname2_rsa User remoteusername
等等 .
foo:~$ssh-add ~/.ssh/xxx_id_rsa
确保在添加之前测试它:
ssh -i ~/.ssh/xxx_id_rsa username@example.com
如果您有任何错误问题,有时更改文件的安全性有助于:
chmod 0600 ~/.ssh/xxx_id_rsa
我同意Tuomas关于使用ssh-agent的观点 . 我还想为工作添加第二个私钥,this tutorial对我来说就像一个魅力 .
步骤如下:
$ ssh-agent bash
$ ssh-add /path.to/private/key 例如 ssh-add ~/.ssh/id_rsa
$ ssh-add /path.to/private/key
ssh-add ~/.ssh/id_rsa
由 $ ssh-add -l 验证
$ ssh-add -l
使用 $ssh -v <host url> 进行测试,例如 ssh -v git@assembla.com
$ssh -v <host url>
ssh -v git@assembla.com
之前的答案已正确解释了创建配置文件以管理多个ssh密钥的方法 . 我认为,还需要解释的重要事情是 replacement of a host name with an alias name while cloning the repository .
假设,你的 company's GitHub account's username is abc1234 . 假设你的 personal GitHub account's username is jack1234
并且,假设您已创建了两个RSA密钥,即 id_rsa_company 和 id_rsa_personal . 那么,你的 configuration 文件将如下所示:
# Company account Host company HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_company # Personal account Host personal HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_personal
现在,当您从公司的GitHub帐户克隆 repository (名为demo)时,存储库URL将类似于:
Repo URL: git@github.com:abc1234/demo.git
现在,在执行 git clone 时,您应该将上述存储库URL修改为:
git clone
git@company:abc1234/demo.git
Notice how github.com is now replaced with the alias "company" as we have defined in the configuration file.
同样,您必须根据配置文件中提供的别名修改个人帐户中存储库的克隆URL .
$ ssh-keygen -t rsa -C <email1@example.com>
another SSH key
$ ssh-keygen -t rsa -f ~/.ssh/accountB -C <email2@example.com>
现在, ~/.ssh/ 目录中应该存在两个公钥( id_rsa.pub , accountB.pub ) .
~/.ssh/
$ ls -l ~/.ssh # see the files of '~/.ssh/' directory
~/.ssh/config
$ nano ~/.ssh/config Host bitbucket.org User git Hostname bitbucket.org PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa Host bitbucket-accountB User git Hostname bitbucket.org PreferredAuthentications publickey IdentitiesOnly yes IdentityFile ~/.ssh/accountB
default
$ git clone git@bitbucket.org:username/project.git
accountB
$ git clone git@bitbucket-accountB:username/project.git
See More Here
answer from Randal Schwartz几乎帮助了我 . 我在服务器上有一个不同的用户名,所以我必须将 User 关键字添加到我的文件中:
Host friendly-name HostName long.and.cumbersome.server.name IdentityFile ~/.ssh/private_ssh_file User username-on-remote-machine
现在您可以使用友好名称进行连接:
ssh friendly-name
可以在OpenSSH man page上找到更多关键字 . NOTE: 列出的某些关键字可能已存在于/ etc / ssh / ssh_config文件中 .
现在,使用最新版本的git,我们可以在特定于存储库的git配置文件中指定 sshCommand .
[core] repositoryformatversion = 0 filemode = true bare = false logallrefupdates = true sshCommand = ssh -i ~/.ssh/id_rsa_user [remote "origin"] url = git@bitbucket.org:user/repo.git fetch = +refs/heads/*:refs/remotes/origin/*
IMPORTANT: You must start ssh-agent
在使用ssh-add之前,必须启动ssh-agent(如果它尚未运行),如下所示:
eval `ssh-agent -s` # start the agent ssh-add id_rsa_2 # where id_rsa_2 is your new private key file
请注意,eval命令在Windows上的GIT bash上启动代理 . 其他环境可能使用变体来启动SSH代理 .
使用ssh-agent作为密钥 .
您可以在 ~/.ssh 文件夹中创建名为 config 的配置文件 . 它可以包含:
~/.ssh
config
Host aws HostName *yourip* User *youruser* IdentityFile *idFile*
这将允许您连接到这样的机器
ssh aws
13 回答
您可以指示ssh在连接时连续尝试多个键 . 这是如何做:
这样您就不必指定哪个键可以与哪个服务器一起使用 . 它只会使用第一个工作密钥 .
如果给定服务器愿意接受密钥,您也只能输入密码 . 如上所示,即使有一个密码,ssh也没有尝试为
.ssh/id_rsa
请求密码 .当然,它不像其他答案那样超出每服务器配置,但至少你不必为你连接的所有服务器添加配置!
我曾经遇到过这个问题,当时我有两个Bitbucket帐户并且想要为两者存储单独的SSH密钥 . 这对我有用 .
我创建了两个单独的ssh配置,如下所示 .
现在,当我不得不从我的工作帐户克隆存储库时 - 命令如下 .
我不得不将此命令修改为:
同样,我的个人帐户中的克隆命令必须修改为
有关更多信息,请参阅this link .
在运行OpenSSH_5.3p1,OpenSSL 1.0.1e-fips的Centos 6.5上,我通过重命名我的密钥文件解决了这个问题,因此没有一个具有默认名称 . 我的.ssh目录包含id_rsa_foo和id_rsa_bar,但没有id_rsa等 .
来自我的
.ssh/config
:等等 .
确保在添加之前测试它:
如果您有任何错误问题,有时更改文件的安全性有助于:
我同意Tuomas关于使用ssh-agent的观点 . 我还想为工作添加第二个私钥,this tutorial对我来说就像一个魅力 .
步骤如下:
$ ssh-agent bash
$ ssh-add /path.to/private/key
例如ssh-add ~/.ssh/id_rsa
由
$ ssh-add -l
验证使用
$ssh -v <host url>
进行测试,例如ssh -v git@assembla.com
之前的答案已正确解释了创建配置文件以管理多个ssh密钥的方法 . 我认为,还需要解释的重要事情是 replacement of a host name with an alias name while cloning the repository .
假设,你的 company's GitHub account's username is abc1234 . 假设你的 personal GitHub account's username is jack1234
并且,假设您已创建了两个RSA密钥,即 id_rsa_company 和 id_rsa_personal . 那么,你的 configuration 文件将如下所示:
现在,当您从公司的GitHub帐户克隆 repository (名为demo)时,存储库URL将类似于:
现在,在执行
git clone
时,您应该将上述存储库URL修改为:Notice how github.com is now replaced with the alias "company" as we have defined in the configuration file.
同样,您必须根据配置文件中提供的别名修改个人帐户中存储库的克隆URL .
another SSH key
:现在,
~/.ssh/
目录中应该存在两个公钥( id_rsa.pub , accountB.pub ) .~/.ssh/config
:default
帐户克隆 .accountB
帐户克隆 .See More Here
answer from Randal Schwartz几乎帮助了我 . 我在服务器上有一个不同的用户名,所以我必须将 User 关键字添加到我的文件中:
现在您可以使用友好名称进行连接:
可以在OpenSSH man page上找到更多关键字 . NOTE: 列出的某些关键字可能已存在于/ etc / ssh / ssh_config文件中 .
现在,使用最新版本的git,我们可以在特定于存储库的git配置文件中指定 sshCommand .
IMPORTANT: You must start ssh-agent
在使用ssh-add之前,必须启动ssh-agent(如果它尚未运行),如下所示:
请注意,eval命令在Windows上的GIT bash上启动代理 . 其他环境可能使用变体来启动SSH代理 .
使用ssh-agent作为密钥 .
您可以在
~/.ssh
文件夹中创建名为config
的配置文件 . 它可以包含:这将允许您连接到这样的机器