如何为同一主机使用多个SSH密钥?

时间:2013-02-15 04:02:35

标签: ssh key

我想要做的就是能够在同一台服务器上为不同的用户使用多个SSH密钥。我有一个服务器,我用于webhosting和SSH隧道。我已经设置了一个没有专门用于SSH隧道的登录shell的帐户。我使用root用户来管理系统的其余部分。

我有2个SSH密钥,一个有root用户密码,另一个没有SSH隧道密码。当我作为隧道用户连接时,如何使用隧道密钥,当我以root用户身份连接时,它使用根密钥?

1 个答案:

答案 0 :(得分:3)

如果您为root用户设置了一个密钥,另一个为您的隧道用户设置了密钥(通过服务器/远程计算机上的authorized_keys),则应自动选择正确的密钥。

这是基于您在ssh-agent中加载密钥的假设,并且它们可供ssh实用程序使用。

否则,您可以使用ssh -i <identity file>手动指定密钥。

除此之外,您可以在ssh_config(〜/ .ssh / config或/ etc / ssh / ssh_config)中设置别名:

Host server-root
User root
IdentityFile <path to your key>
Hostname <real hostname>

Host server-tunnel
User tunnel-user
IdentityFile <path to your key>
Hostname <real hostname>

然后您使用ssh server-rootssh server-tunnel

但我认为使用ssh-agent可能是最简单的设置。

如果您想在没有ssh-agent的情况下自动选择正确的密钥,可以通过-i指定两个密钥。

引用openssh手册页:

 -i identity_file
     Selects a file from which the identity (private key) for public
     key authentication is read.  The default is ~/.ssh/identity for
     protocol version 1, and ~/.ssh/id_dsa, ~/.ssh/id_ecdsa and
     ~/.ssh/id_rsa for protocol version 2.  Identity files may also be
     specified on a per-host basis in the configuration file.  It is
     possible to have multiple -i options (and multiple identities
     specified in configuration files).  ssh will also try to load
     certificate information from the filename obtained by appending
     -cert.pub to identity filenames.
相关问题