我想使用多个私钥连接到不同的服务器或同一服务器的不同部分(我的用途是服务器的系统管理、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主机


其他回答

您可以在~/中创建一个名为config的配置文件。ssh文件夹。它可以包含:

Host aws
    HostName *yourip*
    User *youruser*
    IdentityFile *idFile*

这将允许您像这样连接到机器

 ssh aws

从我的.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我的其他

等等。

前面的回答已经正确地解释了如何创建一个配置文件来管理多个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克隆时,你应该把上面的存储库URL修改为:

git@company:abc1234/demo.git

注意github.com现在是如何被我们在配置文件中定义的别名“company”所取代的。

同样,您必须根据配置文件中提供的别名修改个人帐户中存储库的克隆URL。

我喜欢在~/.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)。

正如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

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