Creating and Importing an SSH Key Pair

Function

This API is used to create an SSH key pair or import a public key to generate a key pair.

After a private SSH key is created, download the private key to a local directory. Then, you can use this private key to log in to the ECS. To ensure ECS security, the private key can be downloaded only once. Keep it secure.

Only the user that created the key pair can view it. If the key pair is created by an IAM user, the IAM account of the user and the other users of the same account cannot view the key pair.

URI

POST /v2.1/{project_id}/os-keypairs

POST /v2/{project_id}/os-keypairs

Table 1 describes the parameters in the URI.

Table 1 Parameter description

Parameter

Mandatory

Description

project_id

Yes

Specifies the project ID.

Request

Table 2 describes the request parameters.

Note

When creating an SSH key, you only need to configure name. When importing an SSH key, you must configure public_key.

Table 2 Request parameters

Parameter

Mandatory

Type

Description

keypair

Yes

Object

Specifies the created or imported SSH key pair. For details, see Table 3.

Table 3 keypair field description

Parameter

Mandatory

Type

Description

public_key

No

String

Specifies the imported public key.

It is recommended that the length of the imported public key be less than or equal to 1024 bytes.

Note

If the length of the public key to be imported exceeds 1024 bytes, importing the public key will fail.

type

No

String

Specifies the key type. The value is ssh or x509.

This parameter is supported in microversion 2.2 and later.

name

Yes

String

Specifies the key pair name.

The new key pair name cannot be the same as an existing one.

user_id

No

String

Specifies the user ID of the key.

This parameter is supported in microversion 2.10 and later.

Response

Table 4 describes the response parameters.

Table 4 Response parameters

Parameter

Type

Description

keypair

Object

Specifies the SSH key pair. For details, see Table 5.

Table 5 keypair field description

Parameter

Type

Description

fingerprint

String

Specifies fingerprint information about the key pair.

name

String

Specifies the key pair name.

public_key

String

Specifies information about the public key.

private_key

String

Specifies information about the private key.

  • The information about the private key is contained in the response for creating an SSH key.

  • The information about the private key is not contained in the response for importing an SSH key.

user_id

String

Specifies the ID of the user to which the key pair belongs.

type

String

Specifies the key type. The value is ssh or x509.

This parameter is supported in microversion 2.2 and later.

Example Request

Importing an SSH Key

POST https://{endpoint}/v2/{project_id}/os-keypairs
POST https://{endpoint}/v2.1/{project_id}/os-keypairs
{
    "keypair": {
        "public_key": "ssh-rsaAAAAB3NzaC1yc2EAAAADAQABAAABAQDWNgTxQYeBzK9LYy4IakX7IsIl5j5zqR6BU2GJaEg3RK6dlS7rKFQhvy/V/1emK+GT/7P8up9VsMZ9Dx6PBOLow5p+2/wGsMlwDJpWiQ8zNnEMg+u/Ar/ZhYHAMyKEAOOJxIcnPoUgxfNdj/eiXV98AabsBdUA7QD30Og8F4Bmn2lii/WD9KbQQVjb7kbB3gNIJpGTUcoX73arorqkq/ppaLRmmwMJ7bTIGl8/0MWU2Dy+eTByOaDMb2htbB+j8ZXyEu7Oooy0NaSd+PNHv3PZ9OIVO7gd1lyoTRvCMK/F346+zmZtk5EASSOx5RifnSwk3NtugVjXs9GMJfFLBRibGenerated-by-Nova\\n\n",
        "type": "ssh",
        "name": "demo1",
        "user_id": "fake"
    }
}

Creating an SSH Key

POST https://{endpoint}/v2/{project_id}/os-keypairs
POST https://{endpoint}/v2.1/{project_id}/os-keypairs
{
    "keypair": {
        "name": "demo"
    }
}

Example response

Importing an SSH Key

