Appendix D. SSH Keys

SSH keys allow you to make a connection to a remote machine without having to enter a password every time. The keys themselves come in pairs: a public-facing key and a private key. The private key should be treated like a password, and never shared with anyone. The public-facing key will be “installed” elsewhere, such as a code hosting system.

Create Your Own SSH Keys

To create an SSH key, you will need to run a program, which will save a pair of files. The necessary software is already installed on *nix-based systems, but Windows users will need to download additional (free) software.

Linux, OS X, and Unix-variants

To generate a key pair, run the following command:

$ ssh-keygen -t rsa -b 4096 -C "[email protected]"

You will be prompted for the following information:

File location

Accept the default location by pressing Return to continue.

Password

It’s optional, but you really should have one. Make it memorable or store it in a very secure password keeper that you use regularly.

The fingerprint for your key will be printed to the screen, and the key pair will be saved to the appropriate location in ~/.ssh/.

You will now need to register this key with your system so that you can begin using it.

This is where things get a little secret agent. You need to register your keys with the local “agent” (using OS X? think “keychain,” but different). Begin the ssh-agent application and redirect it to use a Bourne shell:

$ eval "$(ssh-agent -s)"

Register your SSH key with the agent:

$ ssh-add ~/.ssh/id_rsa

Your key has been registered.

If you need to use the key immediately, skip ahead to “Retrieving Your Public SSH Key”.

Windows

To generate an SSH key-pair on Windows you will need to use the software, PuTTYgen:

  1. Locate the latest binary for PuTTY from the PuTTY Download Page. The file is named puttygen.exe.

  2. Right-click the link puttygen.exe and choose “Save link as.” The text may vary slightly depending on your browser.

  3. When prompted, select a folder that you can find easily (for example, your desktop folder).

  4. Locate the PuTTYgen application on your desktop. Double-click the icon to run the program.

  5. At the bottom of the window, below “Type of key to generate,” select SSH-2 RSA.

  6. Locate and click the button “Generate.”

  7. Wiggle your mouse. Seriously. You’ll be making random data (noise), which helps with the key-generation process. Continue doing so until the progress bar is full.

  8. You will be prompted for a passphrase. It’s optional, but you should add one.

  9. Locate and click the button “Save private key.”

  10. Locate and click the button “Save public key.”

This should save the keys to the appropriate location in ~/.ssh/.

If you are ready to use the SSH key immediately, complete the following steps as well:

  1. Locate the heading “Public key for pasting into OpenSSH authorized_keys file.”

  2. Right-click the random string below the heading.

  3. Choose “Select all,” and then “Copy.”

Your public key has been copied to the clipboard. You are ready to proceed.

Retrieving Your Public SSH Key

When your code hosting system asks for your “Public SSH Key,” it needs the contents of the file id_rsa.pub. This file is usually stored in a hidden folder of your home directory: .ssh. To locate this file, and copy its contents to your clipboard, complete the commands outlined next as is relevant for your operating system. By working from the command line, you can avoid trying to find an editor that recognizes a .pub file. It’s just text, but the text editors you have installed probably don’t know that.

OS X:

  1. Open a terminal window.

  2. Run the following command: cat ~/.ssh/id_rsa.pub | pbcopy

Linux:

  1. Open a terminal window.

  2. Run the following command: cat ~/.ssh/id_rsa.pub. You should have a very long string of characters printed to the screen. It should stretch the entire width of the terminal, and it should not include the words “PRIVATE KEY.” If the file is not found, you will need to create an SSH key first.

  3. Copy all of the text that was printed to the screen.

Windows:

  1. Open a Git Bash window.

  2. Run the following command: clip < ~/.ssh/id_rsa.pub. This will copy your public SSH key to the clipboard.

Your public SSH key is now copied to the clipboard and you are ready to paste it into the configuration screen for your code hosting system of choice.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset