簡體   English   中英

如何為同一主機使用多個 SSH 密鑰?

[英]How can I use multiple SSH keys for the same host?

我幾乎希望能夠在同一服務器上為不同用戶使用多個 SSH 密鑰。 我有一台服務器,用於虛擬主機和 SSH 隧道。 我已經設置了一個沒有專門用於 SSH 隧道的登錄 shell 的帳戶。 我使用 root 用戶來管理系統的其余部分。

我有兩個 SSH 密鑰,一個有 root 用戶的密碼,另一個沒有 SSH 隧道的密碼。 當我以隧道用戶身份連接時,它使用隧道密鑰,而當我以根用戶身份連接時,它使用根密鑰,我該如何做到這一點?

如果您為 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.

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM