Creating a key pair

This section explains how to create a key pair to be used when logging in to a virtual server using SSH.



When registering the key pair, create the key file (*.pem), which is used for SSH authentication. You can use this key pair to log in to a virtual server.

Important: The key file should be managed with care.
  1. Set the environment variable below as follows:
    $ KEYPAIR=<keyPairName> (any)
    $ AZ=<availabilityZoneName>
  2. Execute the following API:
    $ curl -X POST -Ss $COMPUTE/v2/$PROJECT_ID/os-keypairs \
    -H "X-Auth-Token:$OS_AUTH_TOKEN" -H "Content-Type:application/json" \
    -d '{"keypair": {"name": "'$KEYPAIR'", "availability_zone": "'$AZ'"}}' |\
    jq -r .keypair.private_key > $KEYPAIR.pem
  3. Enable the key file:
    $ chmod 600 $KEYPAIR.pem
  4. Display the content of the key file:
    cat $KEYPAIR.pem

    The following response is output:

    -----BEGIN RSA PRIVATE KEY-----
    
    ~~omitted~~
    
    -----END RSA PRIVATE KEY-----
  5. Execute the following API to check the key pair that you created:
    $ curl -X GET -Ss $COMPUTE/v2/$PROJECT_ID/os-keypairs \
    -H "X-Auth-Token:$OS_AUTH_TOKEN" -H "Content-Type:application/json" \
    | jq .

    If a list including the key pair name that you specified is output, as follows, that means the key pair was created successfully.

    {
      "keypairs": [
        
        ...
        
        {
          "keypair": {
            "public_key": "ssh-rsa 
            
            ~~content omitted~~
            
            == Generated by Nova\n",
            "name": "<keyPairName>",
            "fingerprint": "64:96:18:f9:f2:96:e9:7d:f2:b3:dd:ee:bc:45:eb:a0"
          }
        },
        
        ...
        
      ]
    }
Tip: Apart from generating key pairs, it is also possible to import key pairs created externally.