【问题标题】:Bare Metal Cloud - How to set authorized ssh keys for compute instances?Bare Metal Cloud - 如何为计算实例设置授权的 ssh 密钥?
【发布时间】:2016-11-23 22:32:20
【问题描述】:

我已使用以下代码成功配置了裸金属云计算实例:

public static Instance createInstance(
        ComputeClient computeClient,
        String compartmentId,
        AvailabilityDomain availabilityDomain,
        String instanceName,
        Image image,
        Shape shape,
        Subnet subnet
    ) {

    LaunchInstanceResponse response = computeClient.launchInstance(
        LaunchInstanceRequest.builder()
            .launchInstanceDetails(
                LaunchInstanceDetails.builder()
                    .availabilityDomain(availabilityDomain.getName())
                    .compartmentId(compartmentId)
                    .displayName(instanceName)
                    .imageId(image.getId())
                    .shape(shape.getShape())
                    .subnetId(subnet.getId())
                    .build())
            .build());  

    return response.getInstance();
}

但是,我无法通过 SSH 连接到通过上述代码创建的任何实例,因为 launchInstance 上没有参数可以传入我的 SSH 密钥对的公钥。

如何告诉实例允许使用什么 SSH 公钥?我知道这一定是可能的,因为控制台 UI 允许我提供 SSH 公钥作为实例创建的一部分。

【问题讨论】:

    标签: oracle-cloud-infrastructure oci-java-sdk oci-go-sdk


    【解决方案1】:

    根据launch instance API documentation,您需要通过metadata参数的ssh_authorized_keys字段传递您的SSH公钥:

    提供 Cloud-Init 元数据

    您可以使用以下元数据键名向 Cloud-Init 提供信息:

    "ssh_authorized_keys" - 提供一个或多个公共 SSH 密钥 包含在默认用户的 ~/.ssh/authorized_keys 文件中 实例。使用换行符分隔多个键。这 SSH 密钥必须采用 authorized_keys 文件所需的格式

    Java SDK 中的代码如下所示:

    public static Instance createInstance(
            ComputeClient computeClient,
            String compartmentId,
            AvailabilityDomain availabilityDomain,
            String instanceName,
            Image image,
            Shape shape,
            Subnet subnet
        ) {
    
        String sshPublicKey = "ssh-rsa AAAAB3NzaC1y...key shortened for example...fdK/ABqxgH7sy3AWgBjfj some description";
    
        Map<String, String> metadata = new HashMap<>();
        metadata.put("ssh_authorized_keys", sshPublicKey);
    
        LaunchInstanceResponse response = computeClient.launchInstance(
            LaunchInstanceRequest.builder()
                .launchInstanceDetails(
                    LaunchInstanceDetails.builder()
                        .availabilityDomain(availabilityDomain.getName())
                        .compartmentId(compartmentId)
                        .displayName(instanceName)
                        .imageId(image.getId())
                        .metadata(metadata)
                        .shape(shape.getShape())
                        .subnetId(subnet.getId())
                        .build())
                .build());  
    
        return response.getInstance();
    }
    

    然后,实例将允许您使用该公钥的 SSH 密钥对通过 SSH 连接到它。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2017-11-06
      • 2016-02-24
      • 1970-01-01
      • 2015-02-16
      • 2018-11-18
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多