我想使用多个私钥连接到不同的服务器或同一服务器的不同部分(我的用途是服务器的系统管理、Git管理和同一服务器中的正常Git使用)。我尝试简单地将密钥堆叠在id_rsa文件中,但没有用。
显然,要做到这一点,一个简单的方法是使用命令
ssh -i <key location> login@server.example.com
这很麻烦。
有什么建议可以让你做这件事更容易一些吗?
我想使用多个私钥连接到不同的服务器或同一服务器的不同部分(我的用途是服务器的系统管理、Git管理和同一服务器中的正常Git使用)。我尝试简单地将密钥堆叠在id_rsa文件中,但没有用。
显然,要做到这一点,一个简单的方法是使用命令
ssh -i <key location> login@server.example.com
这很麻烦。
有什么建议可以让你做这件事更容易一些吗?
当前回答
对于那些使用aws的人,我强烈建议使用EC2实例连接。
Amazon EC2 Instance Connect提供了一种使用secure Shell (SSH)连接到实例的简单而安全的方法。
使用EC2实例连接,您可以使用AWS身份和访问管理(IAM)策略和原则来控制对实例的SSH访问,从而消除了共享和管理SSH密钥的需要。
在安装相关的包(pip install ec2instanceconnectcli或直接克隆repo)后,您可以通过更改实例id轻松连接到多个EC2实例:
幕后发生了什么?
当您使用EC2实例连接连接到实例时,实例连接API将一次性使用的SSH公钥推送到实例元数据中,该公钥在该实例元数据中保持60秒。附加到您的IAM用户的IAM策略授权您的IAM用户将公钥推送到实例元数据。
SSH守护进程使用在安装实例连接时配置的AuthorizedKeysCommand和AuthorizedKeysCommandUser从实例元数据中查找公钥进行身份验证,并将您连接到实例。
(*)亚马逊Linux 2 2.0.20190618或更高版本和Ubuntu 20.04或更高版本预先配置了EC2实例连接。 对于其他受支持的Linux发行版,必须为支持使用Instance Connect的每个实例设置Instance Connect。这是每个实例的一次性需求。
链接:
设置EC2实例连接 使用EC2实例连接进行连接 使用Amazon EC2实例连接保护您的bastion主机
其他回答
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
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克隆命令),你可以把它从配置中去掉。
点击这里查看更多信息
这是我从sajib-khan的回答中得到的灵感。没有设置默认配置;这是我在GitLab上的个人账号,另一个是我的公司账号。以下是我所做的:
生成SSH密钥
ssh-keygen -t rsa -f ~/.ssh/company -C "name.surname@company.com"
编辑SSH配置
nano ~/.ssh/config
Host company.gitlab.com
HostName gitlab.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/company
删除缓存的SSH密钥
ssh-add -D
测试它!
ssh -T git@company.gitlab.com
欢迎来到GitLab, @hugo.sohm!
ssh -T git@gitlab.com
欢迎你的芳名…
使用它!
公司账户
git clone git@company.gitlab.com:group/project.git
个人/默认账户
git clone git@gitlab.com:username/project.git
这是我使用的源代码。
对我来说,在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。
正如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
然后,您可以简单地用后缀域签出,在项目中,您可以在本地配置作者名称等。