← HostLaunch Docs

How to Generate an SSH Key Pair for HostLaunch

Overview

For security purposes and to ensure you will have root access to your customers' servers, HostLaunch requires you to provide a public SSH key before customers are able to sign up for your services.

Public key authentication is a way of logging into an SSH/SFTP account using a cryptographic key rather than a password.

If you use very strong SSH/SFTP passwords, your accounts are already safe from brute force attacks. However, using public key authentication provides many benefits when working with multiple developers. For example, with SSH keys you can

  • eliminate the need to enter your long, complex password every time you log in;
  • revoke a single user's access without revoking access by other developers;
  • allow multiple developers to log in as the same system user easily without needing a common password; and,
  • make it easier for a single developer to log in to many accounts without needing to manage many different passwords or share the same password across all accounts.

How Public Key Authentication Works

Keys come in pairs of a public key and a private key. Each key pair is unique, and the two keys work together.

These two keys have a very special and beautiful mathematical property: if you have the private key, you can prove you have it without showing what it is. It's like proving you know a password without having to show someone the password.

Public key authentication works like this:

  1. Generate a key pair.
  2. Give someone (or a server) the public key.
  3. Later, anytime you want to authenticate, the person (or the server) asks you to prove you have the private key that corresponds to the public key.
  4. You prove you have the private key.

You don't have to do the math or implement the key exchange yourself. The SSH server and client programs take care of this for you.

Generate an SSH Key Pair

To follow best practices, you should generate your key pair on a different machine, not on your server. However, if your server is a brand new instance and is the only Linux system to which you have access, you can do it on your server. If you're using Windows, you can also generate the keys on your server.

All Mac and Linux systems include a command called ssh-keygen that will generate a new key pair. We'll show you how below.

IMPORTANT: If you do generate your key pair on your server, please be sure to delete your private key from the server immediately after copying it to your local system. Leaving the key on the server is much like leaving the physical key to your house in a place you can easily see from the door.

To generate an SSH key pair, run the command ssh-keygen.

ssh-keygen

It will look like this when you run it:

laptop1:~ yourname$ ssh-keygen
Generating public/private rsa key pair.

You'll be prompted to choose the location to store the keys. The default location is good unless you already have an SSH key. If you have previously generated an SSH key in the id_rsa file, you'll need to select an alternate location. If no key exists, press Enter to choose the default location.

Enter file in which to save the key (/Users/yourname/.ssh/id_rsa):

Next, you'll be asked to choose a password. Using a password means a password will be required to use the private key. It's a very good idea to use a password on your private key because it is another layer of security, but it is not required.

Enter passphrase (empty for no passphrase):
Enter same passphrase again:

After you choose a password, your public and private keys will be generated. There will be two different files. The one named id_rsa is your private key, and the one named id_rsa.pub is your public key.

Your identification has been saved in /Users/yourname/.ssh/id_rsa.
Your public key has been saved in /Users/yourname/.ssh/id_rsa.pub.

You'll also be shown a fingerprint and "visual fingerprint" of your key. You do not need to save these.

The key fingerprint is:
d7:21:c7:d6:b8:3a:29:29:11:ae:6f:79:bc:67:63:53 yourname@laptop1
The key's randomart image is:
+--[ RSA 2048]----+
|                 |
|           . o   |
|      .   . * .  |
|     . .   = o   |
|      o S . o    |
|     . . o oE    |
|    . .oo +.     |
|     .o.o.*.     |
|     ....= o     |
+-----------------+

Configure an SSH/SFTP User for Your Key

Method 1: Using ssh-copy-id

Now that you have an SSH key pair, you're ready to configure your app's system user so you can SSH or SFTP in using your private key instead of a password.

To copy your public key to your server, run the following command. Be sure to replace "x.x.x.x" with your server's IP address and SYSUSER with the name of the the system user your app belongs to.

ssh-copy-id SYSUSER@x.x.x.x

That's it!

Method 2: Manual Configuration

If you don't have the ssh-copy-id command (for example, if you are using Windows), you can instead SSH in to your server and manually create the .ssh/authorized_keys file so it contains your public key.

First, log into your server as the system user you want to use with the SSH key pair and run the following commands to create the file with the correct permissions.

(umask 077 && test -d ~/.ssh || mkdir ~/.ssh)
(umask 077 && touch ~/.ssh/authorized_keys)

Next, edit the file .ssh/authorized_keys using your preferred text editor (eg. nano). Then copy and paste the contents of your id_rsa.pub file into the file, save it, and exit your text editor.

Log In Using Your Private Key

You can now SSH or SFTP into your server using your private key. From the command line, you can use:

ssh SYSUSER@x.x.x.x

If you didn't create your key in the default location, you'll need to specify the location:

ssh -i ~/.ssh/custom_key_name SYSUSER@x.x.x.x

If you're using a Windows SSH client, such as PuTTy, look in the configuration settings to specify the path to your private key.

Additional Information

Retrieve Your Public Key from Your Private Key

The following command will retrieve the public key from a private key:

ssh-keygen -y -f /path/to/your_private_key_file (eg. /root/.ssh/id_rsa or ~/.ssh/custom_key_name)

This can be useful, for example, if your server provider generated your SSH key for you and you were only able to download the private key portion of the key pair.

Note: For security purposes, you cannot retrieve the private key if you only have the public key.

Correcting Permissions on the .ssh Directory

The instructions in this article will create your server's .ssh directory and .ssh/authorized_keys file with the correct permissions. However, if you've created them yourself and need to fix permissions, you can run the following commands on your server while SSH'd in as your app's system user.

chmod 700 ~/.ssh
chmod 600 ~/.ssh/authorized_keys

Why Can't HostLaunch Generate these Keys for Me?

That's a great question! Because this SSH key pair allows the same access to the server as your root password, we don't generate them for you to ensure no one else ever has access to your private key. If we were to generate them for you, you would have no assurances that we did not retain a copy of your private key for future use.

Last updated: February 10, 2020

Launch your hosting company now.