【问题标题】:How to use existing SSH key stored for Virtual Machine creation in azure using ARM template如何使用 ARM 模板在 Azure 中使用存储的现有 SSH 密钥创建虚拟机
【发布时间】:2021-08-06 13:04:13
【问题描述】:

我在 Azure 云上创建了一个 SSH 密钥。现在我想使用现有密钥创建一个虚拟机来使用该公钥源。现在我想用 ARM 模板来做这个操作。我正在尝试这样的事情

“osProfile”:{ "计算机名": "我的计算机名", "adminUsername": "adminUser", “linux配置”:{ “disablePasswordAuthentication”:真, “ssh”:{ “公钥”:[ { "path": "[concat('/home/', 'adminUser', '/.ssh/authorized_keys')]", "keyData": "我如何获取现有 ssh 密钥的公钥" } ] } } }

但我不确定如何获取创建的 SSH 密钥的公钥。

【问题讨论】:

    标签: azure azure-resource-manager


    【解决方案1】:

    如果您在 Azure 云外壳上有 generated keys with ssh-keygen。 SSH 密钥对默认保存在/home/azureuser/.ssh 目录中。

    通过运行cat 来查看您的公钥,如下所示:

    cat ~/.ssh/id_rsa.pub
    

    然后将公钥文件的内容复制并粘贴到您的资源管理器模板中,确保您没有复制任何额外的空格或引入额外的换行符。

    阅读详细步骤:Create and manage SSH keys for authentication to a Linux VM in AzureHow to create a Linux virtual machine with Azure Resource Manager templates

    【讨论】:

    • 感谢南希的回复,我读到了 cat 命令,但问题是我应该从哪个机器/系统运行 cat ~/.ssh/id_rsa.pub command ?因为现有的 ssh 密钥在 azure 云上可用,而不是在我的本地系统中。
    • 你是指Azure云壳还是其他?如果它存在于云外壳上,您可以打开您的云外壳,然后运行命令作为我的屏幕截图。
    • 你的问题解决了吗?
    • 尚未修复,SSH 密钥存在于我订阅的 Azure 云中。我想要在运行时(在执行我的应用程序时)的公钥内容。有可能使用云壳来获得吗?请描述一下。
    • 您的应用程序在哪里?您是否还想部署将在 Azure 云外壳上获取 ssh 密钥的 arm 模板?
    【解决方案2】:

    首先为 Azure 上现有的 ssh 密钥存储构建资源 ID:

    "variables": {
        "sshKeyResourceId": "[resourceId(variables('sshKeyRg'), 'Microsoft.Compute/sshPublicKeys', variables('sshKeyName'))]",
    }
    

    接下来从该资源中提取 ssh “keyData”:

    "osProfile": {
        "linuxConfiguration": {
            "disablePasswordAuthentication": true,
                "ssh": {
                    "publicKeys": [
                        {
                            "keyData": "[reference(variables('sshKeyResourceId'), '2019-12-01').publicKey]",
                            "path": "[concat('/home/', variables('adminUser'), '/.ssh/authorized_keys')]"
                        }
                    ]
                 }
             }
         }
    }
    
    

    查看更详细的示例here

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-07-23
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-01-17
      • 2018-09-19
      • 2018-09-06
      • 2018-02-09
      相关资源
      最近更新 更多