Creating SSH Keys for Use with Oracle Cloud Services


Options



Before You Begin

Purpose

This tutorial covers how to create a secure shell (SSH) key pair in two ways:

  • By using PuTTY on Windows.

  • By using a command line with an SSH utility, such as those already included in many UNIX and UNIX-like systems such as Linux, Solaris, etc. Alternatively, you can install and use OpenSSH on UNIX or Cygwin on Windows.

Time to Complete

Approximately 30 minutes.

Background

Oracle Cloud services such as Oracle Java Cloud Service and Oracle Database Cloud - Database as a Service are built on top of infrastructure and functionality that are provided by Oracle Compute Cloud Service. When you create a service instance of one of these Oracle Cloud services, all the Oracle Compute virtual machines (VMs) required to support the service instance are provisioned and configured for you.

You can access the service instances and resources provided by the VMs by logging into the machine through a secure shell (SSH). To do so, you need an SSH public/private key pair for the VM. You will need the public key when creating a service instance, and you will need the paired private key when you access it using an SSH utility. This tutorial shows how to create the key pair.

Note: You can associate a single SSH public key with multiple service instances. However, you cannot associate an SSH public key with a service instance after you have created the instance.

Scenario

You are ready to create an Oracle Cloud service instance, and you need to create an SSH public key to use when creating it. Later, you will need the paired private key to access the VM remotely.

What Do You Need

  • PuTTY

    PuTTY is a free, open-source implementation of several network protocols, including SSH. It is available for Windows and for UNIX platforms. PuTTY includes several utilities including a terminal emulator, an SSH key generator, and a network transfer application. For this tutorial we will use the PuTTY Key Generator (puttygen.exe) for Windows.

    PuTTY is available from many sites, but you can reach the main download site http://www.putty.org.

  • An implementation of the UNIX ssh-keygen utility and a command line shell for running it. Many implementations of UNIX and UNIX-like operating systems already include ssh-keygen, so you don't have to install it yourself. Check the documentation for your operating system to see if you have ssh-keygen already (or simply try typing it at the command line).

    If you don't already have ssh-keygen installed, you can obtain it from any of a number of sources and install it yourself. Popular options are OpenSSH for UNIX and Cygwin for Windows. OpenSSH is available from http://www.openssh.com/portable.html. Cygwin is available from https://cygwin.com/install.html.

Before starting this tutorial, you should have:

  • Installed PuTTY on your computer with a Windows operating system.

  • Installed the ssh-keygen utility by either:

    • Making sure your UNIX or UNIX-like operating system already includes ssh-keygen; or

    • Installing an SSH utility with a command line interface, such as OpenSSH for UNIX or Cygwin for Windows.

  • Some knowledge of UNIX, for working at the command line.

Generating an SSH Key Pair Using PuTTY Key Generator

