id_rsa
文件中,但无济于事。显然,一个直接的方法是使用以下命令:ssh -i <key location> login@server.example.com
这相当繁琐。
你有什么简便的方法建议吗?
来自我的.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
然后您可以使用以下内容进行连接:
ssh myshortname
ssh myother
等等。
IdentitiesOnly yes
еҸҜд»ҘйҳІжӯўдҪҝз”Ё~/.ssh/id_rsaжҲ–е…¶д»–иә«д»ҪйӘҢиҜҒдҝЎжҒҜгҖӮ(иҝҷжҳҜеҺҹе§Ӣзј–иҫ‘зҡ„еҶ…е®№) - user3338098当连接时,您可以指示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密钥。我认为,还需要解释的重要事情是在克隆存储库时使用别名替换主机名。
假设您的公司GitHub帐户用户名为abc1234,您的个人GitHub帐户的用户名为jack1234。
并且,假设您创建了两个RSA密钥,即id_rsa_company和id_rsa_personal。那么,你的配置文件将像下面这样:
# 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 帐户克隆名为 demo 的代码仓库时,仓库的 URL 将类似于:
Repo URL: git@github.com:abc1234/demo.git
现在,在执行git clone
时,你应该将上述仓库的URL修改为:
git@company:abc1234/demo.git
注意现在github.com已被我们在配置文件中定义的别名“company”所替代。
同样地,根据配置文件中提供的别名修改个人账户中仓库的克隆URL。
ssh-agent
,则应将 IdentitiesOnly yes
选项添加到您的配置文件中,否则ssh-agent
会尝试使用不同别名的密钥连接到给定别名的主机名相匹配的主机。 - William StanleyRandal 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手册页中找到。注意:列出的一些关键字可能已经存在于您的/etc/ssh/ssh_config文件中。
ssh-add ~/.ssh/xxx_id_rsa
在添加之前,请确保您进行测试:
ssh -i ~/.ssh/xxx_id_rsa username@example.com
如果您遇到错误问题,有时更改文件的安全性可以解决问题:
chmod 0600 ~/.ssh/xxx_id_rsa
ssh -i
指定身份文件时,为什么需要 ssh-add
? - atlex2Generate an SSH key:
$ ssh-keygen -t rsa -C <email1@example.com>
Generate another SSH key:
$ ssh-keygen -t rsa -f ~/.ssh/accountB -C <email2@example.com>
Now, two public keys (id_rsa.pub, accountB.pub) should be exists in the ~/.ssh/
directory.
$ ls -l ~/.ssh # see the files of '~/.ssh/' directory
Create configuration file ~/.ssh/config
with the following contents:
$ 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
Clone from default
account.
$ git clone git@bitbucket.org:username/project.git
Clone from the accountB
account.
$ git clone git@bitbucket-accountB:username/project.git
User git
指令的存在,您可以省略 repo URL 中的 git@
部分,从而缩短您的 clone
命令,如下所示: $ git clone bitbucket-accountB:username/project.git
我同意Tuomas的观点,建议使用ssh-agent。我还想为工作添加第二个私钥,这篇教程 对我非常有帮助。
步骤如下:
$ 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-agent
,但最近在我的 i3
窗口管理器中切换到了 Gnome 的 gnome-keyring
。原因很简单:Gnome Keyring 管理器可以自动添加和删除 ssh 密钥,无需我记住 ssh-add
命令。此外,它还提供了一个密码来解锁这些密钥(并在指定时间后超时以增强安全性)。各有所好。由于我在 Arch 上使用 Gnome 设置,所以与我的设置兼容,非常方便。如果您反对 Gnome,请忽略此评论。 - eduncan911ssh-agent bash
starts subsession. Also you can just evaluate the output of ssh-agent -s
like eval `ssh-agent -s`
. After that use ssh-add
- Piroxiljin对于我在 MacOs 上,唯一有效的解决方案是在文件~/.ssh/config
中添加以下内容:
Host *
IdentityFile ~/.ssh/your_ssh_key
IdentityFile ~/.ssh/your_ssh_key2
IdentityFile ~/.ssh/your_ssh_key3
AddKeysToAgent yes
your_ssh_key
没有任何扩展名,不要使用 .pub
。
$ eval "$(ssh-agent -s)"
$ ssh-add ~/.ssh/{private_key}
$ git clone git@{alias}:{github_user}/{repo}.git
然后在 ~/.ssh/config
中添加以下内容: Host github.com-{alias}
HostName github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/{private_key}
在 {repo}/.git/config
中添加以下内容: [remote "origin"]
url = git@github.com-{alias}:{github_user}/{repo}.git
fetch = +refs/heads/*:refs/remotes/origin/*
. gist - vcamargo现在,使用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/*
我之前遇到过这个问题,当时我有两个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
有关更多信息,请参阅此链接。