使用 Azure CLI 產生並儲存 SSH 金鑰

適用於: ✔️ Linux VM ✔️ Windows VM ✔️ 彈性擴展集 ✔️ 統一擴展集

您可以在建立 VM 之前建立 SSH 金鑰,並將其儲存在 Azure 中。 每個新建立的 SSH 金鑰也會儲存在本機。

如果具有現有的 SSH 金鑰,您可以上傳這些金鑰並將其儲存在 Azure 中以供重複使用。

如需詳細資訊,請參閱詳細步驟:在 Azure 中建立和管理對 Linux VM 進行驗證所需的 SSH 金鑰

如需如何建立 SSH 金鑰並將其與 Linux VM 搭配使用的詳細資訊,請參閱使用 SSH 金鑰連線至 Linux VM

產生新的金鑰

  1. 登入後,請使用 az sshkey create 命令來建立新的 SSH 金鑰:

    az sshkey create --name "mySSHKey" --resource-group "myResourceGroup"
    
  2. 產生的輸出會列出新的金鑰檔案路徑:

    Private key is saved to "/home/user/.ssh/7777777777_9999999".
    Public key is saved to "/home/user/.ssh/7777777777_9999999.pub".
    
  3. 請變更私密金鑰的權限,以保護隱私權:

    chmod 600 /home/user/.ssh/7777777777_9999999
    

連線至 VM

在您的本機電腦上,開啟 Bash 提示:

ssh -identity_file <path to the private key file> username@<ipaddress of the VM>

例如,輸入:ssh -i /home/user/.ssh/mySSHKey azureuser@123.45.67.890

上傳 SSH 金鑰

您可以上傳公用 SSH 金鑰以儲存在 Azure 中。

請使用 az sshkey create 命令,藉由指定其檔案來上傳 SSH 公開金鑰:

az sshkey create --name "mySSHKey" --public-key "@/home/user/.ssh/7777777777_9999999.pub" --resource-group "myResourceGroup"

列出金鑰

請使用 az sshkey list 命令來列出所有公用 SSH 金鑰,並選擇性地指定資源群組:

az sshkey list --resource-group "myResourceGroup"

取得公開金鑰

請使用 az sshkey show 命令來顯示公用 SSH 金鑰的值:

az sshkey show --name "mySSHKey" --resource-group "myResourceGroup"

下一步

若要深入了解如何搭配 Azure VM 使用 SSH 金鑰,請參閱使用 SSH 金鑰連線至 Linux VM