我想使用多个私钥连接到不同的服务器或同一服务器的不同部分(我的用途是服务器的系统管理、Git管理和同一服务器中的正常Git使用)。我尝试简单地将密钥堆叠在id_rsa文件中,但没有用。

显然,要做到这一点,一个简单的方法是使用命令

ssh -i <key location> login@server.example.com 

这很麻烦。

有什么建议可以让你做这件事更容易一些吗?


当前回答

Generate 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@部分,像这样缩短你的克隆命令:

    $ git clone bitbucket-accountB:username/project.git

这是该指令的唯一目的。如果你不需要它(例如,你总是从网站复制粘贴git克隆命令),你可以把它从配置中去掉。

点击这里查看更多信息

其他回答

Generate 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@部分,像这样缩短你的克隆命令:

    $ git clone bitbucket-accountB:username/project.git

这是该指令的唯一目的。如果你不需要它(例如,你总是从网站复制粘贴git克隆命令),你可以把它从配置中去掉。

点击这里查看更多信息

正如Atlassian博客页面上提到的, 在.ssh文件夹中生成一个配置文件,包括以下文本:

#user1 account
 Host bitbucket.org-user1
     HostName bitbucket.org
     User git
     IdentityFile ~/.ssh/user1
     IdentitiesOnly yes

 #user2 account
 Host bitbucket.org-user2
     HostName bitbucket.org
     User git
     IdentityFile ~/.ssh/user2
     IdentitiesOnly yes

然后,您可以简单地用后缀域签出,在项目中,您可以在本地配置作者名称等。

Randal Schwartz的回答几乎帮助了我。 我在服务器上有一个不同的用户名,所以我必须在我的文件中添加User关键字:

Host           friendly-name
HostName       long.and.cumbersome.server.name
IdentityFile   ~/.ssh/private_ssh_file
User           username-on-remote-machine

现在你可以使用friendly-name进行连接:

ssh friendly-name

更多关键字可以在OpenSSH手册页上找到。注意:列出的一些关键字可能已经出现在/etc/ssh/ssh_config文件中。

GitHub上的多个键对

1.0 SSH配置文件

1.1创建~/.ssh/config

1.2 chmod 600 ~/。ssh / config(必须)

1.3在文件中输入如下内容:

主机披萨 主机github。com 选择公共关系 IdentityFile ~ / . ssh / privatekey1

案例A:全新的Git克隆

使用此命令克隆Git:

$ git clone git@pizza:yourgitusername/pizzahut_repo.git

注意:如果以后要更改.ssh/config的主机名“pizza”,进入Git克隆文件夹,编辑.git/config文件URL行(见case B)

情况B:已经有Git克隆文件夹

2.1进入克隆文件夹,进入“。git”文件夹

2.2编辑配置文件

2.3将URL从*old更新为new:

(Old) URL = git@github.com:yourgitusername/pizzahut_repo.git

(New) URL = git@pizza:yourgitusername/pizzahut_repo.git

我喜欢在~/.ssh/config文件中设置以下方法:

# Configuration for GitHub to support multiple GitHub  keys
Host  github.com
  HostName github.com
  User git

# UseKeychain adds each keys passphrase to the keychain so you
# don't have to enter the passphrase each time.
  UseKeychain yes

# AddKeysToAgent would add the key to the agent whenever it is
# used, which might lead to debugging confusion since then
# sometimes the one repository works and sometimes the
# other depending on which key is used first.
#  AddKeysToAgent yes

# I only use my private id file so all private
# repositories don't need the environment variable
# `GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa"` to be set.
  IdentityFile ~/.ssh/id_rsa

然后在你的存储库中创建一个.env文件,其中包含要使用的ssh命令:

GIT_SSH_COMMAND="ssh -i ~/.ssh/your_ssh_key"

如果你使用dotenv,环境变量会自动导出,你可以为每个项目/目录指定你想要的键。口令只被请求一次,因为它被添加到密钥链中。

这个解决方案与Git一起完美地工作,并且设计为在Mac上工作(由于UseKeychain)。