首页 > 代码库 > SSH配置

SSH配置

  SSH是Secure shell的缩写,即“安全外壳协议”,专为远程登录会话和其他网络服务提供安全性的协议,是一项计算机上的安全协议。SSH传输的数据可以是经过压缩的,所以可以加快传输的速度,至少比HTTP要快多了。所以在git中,我们最好使用SSH协议,ssh://git@gitlab.17zuoye.net: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.pubid_ecdsa.pubid_ed25519.pubid_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 "your_email@example.com"

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 git@github.com  #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 notprovide 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/

SSH配置