---
layout: guide
title: Generating SSH keys (OSX)
subtitle: This guide will step you through the process of generating a keypair on Mac OSX and uploading it to GitHub
description: Setting up SSH keys on Mac OSX
categories: mac setup
---

Generating an SSH key on OSX is a fairly straightforward process.  First and foremost, open up Terminal.app.  You can usually find it at @/Applications/Utilities@.

h3. Backup and remove existing keys

Unless this is your first time setting up ssh or git on your computer, you should doublecheck that keys do not already exist.  If they do you can either use the existing key(s) or remove them.  In either case, you should make a backup of the keys.

<pre>$ cd ~/.ssh
$ ls
config  id_rsa  id_rsa.pub  known_hosts
$ cd ..
$ mkdir ssh_key_backup
$ cp .ssh/id_rsa* ssh_key_backup
$ rm .ssh/id_rsa*</pre>

Here we have an existing keypair, @id_rsa@ and @id_rsa.pub@, which we've copied into @~/ssh_key_backup@ before removing.  By default, ssh will use keys in @~/.ssh@ that are named @id_rsa@, @id_dsa@ or @identity@.

h3. Generating a key

p(. _If you have an existing keypair you wish to use, you can skip this step._

Now that we're certain ssh won't use an existing key, it's time to generate a new keypair.  Lets make an RSA keypair:

<pre>$ ssh-keygen -t rsa -c "tekkub@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/tekkub/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /Users/tekkub/.ssh/id_rsa.
Your public key has been saved in /Users/tekkub/.ssh/id_rsa.pub.
The key fingerprint is:
01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db tekkub@gmail.com
The key's randomart image is:
+--[ RSA 2048]----+
|     .+   +      |
|       = o O .   |
|        = * *    |
|       o = +     |
|      o S .      |
|     o o =       |
|      o . E      |
|                 |
|                 |
+-----------------+</pre>

At each prompt we just hit enter.  This will generate the key with the default name and no passphrase.  If you want to add a passphrase to your key you can do so later.  You will be prompted for the passphrase every time you push or pull from GitHub, so you might not want to use one unless you are using a shared computer.

h3. Adding the key to your GitHub account

Now launch your browser and open the "account page":https://github.com/account.  In the "SSH Public Keys" section click "add another public key", then paste your public key into the "key" field.  If you leave the title blank the key comment (your email) will be used for the title.

Make sure you use the public key (@id_rsa.pub@ in our example), and do not add any newlines or whitespace inside the key.

!/images/add_key.png!

h3. Testing things out

Testing if our new key works is simple:

<pre>$ ssh git@github.com
Hi tekkub! You've successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.</pre>

If you do not get the "successfully authenticated" message, check out the "troubleshooting guide":http://github.com/guides/addressing-authentication-problems-with-ssh.