Skip to content

Generate SSH Keys in Two Easy Steps

openssh-logoThis post is probably as much for me as it is everyone else. I got sick of having to look up 3 or 4 different SSH keygen tutorials every time I needed to generate a private/public SSH key pair, so I thought I’d write up my own. Here’s how to make your own SSH keys at a shell prompt (i.e. Linux):

  1. Run the following commands on the machine you will be logging into:

    chmod 700 ~/.ssh
    cat ~/.ssh/ >> ~/.ssh/authorized_keys2
    chmod 600 ~/.ssh/authorized_keys2
    rm ~/.ssh/

    Note: You may need to replace authorized_keys2 with just authorized_keys.

  2. Copy the id_rsa file to the client machine (the machine you will be logging in from) and run the following command:

    ssh-add id_rsa

That’s all there is to it! Please be sure to keep the id_rsa file safe, and don’t ever share it with anyone unless you want them to log into the account. You should protect the private keyfile in the same manner you would protect any password.

You may get an error on the client machine that says Could not open a connection to your authentication agent. If you see this message, run the following command:

exec ssh-agent bash

That will start the ssh agent and allow you to connect to it so that you can add the new key.

Alternate Step 2 for Windows Clients

If your client is a Windows machine, you’re probably using PuTTY to connect to the host. If that is the case, step 2 will be different for you and you’ll want to use PuTTYgen to import the key.

Just launch PuTTYgen, click Conversions and then Import key.
Then click Save private key and you’re done!

Connecting to an SSH Host with PuTTY

You should be able to use the key to connect to the host from PuTTY. Just start up PuTTY, load your saved session or enter the host info, go to Connection > SSH > Auth and browse to the private key (.ppk file) you just saved.
Using Key File in PuTTY

1 thought on “Generate SSH Keys in Two Easy Steps”

Leave a Reply

Your email address will not be published. Required fields are marked *