Created
November 12, 2017 19:57
-
-
Save dannygsmith/b0dc5cd1c8c983cb0e36cdf81bd13500 to your computer and use it in GitHub Desktop.
Generating a new SSH key and adding it to the ssh-agent
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
[Source](https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent/ "Permalink to Generating a new SSH key and adding it to the ssh-agent") | |
# Generating a new SSH key and adding it to the ssh-agent | |
After you've checked for existing SSH keys, you can generate a new SSH key to use for authentication, then add it to the ssh-agent. | |
If you don't already have an SSH key, you must [generate a new SSH key][1]. If you're unsure whether you already have an SSH key, check for [existing keys][2]. | |
If you don't want to reenter your passphrase every time you use your SSH key, you can [add your key to the SSH agent][3], which manages your SSH keys and remembers your passphrase. | |
### Generating a new SSH key | |
1. Open TerminalTerminalGit Bash. | |
2. Paste the text below, substituting in your GitHub email address. | |
ssh-keygen -t rsa -b 4096 -C "[email protected]_" | |
This creates a new ssh key, using the provided email as a label. | |
Generating public/private rsa key pair. | |
3. When you're prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location. | |
Enter a file in which to save the key (/Users/_you_/.ssh/id_rsa): [_Press enter]_ | |
Enter a file in which to save the key (/c/Users/_you_/.ssh/id_rsa):[_Press enter]_ | |
Enter a file in which to save the key (/home/_you_/.ssh/id_rsa): [_Press enter]_ | |
4. At the prompt, type a secure passphrase. For more information, see ["Working with SSH key passphrases"][4]. | |
Enter passphrase (empty for no passphrase): [_Type a passphrase]_ | |
Enter same passphrase again: [_Type passphrase again]_ | |
### Adding your SSH key to the ssh-agent | |
Before adding a new SSH key to the ssh-agent to manage your keys, you should have [checked for existing SSH keys][2] and [generated a new SSH key][5]. When adding your SSH key to the agent, use the default macOS `ssh-add` command, and not an application installed by [macports][6], [homebrew][7], or some other external source. | |
1. Start the ssh-agent in the background. | |
eval "$(ssh-agent -s)" | |
Agent pid 59566 | |
2. If you're using macOS Sierra 10.12.2 or later, you will need to modify your `~/.ssh/config` file to automatically load keys into the ssh-agent and store passphrases in your keychain. | |
Host * | |
AddKeysToAgent yes | |
UseKeychain yes | |
IdentityFile ~/.ssh/id_rsa | |
3. Add your SSH private key to the ssh-agent and store your passphrase in the keychain. If you created your key with a different name, or if you are adding an existing key that has a different name, replace _id_rsa_ in the command with the name of your private key file. | |
$ ssh-add -K ~/.ssh/id_rsa | |
4. [Add the SSH key to your GitHub account][8]. | |
If you have [GitHub Desktop][9] installed, you can use it to clone repositories and not deal with SSH keys. It also comes with the Git Bash tool, which is the preferred way of running `git` commands on Windows. | |
1. Ensure the ssh-agent is running: | |
2. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace _id_rsa_ in the command with the name of your private key file. | |
ssh-add ~/.ssh/id_rsa | |
3. [Add the SSH key to your GitHub account][8]. | |
1. Start the ssh-agent in the background. | |
eval "$(ssh-agent -s)" | |
Agent pid 59566 | |
2. Add your SSH private key to the ssh-agent. If you created your key with a different name, or if you are adding an existing key that has a different name, replace _id_rsa_ in the command with the name of your private key file. | |
ssh-add ~/.ssh/id_rsa | |
3. [Add the SSH key to your GitHub account][8]. | |
### Further reading | |
[1]: https://help.github.com#generating-a-new-ssh-key | |
[2]: https://help.github.com/articles/checking-for-existing-ssh-keys | |
[3]: https://help.github.com#adding-your-ssh-key-to-the-ssh-agent | |
[4]: https://help.github.com/articles/working-with-ssh-key-passphrases | |
[5]: https://help.github.com/articles/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent#generating-a-new-ssh-key | |
[6]: https://www.macports.org/ | |
[7]: http://brew.sh/ | |
[8]: https://help.github.com/articles/adding-a-new-ssh-key-to-your-github-account | |
[9]: https://desktop.github.com/ | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment