git 配置SSH协议

Posted

tags:

篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了git 配置SSH协议相关的知识,希望对你有一定的参考价值。

参考技术A 本文章总共讲述以下几点内容:

执行过程一直 enter 即可

假设已经在远程仓库建立了一个仓库,需要将本地的仓库推送上去

到这里我们就可以推送我的本地仓库的内容到github上托管起来了,注意第一次建立连接的时候,需要进行确定,直接 yes 就好,并且会将配置的相关信息写入known_hosts,后续再进行推送,就不需要再次确认了,SSH链接已经配置了好了。

SSH配置

  SSH是Secure shell的缩写,即“安全外壳协议”,专为远程登录会话和其他网络服务提供安全性的协议,是一项计算机上的安全协议。SSH传输的数据可以是经过压缩的,所以可以加快传输的速度,至少比HTTP要快多了。所以在git中,我们最好使用SSH协议,ssh://[email protected]:10022/17zuoye/frontend_nodejs.git 而非 http://gitlab.17zuoye.net:10080/17zuoye/frontend_nodejs.git
在本地创建一对密钥(公有密钥:id_rsa.pub、私有密钥:id_rsa),把共有密钥放在需要访问的服务器上,如粘贴id_rsa.pub的内容至你的github账户中的SSH Keys中,这样就建立了本地、远程认证关系。当向服务器发送请求时,如git push origin master,推送主分支到远程仓库。会通过你发送过来的共有密钥和服务器上的共有密钥进行比较,如果两个密钥一致则服务器验证通过。这样就避免了中间人攻击。
  检查本地是否已经生成了SSH Keys,打开Git Bash ,切换到.ssh目录。 如果存在id_rsa.pub or id_dsa.pub两个文件则表示之前已经创建了。如果没有就要创建,如果存在就要判断是否生效。新建SSH选用默认名称即可(连续3个回车)
 
An SSH key allows you to establish a secure connection between your computer and GitHub.

Checking for existing SSH keys

1、Open Terminal.

2、Enter ls -al ~/.ssh to see if existing SSH keys are present:

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

cat ~/.ssh/id_rsa.pub
# a long string starting with ssh-rsa

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

If you see an existing public and private key pair listed (for example id_rsa.pub and id_rsa) that you would like to use to connect to GitHub

Generating a new SSH key

1、Open Terminal.

2、Paste the text below, substituting in your GitHub email address.

ssh-keygen -t rsa -b 4096 -C "[email protected]"

This creates a new ssh key, using the provided email as a label. Generating public/private rsa key pair.

When you‘re prompted to "Enter a file in which to save the key," press Enter. This accepts the default file location. just press enter to use the default

Enter a file in which to save the key (/Users/you/.ssh/id_rsa): [Press enter]

Enter passphrase (empty for no passphrase): [Type a passphrase]

Enter same passphrase again: [Type passphrase again]

Adding a new SSH key to your GitHub account

To copy your public key to the clipboard, use the code below. Depending on your OS you‘ll need to use a different command:

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

Click New SSH key or Add SSH key.then Click Add SSH key.

Testing your SSH connection

1、Open Terminal.

2、Enter the following:

ssh -T [email protected]  #Attempts to ssh to GitHub

You may see one of these warnings:

The authenticity of host github.com (192.30.252.1) cant 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)?


The authenticity of host github.com (192.30.252.1) cant be established.
RSA key fingerprint is nThbg6kXUpJWGl7E1IGOCspRomTxdCARLviKw6E5SY8.
Are you sure you want to continue connecting (yes/no)?
Note: The example above lists the GitHub IP address as 192.30.252.1. When pinging GitHub, you may see a range of IP addresses. 
  1. Verify that the fingerprint in the message you see matches the following message, then type yes:

    Hi username! Youve successfully authenticated, but GitHub does not
    provide shell access.
  2. If you‘re switching from HTTPS to SSH, you‘ll need to update your remote repository URLs.

官方文档

https://help.github.com/articles/generating-an-ssh-key/

以上是关于git 配置SSH协议的主要内容,如果未能解决你的问题,请参考以下文章

Git使用SSH协议通讯的常见配置

如何在Visual Studio中配置git

Git 常用配置

GitLab配置ssh key

GitLab配置ssh key

git http ssh哪个好