To generate an SSH key pair using the PuTTY Key Generator,

  1. Find puttygen.exe in the PuTTY folder on your computer, for example, C:\Program Files (x86)\PuTTY. Double-click puttygen.exe to open it.

    PuTTY Key Generator
    Description of this image
  2. Accept the default key type, SSH-2 RSA.

    SSH-2 is the most recent version of the SSH protocol (and is incompatible with SSH-1). RSA and DSA are algorithms for computing digital signatures.

    PuTTY Key Generator parameters
    Description of this image
  3. Set the Number of bits in a generated key to 2048 bits, if it is not already set with that value.

    This sets the size of your key and thus the security level. A minimum of 2048 bits is recommended for SSH-2 RSA.

    PuTTY Key Generator parameters
    Description of this image
  4. Click Generate.

    Generate button
    Description of this image
  5. Move your mouse around the blank area to generate randomness to the key.

    Note: the dotted red line in the image below is for illustration purposes only. It does not appear in the generator pane as you move the mouse.

    Generating key - status
    Description of this image
  6. The generated key appears under Public key for pasting into OpenSSH authorized_keys file.

    Generated public key
    Description of this image
  7. The key comment is the name of the key that you will use to identify it. You can keep the generated key comment or create your own.

    Key comment field
    Description of this image
  8. If you want to password-protect your key, enter a Key passphrase and enter it again for Confirm passphrase. When you reload a saved private key, you will be asked for the passphrase, if one is set.

    Key passphrase fields
    Description of this image

    While a passphrase is not required, you should specify one as a security measure to protect the private key from unauthorized use.

    There is no way to recover a passphrase if you forget it.

  9. Save the private key of the key pair. Depending how you work with the private key in the future, you may need one saved in the PuTTY PPK format and one saved in OpenSSH format. Let's do both.

    1. To save the key in the PuTTY PPK format, click Save private key to save the private key of the key pair.

      Save privatge key button
      Description of this image

      You can name it anything you want, although you may want to use the same name as you used for the key comment. The private key is saved in PuTTY's Private Key (PPK) format, which is a proprietary format that works only with the PuTTY toolset.

      Save private key - file name and type
      Description of this image

      You can use this key whenever you use Putty to perform SSH actions.

    2. To save the key in OpenSSH format, open the Conversions menu and select Export SSH key. This will be the same key as above, just saved in a different format.

      Save private key in OpenSSH format
      Description of this image

      You can name it anything you want, but to keep track of your keys, you should give it the same name as the key you saved in PPK format in the previous step. You can also use any extension (or no extension), but let's use .ssh, to make it clear what format it is.

      Save private key in OpenSSH format
      Description of this image

      You can use this key whenever you use OpenSSH to perform SSH actions using ssh utitlities that support OpenSSH, for example when using Linux in a command shell.

  10. Now you need to create the public key to be paired with the private key(s) you just created. However, clicking the Save public key button will create a public key that won't work with Oracle Cloud services in certain cases. So, for the purposes of this tutorial, there is no reason to save a public key using the Save public key button.

    Do not click Save public key button
    Description of this image

    Instead, proceed as follows.

  11. In the PuTTY Key Generator, select all of the characters under Public key for pasting into OpenSSH authorized_keys file.

    Make sure you select all the characters, not just the ones you can see in the narrow window. If a scroll bar is next to the characters, you aren't seeing all the characters.

    Public key for pasting
    Description of this image
  12. Right click somewhere in the selected text and select Copy from the menu.

    Public key for pasting - copy
    Description of this image
  13. Open a text editor and paste the characters, just as you copied them. Start at the first character in the text editor, and do not insert any line breaks.

    Public key pasted into text editor
    Description of this image
  14. Save the key as a text file, using the same root name as you used for the private key. Add a .pub extension. You can give it any extension you want, but .pub is a useful convention to indicate that this is a public key.

    Save public key - file name and type
    Description of this image
  15. Write down the names of your public and private keys, and note where they are saved. You will need the public key when creating service instances in, for example, Oracle Java Cloud Service and Oracle Database Cloud - Database as a Service. You will need the private key when trying to access a service instance's virtual machine via SSH.

Creating an SSH Key Pair on the Command Line

To create an SSH key pair on the command line using ssh-keygen:

  1. Open a shell for entering the commands.

  2. At the prompt, enter the following:

       ssh-keygen -t rsa -N "" -b "2048" -C "key comment" -f path/root_name

    where the arguments are as follows:

    -t rsa Use the RSA algorithm.
    -N "passphrase"

    Passphrase to protect the use of the key (like a password). If you don't want to set a passphrase, don't enter anything between the quotes.

    Note: While a passphrase is not required, you should specify one as a security measure to protect the private key from unauthorized use.

    -b "2048"

    Generate a 2048 bit key. You don't have to set this if 2048 is acceptable, as 2048 is the default.

    Note: A minimum of 2048 bits is recommended for SSH-2 RSA.

    -C "key comment" A name to identify the key.
    -f path/root_name

    The location where the key pair will be saved and the root name for the files. For example, if you give the root name as id_rsa, the name of the private key will be id_rsa and the public key will be id_rsa.pub.

    For example,

    Command prompt - creating key pair, specifying parameters
    Description of this image
  3. Alternatively, you can simply enter ssh-keygen and then enter responses when prompted for a name and a passphrase. The keys will be created with default values: RSA keys of 2048 bits.

    Command prompt, creating key pair without parameters
    Description of this image
  4. To see that the files were created, type ls path/root_name*, for example,

    Command prompt, using li command to see files
    Description of this image
  5.  

Want to Learn More?