Using the SSH protocal, you can connect and authenticate to remote servers and services. With SSH keys, you can connect to Github without supplying your username or password at each visit.
检查是否存在公钥(public keys)
- Open Terminal
ls -al ~/.ssh
to see if existing SSH keys are present- Check the directory listing to see if you already have a public SSH key
if you donot have an existing public and private key pair, or donot wish to use any that are available to connect to Github, then generate a new SSH key.
If you donot want to reenter your passphrase every time you use your SSH key,you can add your key to the SSH agent
,
which manages your SSH keys and remembers your passphrase.
- Open Terminal.
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"
This creates a new ssh key,using the provided email as a label.- When you are 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.
解决每次push代码时都要输入Enter passphrase for key '/Users/jing_zhang/.ssh/id_rsa':
密码的问题。
When adding your SSH key to the agent, use the default macOS ssh-add
command.
- Start the ssh-agent in the background
eval "$(ssh-agent -s)"
- If you are using macOS Sierra 10.12.2 or later, you will need to modify your
~/.ssh/config
(if donot have config file, just create) file to automatically load keys into the ssh-agent and store passphrase in your keychain.
Host *
AddkeysToAgent yes
UseKeychain yes
IdentityFile ~/.ssh/id_rsa
ssh-add -K ~/.ssh/id_rsa
Add your SSH private key to the ssh-agent and store your passphrase in the keychain.
ssh -T git@github.com
- Verify that the fingerprint in the message you see matches one of the message in step 1,then type
yes
:
> Hi username! You have successfully authenticated, but Github does not provide shell access
You can secure your SSH keys and configure an authentication agent so that you won't have to reenter your passphrase every time you use your SSH keys.
With SSH keys, if someone gains access to your computer, they also gain access to every system that uses that key.
To add an extra layer of security, you can add a passphrase to your SSH key. You can use ssh-agent
to securely
save your passphrase so you don't have to reenter it.
$ ssh-keygen -p
# Start the SSH key creation process
> Enter file in which the key is (/Users/you/.ssh/id_rsa): [Hit enter]
> Key has comment '/Users/you/.ssh/id_rsa'
> Enter new passphrase (empty for no passphrase): [Type new passphrase]
> Enter same passphrase again: [One more time for luck]
> Your identification has been saved with the new passphrase.
If your key already has a passphrase, you will be prompted to enter it before you can change to a new passphrase.
The first time you use your key, you will be prompted to enter your passphrase. If you choose to save the passphrase with your keychain, you won't have to enter it again. Otherwise, you can store your passphrase in the keychain when you add your key to the ssh-agent.