Skip to content

Instantly share code, notes, and snippets.

@austinhappel
Created December 12, 2012 04:45
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save austinhappel/4264925 to your computer and use it in GitHub Desktop.
Save austinhappel/4264925 to your computer and use it in GitHub Desktop.
Simple commands for managing ssh keys on servers.

Overview

This is a basic walk through of how to generate an ssh key and how to set it up on a server so you can log in without having to type your password every time you want to log in.

Steps

  1. Generate ssh key
  2. Add your key to the authorized_keys file on your server.

Step 1: generate ssh public/private key

$ ssh-keygen -t rsa -b 4096

What this does

Generates a 4096 bit RSA private / public keypair. By default this generates id_rsa and id_rsa.pub in your ~/.ssh folder. If you chose a different location or file name, you'll have to edit step 2 accordingly.

Step 2: Copy your new pubkey into the authorized_keys file of your remote server.

$ TEMP=`cat ~/.ssh/id_rsa.pub` && ssh [user]@[server.com] "mkdir -p ~/.ssh && echo '${TEMP}' >> ~/.ssh/authorized_keys" && unset TEMP

What this does

  1. Copies your id_rsa.pub file to a local variable
  2. Makes a ~/.ssh directory in the home folder of your server
  3. Appends the public key to your ~/.ssh/authorized_keys file.
  4. Deletes the local variable we made originally to store the pubkey.

How to use

  1. Replace [user] and [server.com] with your server username/address in the line below.
  2. Alter the paths to your local public key if you chose to use something different than ~/.ssh/id_rsa.pub.
  3. Copy and paste this line (without the initial $) into your terminal and hit [enter]

Note

  • This has only been tested on Mac OSX.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment