Before You Begin
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.
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.
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 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-keygenutility 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-keygenalready (or simply try typing it at the command line).
If you don't already have
ssh-keygeninstalled, 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.
ssh-keygenutility by either:
Making sure your UNIX or UNIX-like operating system already includes
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,
puttygen.exein the PuTTY folder on your computer, for example,
C:\Program Files (x86)\PuTTY. Double-click
puttygen.exeto open it.
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.
Set the Number of bits in a generated key to
2048bits, 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.
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.
The generated key appears under Public key for pasting into OpenSSH authorized_keys file.
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.
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.
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.
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.
To save the key in the PuTTY PPK format, click Save private key to save the private key of the key pair.
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.
You can use this key whenever you use Putty to perform SSH actions.
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.
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.
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.
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.
Instead, proceed as follows.
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.
Right click somewhere in the selected text and select Copy from the menu.
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.
Save the key as a text file, using the same root name as you used for the private key. Add a
.pubextension. You can give it any extension you want, but
.pubis a useful convention to indicate that this is a public key.
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
Open a shell for entering the commands.
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:
Use the RSA algorithm.
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.
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.
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_rsaand the public key will be
Alternatively, you can simply enter
ssh-keygenand then enter responses when prompted for a name and a passphrase. The keys will be created with default values: RSA keys of 2048 bits.
To see that the files were created, type
ls path/root_name*, for example,