- Generate a new SSH key
- Add your key to the ssh-agent
- Add your SSH key to your account
- Test the connection
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.
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.
# Enter file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]
You’ll be asked to enter a passphrase.建议如果是个人玩玩的项目,没必要加这个密码,否则pull和push时候还挺麻烦的。
# Enter passphrase (empty for no passphrase): [Type a passphrase]
# Enter same passphrase again: [Type passphrase again]
Tip: We strongly recommend a very good, secure passphrase. For more information, see “Working with SSH key passphrases”. After you enter a passphrase, you’ll be given the fingerprint, or id, of your SSH key. It will look something like this:
# 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
Add your key to the ssh-agent
To configure the ssh-agent program to use your SSH key:
If you have GitHub for Windows 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.
Ensure ssh-agent is enabled:
If you are using Git Bash, turn on ssh-agent:
# start the ssh-agent in the background
ssh-agent -s
# Agent pid 59566
If you are using another terminal prompt, such as msysgit, turn on ssh-agent:
# start the ssh-agent in the background
eval $(ssh-agent -s)
# Agent pid 59566
Add your SSH key to the ssh-agent:我这步没成功,好像就没成功过,但是不影响使用。
ssh-add ~/.ssh/id_rsa
Add your SSH key to your account
To configure your GitHub account to use your SSH key:
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:
clip < ~/.ssh/id_rsa.pub # 这句命令很不错!
# Copies the contents of the id_rsa.pub file to your clipboard
Warning: It’s important to copy the key exactly without adding newlines or whitespace. Add the copied key to GitHub:
Settings icon in the user barIn the top right corner of any page, click your profile photo, then click Settings. SSH keysIn the user settings sidebar, click SSH keys.
SSH Key buttonClick 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”. The key fieldPaste your key into the “Key” field. The Add key buttonClick Add key. Confirm the action by entering your GitHub password.
Test the connection
To make sure everything is working, you’ll now try to SSH into GitHub. 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:
ssh -T git@github.com
# Attempts to ssh to GitHub
You may see this warning:
# 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:
# Hi username! You've successfully authenticated, but GitHub does not
# provide shell access.
## 示例操作
$ ssh-keygen.exe -t rsa -b 4096 -C "zyDe8ug@gmail.com"
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/someone/.ssh/id_rsa): # 建议就默认这个名字
/c/Users/someone/.ssh/id_rsa already exists.
Overwrite (y/n)? y
Enter passphrase (empty for no passphrase): 就空着好了。
Enter same passphrase again:
Your identification has been saved in /c/Users/someone/.ssh/id_rsa.
Your public key has been saved in /c/Users/someone/.ssh/id_rsa.pub.
The key fingerprint is:
2d:c1:e5:7b:44:c4:97:d0:6:84 zyDe8ug@gmail.com
The key's randomart image is:
+--[ RSA 4096]----+
| .o+o . |
| . o .o = |
| o .o.= . |
+-----------------+
someone@localhost /E/git/de8ug.github.io (master)
$ ssh-agent -s
SSH_AUTH_SOCK=/tmp/ssh-HNgl8DCVxExM/agent.10148; export SSH_AUTH_SOCK;
SSH_AGENT_PID=11052; export SSH_AGENT_PID;
echo Agent pid 11052;
someone@localhost /E/git/de8ug.github.io (master)
$ ssh-add /c/Users/someone/.ssh/id_rsa # 这里不成功
Could not open a connection to your authentication agent.
someone@localhost /E/git/de8ug.github.io (master)
$ ssh-add ~/.ssh/id_rsa
Could not open a connection to your authentication agent.
someone@localhost /E/git/de8ug.github.io (master)
$ clip < ~/.ssh/id_rsa.pub
someone@localhost /E/git/de8ug.github.io (master)
$ ssh -T git@github.com
Hi de8ug! You've successfully authenticated, but GitHub does not provide she
ll access.