我非常希望能够在同一台服务器上为不同的用户使用多个 SSH key 。我有一台服务器,可用于虚拟主机和 SSH 隧道。我已经设置了一个没有专门用于 SSH 隧道的登录 shell 的帐户。我使用 root 用户来管理系统的其余部分。
我有两个 SSH key ,一个有 root 用户的密码,另一个没有 SSH 隧道的密码。我如何做到这一点,当我以隧道用户身份连接时,它使用隧道 key ,而当我以根用户身份连接时,它使用根 key ?
最佳答案
如果您为 root 用户设置了一个 key ,另一个为您的隧道用户设置(通过服务器/远程计算机上的文件 authorized_keys),将自动选择正确的 key 。
这是基于您在 ssh-agent
中加载 key 的假设并且它们可供 ssh
使用效用。
否则,您可以使用 ssh -i <identity file>
手动指定 key .
除此之外,您还可以在 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-root
或 ssh server-tunnel
.
但我会说使用 ssh-agent 可能是最简单的设置。
如果你想在没有 ssh-agent 的情况下自动选择正确的 key ,你可以通过 -i
指定两个 key 。 .
引用自 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.
关于ssh - 如何为同一主机使用多个 SSH key ?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14888056/