如何使用Git生成并配置SSH密钥

前提


安装git到本地,能在cmd中使用git命令行

步骤


1.设置全局用户名和邮箱

git config –global user.name “kakadanica”

git config –global user.email “kakadanica.gmail.com”

2.生成SSH密钥

ssh-keygen -t rsa -C “kakadnaica@gmail.com”

如果你之前有生成过,控制台会提示你already exist,并给你打印出密钥的地址,建议删除后重新生成。如果你之前未生成过,直接敲三个回车,密码为空,将会提示你生成成功并显示

例如:


Your identification has been saved in /c/Users/E450/.ssh/id_rsa.
Your public key has been saved in /c/Users/E450/.ssh/id_rsa.pub.

The key fingerprint is:


3.添加密钥到github上

找到id_rsa.pub文件,使用记事本打开,复制内容。

登录GitHub,到https://github.com/settings/keys该页面中,新建new SSH Key。

输入任意Title,将刚刚复制的内容粘贴到key中,保存提交。

结果


在cmd中执行ssh git@github.com命令操作



Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added ‘github.com,192.30.255.112’ (RSA) to the list of known hosts.
PTY allocation request failed on channel 0
Hi kakadanica! You’ve successfully authenticated, but GitHub does not provide shell access.
Connection to github.com closed.


如上,表示设置成功!