在同一服务器内)。我试过简单地堆叠id_rsa文件中的密钥无济于事。
显然,直接的方法是使用 命令 </跨度>
ssh -i&lt;密钥位置&gt; login@server.example.com
这非常麻烦。
关于如何更轻松地做这件事的任何建议?
您可以创建一个名为的配置文件 config 在你的 ~/.ssh 夹。它可以包含:
config
~/.ssh
Host aws HostName *yourip* User *youruser* IdentityFile *idFile*
这将允许您连接到这样的机器
ssh aws
现在,使用最新版本的git,我们可以指定 的 sshCommand 强> 在特定于存储库的git配置文件中。
[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/*
在运行OpenSSH_5.3p1,OpenSSL 1.0.1e-fips的Centos 6.5上,我通过重命名我的密钥文件解决了这个问题,因此没有一个具有默认名称。我的.ssh目录包含id_rsa_foo和id_rsa_bar,但没有id_rsa等。
生成SSH密钥:
$ ssh-keygen -t rsa -C <email1@example.com>
生成 another SSH key :
another SSH key
$ ssh-keygen -t rsa -f ~/.ssh/accountB -C <email2@example.com>
现在,两个公钥( 的 id_rsa.pub 强> , 的 accountB.pub 强> )应该存在于 ~/.ssh/ 目录。
~/.ssh/
$ ls -l ~/.ssh # see the files of '~/.ssh/' directory
创建配置文件 ~/.ssh/config 具有以下内容:
~/.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 帐户。
default
$ git clone git@bitbucket.org:username/project.git
克隆自 accountB 帐户。
accountB
$ git clone git@bitbucket-accountB:username/project.git
的 在这里查看更多 强>
从我的 .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 # different private key for realname2 User remoteusername
等等。
该 Randal Schwartz的回答 几乎帮助了我。 我在服务器上有一个不同的用户名,所以我不得不添加 的 用户 强> 关键字到我的文件:
Host friendly-name HostName long.and.cumbersome.server.name IdentityFile ~/.ssh/private_ssh_file User username-on-remote-machine
现在您可以使用友好名称进行连接:
ssh friendly-name
可以在上找到更多关键字 OpenSSH手册页 。 的 注意: 强> 列出的某些关键字可能已存在于您的 的/ etc / SSH / ssh_config中 文件。
的 重要提示:您必须启动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作为密钥。