{
    "keypair": {
        "public_key": "ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQDWNgTxQYeBzK9LYy4IakX7IsIl5j5zqR6BU2GJaEg3RK6dlS7rKFQhvy/V/1emK+GT/7P8up9VsMZ9Dx6PBOLow5p+2/wGsMlwDJpWiQ8zNnEMg+u/Ar/ZhYHAMyKEAOOJxIcnPoUgxfNdj/eiXV98AabsBdUA7QD30Og8F4Bmn2lii/WD9KbQQVjb7kbB3gNIJpGTUcoX73arorqkq/ppaLRmmwMJ7bTIGl8/0MWU2Dy+eTByOaDMb2htbB+j8ZXyEu7Oooy0NaSd+PNHv3PZ9OIVO7gd1lyoTRvCMK/F346+zmZtk5EASSOx5RifnSwk3NtugVjXs9GMJfFLBRib Generated-by-Nova\\n\n",
        "user_id": "6fc0d2cbbfab40b199874b97097e913d",
        "name": "demo1",
        "fingerprint": "fc:47:b5:c3:7d:25:32:d5:d2:0c:19:f9:62:ac:8c:5a"
    }
}

Creating an SSH Key

{
    "keypair": {
        "public_key": "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c543H3Di\n4673helqSdMnI0/TgyfLQcNuOsfQcD02ABWlGBe0nCTqP8pTRo86nzK1+AoCUp72\nIsTeviECgYEA8yHKeo/eZw25eDb3YTJovbgzA61n6AYQlDQv7rBGQDwKKQHdEqhR\nP0PbScaoT7wSeLtYV0vxxA6qjEEuHhZIk/t2wEILu+AH4AK88SUbUn6ZoYu+XmTA\nx26e2QRo8Ngi/KtIfeOGXx1PM/H2/OjEN3XjkfwJsj5bB+HjpF/wsnUCgYEA4Yxg\nWJYNrvSkmvXmDgxHwdxfUpVAcp40bvomNgYpKn9R2TyjMCSDIw8vVC6cGCFB9/Pc\nG0pr8RN2SvbTaPo/96DkKdHz7NAWkzUSChD4Oy7ZNXw6GK3x1tGwMWeTs1hQDHhO\nrjS+E3bV2jC4EIvLLBxCNCbhtmQwlGUj7ZhgHM8CgYEA14UGpWpOrW8/D086LpCu\nxC46GnJmfwiRPa6dJqpfO6V9JCigvV8y1i/ifR16KWP/w8HeZ1PMtgyCJd3JcaYz\nI+pus7JYEGxgzrPepKxN8eyDZu4nDCmnsaFfceQ02fnd2bhDhERh4oJqqRM966ax\n+K+p0MhoF/aqXuxgDF93T9kCgYEAw7TsfLFnGiJJGfS4NARP11UCmUPMcif4UztX\nIJVj7u4e9SJ6bvGfoDIy3Ra8duuUtDOzDzMaSkqa4B0f//z0uEew8uCsiRVeIUlx\nZ66l1aSm8JPkTTnRmJbGDXhUXtAIVWmmy94T+AurL/IKJMFH//RdNadvPrXcuUax\nUB5hd10CgYEA3JBuX4BriSk6Bii0kYniqFM/1tEgVelAP6DT6uePvzTFdSJ0dMQo\nzwgWNmm43CyoKW/rw8yIbtIQZKBfHudSNx72nSmnBKaf3QPB40xsCip90ZUTfZdn\nLJzX1t4clg1wNsN4mJDwiYM9k3rB/8EY1fh9gUYI84X6xFAHllkv0To=\n-----END RSA PRIVATE KEY-----\n",
        "user_id": "6fc0d2cbbfab40b199874b97097e913d",
        "type": "ssh",
        "name": "demo",
        "fingerprint": "fc:47:b5:c3:7d:25:32:d5:d2:0c:19:f9:62:ac:8c:5a"
   }
}

Returned Values

See Returned Values for General Requests.