文章目录
  1. 1. Step 1: Check for SSH keys
  2. 2. Step 2: Generate a new SSH key
  3. 3. Step 3: Add your SSH key to your account
  4. 4. Step 4: Test the connection

Step 1: Check for SSH keys

1
2
$ ls -al ~/.ssh
# Lists the files in your .ssh directory, if they exist

Check the directory listing to see if you already have a public SSH key. By default, the filenames of the public keys are one of the following:
> id_dsa.pub
> id_ecdsa.pub
> id_ed25519.pub
> id_rsa.pub

Step 2: Generate a new SSH key

  • With Git Bash still open, copy and paste the text below. Make sure you substitute in your GitHub email address.

    1
    2
    3
    $ ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
    # Creates a new ssh key, using the provided email as a label
    Generating public/private rsa key pair.
  • We strongly suggest keeping the default settings as they are, so when you’re prompted to “Enter a file in which to save the key”, just press Enter to continue.

    1
    Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
  • You’ll be asked to enter a passphrase.

    1
    2
    Enter passphrase (empty for no passphrase): [Type a passphrase]
    Enter same passphrase again: [Type passphrase again]
  • After you enter a passphrase, you’ll be given the fingerprint, or id, of your SSH key. It will look something like this:

    1
    2
    3
    4
    Your identification has been saved in /Users/you/.ssh/id_rsa.
    Your public key has been saved in /Users/you/.ssh/id_rsa.pub.
    The key fingerprint is:
    01:0f:f4:3b:ca:85:d6:17:a1:7d:f0:68:9d:f0:a2:db your_email@example.com

Step 3: Add your SSH key to your account

Copy the SSH key to your clipboard. If your key is named id_dsa.pub, id_ecdsa.pub or id_ed25519.pub, then change the filename below from id_rsa.pub to the one that matches your key:

1
2
$ clip < ~/.ssh/id_rsa.pub
# Copies the contents of the id_rsa.pub file to your clipboard

Add the copied key to GitHub:

  • In the top right corner of any page, click setting.
    setting

  • In the user settings sidebar, click SSH keys.
    SSH keys

  • Click Add SSH key.
    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.
    Key

  • Click Add key.
    Add key

  • Confirm the action by entering your GitHub password.

Step 4: Test the connection

To make sure everything is working, you’ll now try to SSH into . When you do this, you will be asked to authenticate this action using your password, which is the SSH key passphrase you created earlier.

  • Open Git Bash and enter:

    1
    2
    $ ssh -T git@github.com
    # Attempts to ssh to GitHub
  • You may see this warning:

    1
    2
    3
    The authenticity of host 'github.com (207.97.227.239)' can't be established.
    RSA key fingerprint is 16:27:ac:a5:76:28:2d:36:63:1b:56:4d:eb:df:a6:48.
    Are you sure you want to continue connecting (yes/no)?
  • Verify the fingerprint in the message you see matches the following message, then type yes:

    1
    2
    Hi username! You've successfully authenticated, but GitHub does not
    provide shell access.
  • If the username in the message is yours, you’ve successfully set up your SSH key!

文章目录
  1. 1. Step 1: Check for SSH keys
  2. 2. Step 2: Generate a new SSH key
  3. 3. Step 3: Add your SSH key to your account
  4. 4. Step 4: Test the connection