Git is a distributed code management tool. When working with a remote Git repository over SSH, you need to configure SSH locally first. For GitHub, the setup usually involves configuring your Git identity, generating an SSH key pair, adding the key to the SSH agent, and then registering the public key on GitHub.
Set the Git user name and email
First, configure the global Git user name and email address. These values will be used by Git when creating commits:
$ git config --global user.name "xuhaiyan"
$ git config --global user.email "[email protected]"
Generate an SSH key
Before creating a new key, check whether an SSH key already exists:
cd \~/.ssh
If this directory does not exist, there is no existing SSH key. If it does exist, back up the old key files first and remove them if necessary.
Next, generate a new RSA key:
$ ssh-keygen -t rsa -C “[email protected]”
Press Enter three times during the prompt process. This keeps the password empty.
After the command finishes, you should see output similar to this:
Your identification has been saved in /home/tekkub/.ssh/id_rsa.
Your public key has been saved in /home/tekkub/.ssh/id_rsa.pub.
The key fingerprint is:
………………
At this point, two files are generated:
id_rsaid_rsa.pub
The id_rsa file is the private key and should be kept locally. The id_rsa.pub file is the public key, which is the one that needs to be added to GitHub.
Add the key to SSH
Add the generated key to SSH with:
ssh-add 文件名
If a password was set when the key was generated, enter it when prompted.
Add the public key on GitHub
Open https://github.com/ and add a new SSH key in the account settings. The content to add is the public key from the id_rsa.pub file.
Test the SSH connection
Finally, test whether SSH authentication works:
ssh [email protected]
The first connection may show a host authenticity 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)? yes
Warning: Permanently added ‘github.com,207.97.227.239′ (RSA) to the list of known hosts.
ERROR: Hi tekkub! You’ve successfully authenticated, but GitHub does not provide shell access
Connection to github.com closed.
The message indicates that authentication succeeded. GitHub does not provide shell access, so the connection is closed after confirming that the SSH key works.