How to add ssh key to git
How to add ssh key to git
Generating a new SSH key and adding it to the ssh-agent
In this article
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.
About SSH key passphrases
You can access and write data in repositories on GitHub AE using SSH (Secure Shell Protocol). When you connect via SSH, you authenticate using a private key file on your local machine. For more information, see «About SSH.»
When you generate an SSH key, you can add a passphrase to further secure the key. Whenever you use the key, you must enter the passphrase. If your key has a passphrase and you don’t want to enter the passphrase every time you use the key, you can add your key to the SSH agent. The SSH agent manages your SSH keys and remembers your passphrase.
If you don’t already have an SSH key, you must generate a new SSH key to use for authentication. If you’re unsure whether you already have an SSH key, you can check for existing keys. For more information, see «Checking for existing SSH keys.»
If you want to use a hardware security key to authenticate to GitHub AE, you must generate a new SSH key for your hardware security key. You must connect your hardware security key to your computer when you authenticate with the key pair. For more information, see the OpenSSH 8.2 release notes.
Generating a new SSH key
You can generate a new SSH key on your local machine. After you generate the key, you can add the key to your account on GitHub AE to enable authentication for Git operations over SSH.
Paste the text below, substituting in your GitHub AE email address.
This creates a new SSH key, using the provided email as a label.
When you’re prompted to «Enter a file in which to save the key,» press Enter. This accepts the default file location.
At the prompt, type a secure passphrase. For more information, see «Working with SSH key passphrases.»
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 and generated a new SSH key. When adding your SSH key to the agent, use the default macOS ssh-add command, and not an application installed by macports, homebrew, or some other external source.
Start the ssh-agent in the background.
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.
First, check to see if your
/.ssh/config file exists in the default location.
If the file doesn’t exist, create the file.
/.ssh/config file, then modify the file to contain the following lines. If your SSH key file has a different name or path than the example code, modify the filename or path to match your current setup.
Notes:
If you chose not to add a passphrase to your key, you should omit the UseKeychain line.
If you see a Bad configuration option: usekeychain error, add an additional line to the configuration’s’ Host * section.
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.
Add the SSH key to your account on GitHub AE. For more information, see «Adding a new SSH key to your GitHub account.»
If you have GitHub Desktop installed, you can use it to clone repositories and not deal with SSH keys.
Ensure the ssh-agent is running. You can use the «Auto-launching the ssh-agent» instructions in «Working with SSH key passphrases», or start it manually:
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.
Add the SSH key to your account on GitHub AE. For more information, see «Adding a new SSH key to your GitHub account.»
Start the ssh-agent in the background.
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.
Add the SSH key to your account on GitHub AE. For more information, see «Adding a new SSH key to your GitHub account.»
Generating a new SSH key for a hardware security key
If you are using macOS or Linux, you may need to update your SSH client or install a new SSH client prior to generating a new SSH key. For more information, see «Error: Unknown key type.»
Insert your hardware security key into your computer.
Paste the text below, substituting in the email address for your account on GitHub AE.
When you are prompted, touch the button on your hardware security key.
When you are prompted to «Enter a file in which to save the key,» press Enter to accept the default file location.
When you are prompted to type a passphrase, press Enter.
Add the SSH key to your account on GitHub. For more information, see «Adding a new SSH key to your GitHub account.»
Help us make these docs great!
All GitHub docs are open source. See something that’s wrong or unclear? Submit a pull request.
How to Add SSH Keys to Your GitHub Account
2 Minutes, 31 Seconds to Read
In this tutorial:
GitHub offers a convenient SSH key feature for accessing your repositories on the command line. Instead of using usernames and passwords every time you push to or pull from your repository, you can use your key. In this article we’ll learn how to add SSH keys to your GitHub account.
Why use an SSH key?
Using an SSH key is a convenient and secure way to access your account. It’s convenient because you don’t need to remember a long password. And you can make your actual password so brutally long and secure that no human or bot could guess it. The SSH key works like an actual key that only you possess.
How to Create an SSH Key
In order to use an SSH key with Git, you must first create the key on your computer. If you already have an SSH key, you can skip these steps. In order to check if you have a key, you can run this command:
Follow along with the steps below to create the key and copy its contents. You will need to open your computer’s default terminal application.
It’s important to remember that the SSH key has two parts: a private key and a public key. As the name suggests, the private key is only for private use. Never ever share your private key with anyone (with exceptions made for your system administrator or other trusted people within your organization). This will be important later.
How to Add an SSH Key to your Github Account
Now we have already seen why you might want to use an SSH key. We also demonstrated how to create an SSH key on your computer.
As mentioned above, we have a private key and a public key. In the steps below you’ll learn how to upload your public key to GitHub.
Adding a new SSH key to your GitHub account
In this article
To configure your account on GitHub.com to use your new (or existing) SSH key, you’ll also need to add the key to your account.
About addition of SSH keys to your account
You can access and write data in repositories on GitHub.com using SSH (Secure Shell Protocol). When you connect via SSH, you authenticate using a private key file on your local machine. For more information, see «About SSH.»
After you generate an SSH key pair, you must add the public key to GitHub.com to enable SSH access for your account.
Before adding a new SSH key to your account on GitHub.com, complete the following steps.
Adding a new SSH key to your account
After adding a new SSH key to your account on GitHub.com, you can reconfigure any local repositories to use SSH. For more information, see «Switching remote URLs from HTTPS to SSH.»
Note: GitHub improved security by dropping older, insecure key types on March 15, 2022.
As of that date, DSA keys ( ssh-dss ) are no longer supported. You cannot add new DSA keys to your personal account on GitHub.com.
RSA keys ( ssh-rsa ) with a valid_after before November 2, 2021 may continue to use any signature algorithm. RSA keys generated after that date must use a SHA-2 signature algorithm. Some older clients may need to be upgraded in order to use SHA-2 signatures.
Copy the SSH public key to your clipboard.
If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.
In the upper-right corner of any page, click your profile photo, then click Settings.
In the «Access» section of the sidebar, click
SSH and GPG keys.
Click New SSH key or Add SSH key.
In the «Title» field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key «Personal MacBook Air».
Paste your key into the «Key» field.
Click Add SSH key.
If prompted, confirm access to your account on GitHub. For more information, see «Sudo mode.»
Copy the SSH public key to your clipboard.
If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.
In the upper-right corner of any page, click your profile photo, then click Settings.
In the «Access» section of the sidebar, click
SSH and GPG keys.
Click New SSH key or Add SSH key.
In the «Title» field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key «Personal MacBook Air».
Paste your key into the «Key» field.
Click Add SSH key.
If prompted, confirm access to your account on GitHub. For more information, see «Sudo mode.»
Copy the SSH public key to your clipboard.
If your SSH public key file has a different name than the example code, modify the filename to match your current setup. When copying your key, don’t add any newlines or whitespace.
In the upper-right corner of any page, click your profile photo, then click Settings.
In the «Access» section of the sidebar, click
SSH and GPG keys.
Click New SSH key or Add SSH key.
In the «Title» field, add a descriptive label for the new key. For example, if you’re using a personal Mac, you might call this key «Personal MacBook Air».
Paste your key into the «Key» field.
Click Add SSH key.
If prompted, confirm access to your account on GitHub. For more information, see «Sudo mode.»
To learn more about GitHub CLI, see «About GitHub CLI.»
Before you can use the GitHub CLI to add an SSH key to your account, you must authenticate to the GitHub CLI. For more information, see gh auth login in the GitHub CLI documentation.
To add an SSH key to your GitHub account, use the ssh-key add subcommand, specifying your public key.
If you generated your SSH key by following the instructions in «Generating a new SSH key», you can add the key to your account with this command.
Generating a new SSH key and adding it to the ssh-agent
In this article
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.
About SSH key passphrases
You can access and write data in repositories on GitHub.com using SSH (Secure Shell Protocol). When you connect via SSH, you authenticate using a private key file on your local machine. For more information, see «About SSH.»
When you generate an SSH key, you can add a passphrase to further secure the key. Whenever you use the key, you must enter the passphrase. If your key has a passphrase and you don’t want to enter the passphrase every time you use the key, you can add your key to the SSH agent. The SSH agent manages your SSH keys and remembers your passphrase.
If you don’t already have an SSH key, you must generate a new SSH key to use for authentication. If you’re unsure whether you already have an SSH key, you can check for existing keys. For more information, see «Checking for existing SSH keys.»
If you want to use a hardware security key to authenticate to GitHub, you must generate a new SSH key for your hardware security key. You must connect your hardware security key to your computer when you authenticate with the key pair. For more information, see the OpenSSH 8.2 release notes.
Generating a new SSH key
You can generate a new SSH key on your local machine. After you generate the key, you can add the key to your account on GitHub.com to enable authentication for Git operations over SSH.
Note: GitHub improved security by dropping older, insecure key types on March 15, 2022.
As of that date, DSA keys ( ssh-dss ) are no longer supported. You cannot add new DSA keys to your personal account on GitHub.com.
RSA keys ( ssh-rsa ) with a valid_after before November 2, 2021 may continue to use any signature algorithm. RSA keys generated after that date must use a SHA-2 signature algorithm. Some older clients may need to be upgraded in order to use SHA-2 signatures.
Paste the text below, substituting in your GitHub email address.
Note: If you are using a legacy system that doesn’t support the Ed25519 algorithm, use:
This creates a new SSH key, using the provided email as a label.
When you’re prompted to «Enter a file in which to save the key,» press Enter. This accepts the default file location.
At the prompt, type a secure passphrase. For more information, see «Working with SSH key passphrases.»
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 and generated a new SSH key. When adding your SSH key to the agent, use the default macOS ssh-add command, and not an application installed by macports, homebrew, or some other external source.
Start the ssh-agent in the background.
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.
First, check to see if your
/.ssh/config file exists in the default location.
If the file doesn’t exist, create the file.
/.ssh/config file, then modify the file to contain the following lines. If your SSH key file has a different name or path than the example code, modify the filename or path to match your current setup.
Notes:
If you chose not to add a passphrase to your key, you should omit the UseKeychain line.
If you see a Bad configuration option: usekeychain error, add an additional line to the configuration’s’ Host * section.
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_ed25519 in the command with the name of your private key file.
Add the SSH key to your account on GitHub. For more information, see «Adding a new SSH key to your GitHub account.»
If you have GitHub Desktop installed, you can use it to clone repositories and not deal with SSH keys.
Ensure the ssh-agent is running. You can use the «Auto-launching the ssh-agent» instructions in «Working with SSH key passphrases», or start it manually:
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_ed25519 in the command with the name of your private key file.
Add the SSH key to your account on GitHub. For more information, see «Adding a new SSH key to your GitHub account.»
Start the ssh-agent in the background.
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_ed25519 in the command with the name of your private key file.
Add the SSH key to your account on GitHub. For more information, see «Adding a new SSH key to your GitHub account.»
Generating a new SSH key for a hardware security key
If you are using macOS or Linux, you may need to update your SSH client or install a new SSH client prior to generating a new SSH key. For more information, see «Error: Unknown key type.»
Insert your hardware security key into your computer.
Paste the text below, substituting in the email address for your account on GitHub.
Note: If the command fails and you receive the error invalid format or feature not supported, you may be using a hardware security key that does not support the Ed25519 algorithm. Enter the following command instead.
When you are prompted, touch the button on your hardware security key.
When you are prompted to «Enter a file in which to save the key,» press Enter to accept the default file location.
When you are prompted to type a passphrase, press Enter.
Add the SSH key to your account on GitHub. For more information, see «Adding a new SSH key to your GitHub account.»
Help us make these docs great!
All GitHub docs are open source. See something that’s wrong or unclear? Submit a pull request.
Set up an SSH key
When you set up SSH key, you create a key pair that contains a private key (saved to your local computer) and a public key (uploaded to Bitbucket). Bitbucket uses the key pair to authenticate anything the associated account can access. This two-way mechanism prevents man-in-the-middle attacks.
This first key pair is your default SSH identity. If you need more than a default identity, you can set up additional keys.
For security reasons, we recommend that you generate a new SSH key and replace the existing key on your account at least once a year.
You can’t use the same key between accounts. You must create new keys for each individual Bitbucket account.
Set up SSH for Git on Windows
Use this section to create a default identity and SSH key when you’re using Git on Windows. By default, the system adds keys for all identities to the /Users/ /.ssh directory.
Step 1. Set up your default identity
For Windows 7 or earlier
You can only enter ssh-keygen into the Git Bash window. It won’t work in the Command prompt.
The command prompts you for a file to save the key in:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/emmap1/.ssh/id_rsa):
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/emmap1/.ssh/id_rsa): c:\Users\emmap1\.ssh\my-new-ssh-key
3. Enter and re-enter a passphrase when prompted.
The command creates your default identity with its public and private keys. The whole interaction looks similar to this:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/emmap1/.ssh/id_rsa):
Created directory ‘/c/Users/emmap1/.ssh’.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /c/Users/emmap1/.ssh/id_rsa.
Your public key has been saved in /c/Users/emmap1/.ssh/id_rsa.pub.
The key fingerprint is: e7:94:d1:a3:02:ee:38:6e:a4:5e:26:a3:a9:f4:95:d4 emmap1@EMMA-PC
You should see something like the following:
The command displays two files, one for the public key (for example id_rsa.pub ) and one for the private key (for example, id_rsa ).
Step 2. Add the key to the ssh-agent
If you don’t want to type your password each time you use the key, you’ll need to add it to the ssh-agent.
To start the agent, run the following:
Enter ssh-add followed by the path to the private key file:
Step 3. Add the public key to your Account settings
From Bitbucket, choose Personal settings from your avatar in the lower left.
Click SSH keys. If you’ve already added keys, you’ll see them on this page.
From Bitbucket, click Add key.
Paste the copied public key into the SSH Key field.
Click Save.
Bitbucket sends you an email to confirm the addition of the key.
Return to the command line and verify your configuration and username by entering the following command:
The command message tells you which of your Bitbucket accounts can log in with that key.
Now that you’ve got an SSH key set up, use the SSH URL the next time you clone a repository. If you already have a repository that you cloned over HTTPS, change the remote URL for your repository to its SSH URL.
Edit an SSH key
After you add a key, you can edit the key’s Label but not the key itself. To change the key’s contents, you need to delete and re-add the key.
Set up SSH on macOS/Linux
Use this section to create a default identity and SSH key on macOS or Linux. By default, the system adds keys to the /Users/ /.ssh directory on macOS and /home/ /.ssh on Linux.
Step 1. Set up your default identity
1. From the terminal, enter ssh-keygen at the command line.
The command prompts you for a file to save the key in:
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/emmap1/.ssh/id_rsa):
2. Press the Enter or Return key to accept the default location.
We recommend you keep the default key name unless you have a reason to change it.
$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/Users/emmap1/.ssh/id_rsa): /Users/emmap1/.ssh/my-new-ssh-key
3. Enter and re-enter a passphrase when prompted.
The command creates your default identity with its public and private keys. The whole interaction will look similar to the following:
4. List the contents of
/.ssh to view the key files.
/.ssh
id_rsa id_rsa.pub
The command displays two files, one for the public key (for example id_rsa.pub ) and one for the private key (for example, id_rsa ).
Step 2. Add the key to the ssh-agent
If you don’t want to type your password each time you use the key, you’ll need to add it to the ssh-agent.
To start the agent, run the following:
$ eval `ssh-agent`
Agent pid 9700
Enter ssh-add followed by the path to the private key file:
(macOS only) So that your computer remembers your password each time it restarts, open (or create) the
/.ssh/config file and add these lines to the file:
Step 3. Add the public key to your Account settings
From Bitbucket, choose Personal settings from your avatar in the lower left.
Click SSH keys.
If you’ve already added keys, you’ll see them on this page.
In your terminal window, copy the contents of your public key file. If you renamed the key, replace id_rsa.pub with the public key file name.
On Linux, you can cat the contents:
On macOS, the following command copies the output to the clipboard:
Select and copy the key output in the clipboard.
If you have problems with copy and paste, you can open the file directly with Notepad. Select the contents of the file (just avoid selecting the end-of-file characters).
From Bitbucket, click Add key.
Paste the copied public key into the SSH Key field.
You may see an email address on the last line when you paste. It doesn’t matter whether or not you include the email address in the Key.
Click Save.
Bitbucket sends you an email to confirm the addition of the key.
Return to the terminal window and verify your configuration and username by entering the following command:
The command message tells you which of your Bitbucket accounts can log in with that key.
Now that you’ve got an SSH key set up, use the SSH URL the next time you clone a repository. If you already have a repository that you cloned over HTTPS, change the remote URL for your repository to use its SSH URL.
Edit an SSH key
After you add a key, you can edit the key’s Label but not the key itself. To change the key’s contents, you need to delete and re-add the key.
Set up SSH with Sourcetree on Windows
Use this section to generate an SSH key using Sourcetree. When you create an SSH key with Sourcetree, you can save the public and private key wherever you want locally. You may want to create an SSH directory when you do, so that you can refer to your SSH keys whenever you need them.
Step 1. Install Sourcetree and add your Bitbucket account
If you don’t yet have Sourcetree, go to https://www.sourcetreeapp.com/ and click the Download free button.
You may see the Load SSH Key? dialog after installation. Click No if you don’t have one and want to use Sourcetree to create one.
Add your account and select SSH as the Preferred Protocol. If you don’t connect your account during set up, click Remote to open the Remote repositories page and click Add an account.
Step 2. Create an SSH key
From Tools, select Create or Import SSH Keys.
From the PuTTY Key Generator dialog, click the Generate button.
As the SSH key generates, hover your mouse over the blank area in the dialog. It may take a minute or two.
When SSH key generation is complete, you see the public key and a few other fields.
Enter a passphrase for your SSH key in the Key passphrase and Confirm passphrase fields.
Click Save private key. From the save dialog, choose where to save your private key, name the file, and click Save.
Close the PuTTY Key Generator dialog.
Step 3. Install your private key on Pageant
Sourcetree comes with an SSH authentication agent called Pageant. Load your private key into Pageant to automatically authenticate so that you don’t need to enter your passphrase.
Double-click the Pageant (PuTTY Authentication Agent) icon in your system tray to open the Pageant Key List dialog.
Click the Add Key button to open the Select Private Key File dialog.
Navigate to the private key file you saved in Step 1 and click Open.
Enter the passphrase for your SSH key and click OK.
Pageant shows your key in the running list.
Click Close.
Step 4. Add the public key to your Account settings
From Sourcetree, open the PuTTY Key Generator dialog by going to Tools > Create or Import SSH Keys.
Click Load, navigate to your SSH folder, and click the private key. Make sure you’re looking at All files if you don’t see your private key.
Enter your passphrase for the SSH key and click OK.
Copy the public key in the first field.
From Bitbucket, choose Personal settings from your avatar in the lower left.
The Account settings page opens.
Click SSH keys.
If you’ve already added keys, you’ll see them on this page.
Click Add key.
Paste the copied public key into the SSH Key field.
Click Save.
Bitbucket sends you an email to confirm the addition of the key.
Now that you’ve got an SSH key set up, use the SSH URL the next time you clone a repository. If you already have a repository that you cloned over HTTPS, change the remote URL for your repository to its SSH URL.
Edit an SSH key
After you add a key, you can edit the key’s Label but not the key itself. To change the key’s contents, you need to delete and re-add the key.
Set up SSH with Sourcetree on macOS
When you create an SSH key with Sourcetree on macOS, you can only create one key. You’ll need to use the command line if you want additional keys.
Step 1. Install Sourcetree and add your Bitbucket account
If you don’t yet have Sourcetree, go to https://www.sourcetreeapp.com/ and click the Download free button.
Open the ZIP file to install Sourcetree. Refer to the Install Sourcetree page for more details.
If you don’t connect your account during set up, you can add it from the Accounts tab by selecting Preferences from the Sourcetree menu.
Step 2. Create an SSH key
Follow these steps if you don’t already have an SSH key for an account. If you do have an SSH key and you want to generate another key, you’ll have to use the terminal because you can’t use Sourcetree to create a second key.
Creating an SSH key looks something like this:
From the Sourcetree menu, select Preferences.
Click the Accounts tab, select the account where you want to add the SSH key and click Edit.
Change the Protocol to SSH if it’s not already selected.
Hold down the OPTION key on your keyboard to see the Generate Key button.
If you’ve already generated an SSH key for this account from Sourcetree, the OPTION key won’t do anything. Use your existing key or generate another key from the terminal.
Click Generate Key.
Enter a passphrase for the SSH key in the Passphrase and Confirm Passphrase fields.
Click Create.
Step 3. Add the public key to your Account settings
From Bitbucket, choose Personal settings from your avatar in the lower left.
The Account settings page opens.
Click SSH keys.
If you’ve already added keys, you’ll see them on this page.
Select your account from your Accounts tab in Sourcetree.
Click the Copy to Clipboard button to copy your public SSH key.
From Bitbucket, click Add key.
Paste the copied public key into the SSH Key field.
Click Save.
Bitbucket sends you an email to confirm the addition of the key.
Now that you’ve got an SSH key set up, use the SSH URL the next time you clone a repository. If you already have a repository that you cloned over HTTPS, change the remote URL for your repository to its SSH URL.
Edit an SSH key
After you add a key, you can edit the key’s Label but not the key itself. To change the key’s contents, you need to delete and re-add the key.
Источники информации:
- http://www.inmotionhosting.com/support/server/ssh/how-to-add-ssh-keys-to-your-github-account/
- http://docs.github.com/en/authentication/connecting-to-github-with-ssh/adding-a-new-ssh-key-to-your-github-account
- http://docs.github.com/en/authentication/connecting-to-github-with-ssh/generating-a-new-ssh-key-and-adding-it-to-the-ssh-agent
- http://support.atlassian.com/bitbucket-cloud/docs/set-up-an-ssh-key/