git 使用ssh密钥

一、查看仓库支持的传输协议
1.1查看仓库支持的传输协议

使用命令 git remote -v 查看你当前的 remote url
root@zengyue:/home/yuanGit# git remote -v

root@zengyue:/home/yuanGit# git remote -v
origin  https://github.com/seventeen71/yuan (fetch)     #1
origin  https://github.com/seventeen71/yuan (push)     #2
origin  [email protected]:seventeen71/yuan.git (push)     #3

#1、#2处的地址都是以https开头的,表示https传输,意味着你每次都要输入讨厌的username、password
作者本人的已经设置好了ssh传输,在#3处:地址是git开头则表示是git协议

提示:完全设置好push模式的话应该是这样

root@zengyue:/home/yuanGit# git remote -v
origin  https://github.com/seventeen71/yuan (fetch)
origin  [email protected]:seventeen71/yuan.git (push) #4

#4处这样当你git push origin master时就不需要输入讨厌的密码了,下面讲如何去设置

1.2检查本机是否已有SSH密钥

在 ~/.ssh目录下查看是否存在如下两个文件:~宿主目录一般为 /home/user(作者是zengyue) /home/zengyue

root@zengyue:~/.ssh# ls
id_rsa  id_rsa.pub

如果不存在id_rsa id_rsa.pub 两个文件的话:

二、生成SSH密钥
2.1 ~/.ssh目录无密钥对从这儿开始

2.1.1 使用命令: ssh-keygen 来创建
root@zengyue:~# ssh-keygen -t rsa -C "[email protected]" 邮件地址为你注册github的地址

Creates a new ssh key using the provided email # Generating public/private rsa key pair.

Enter file in which to save the key (/home/zengyue/.ssh/id_rsa):

这儿让你确认密钥目录,直接Enter回车就好:
Enter same passphrase again: [Type passphrase again] 接着让你输入及确认密码,然后就生成了密钥对。
出现下面提示说明OK了
Your public key has been saved in /home/zengyue/.ssh/id_rsa.pub.
The key fingerprint is: # ............ [email protected]
现在你可以在你的 ~/.ssh 目录看是否有id_rsa id_rsa.pub 两个文件

2.2 github仓库中添加公钥

2.2.1 查看复制本地公钥:cat id_rsa.pub 或者 vim id_rsa.pub 打开直接copy
记得一点都不能省略从 ssh-rsa 开始 [email protected]结束

root@zengyue:~/.ssh# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCbCLmkVonQAFjAgQVY7vH86NIsgw1kDV3dDYRXdmWoYSZ3gY4OEgzCqm8GWXRr+wNFUsSwhMmUabtEfRGGKlP/z2TBMzXFkcJ18zLAWXtgWWUH73tVfxXg+sdFpKhY0ppDYdU9Amk8ljVsge8+pKHVluk0ReBNtFsbp1w/seyrfUqsjigsnQHWeSPBns3CD7hs++7BXW5Mv4SH+ap30N+2tFBlqSuzitwLJCFed9isSFq2UQ4wiX25rs+eYpSloMY9kClMVVYuXN860EKCyF5V1ThS/TOcMoxDhGclDBoItS/oeTDGwopSTrsm5CFyUJFAd3qDYJ+yVy5fycVYfb6x [email protected]

2.2.2 登陆你的github帐户。点击你的头像,然后
Settings -> 左栏点击 SSH and GPG keys -> 点击 New SSH key

2.2.3 将复制的公钥粘贴到key文本域,title随便起名,再点击Add key

2.2.4 然后回到linux下确认:命令 ssh -T [email protected]
root@zengyue:~/.ssh# ssh -T [email protected]
Hi xxx! You've successfully authenticated, but GitHub does not # provide shell access. 看到这个你就成功了。

2.2.5 调整remote url
命令 git remote -v 查看,详细见文章开头

root@zengyue:/home/yuanGit# git remote -v
origin  https://github.com/seventeen71/yuan (fetch)     
origin  https://github.com/seventeen71/yuan (push)     

如果你的是这样,意思就是现在只有https传输,还需要输入username /password
输入命令:
git remote set-url origin [email protected]:[你的账户]/[你的github仓库].git
如下图进入你的仓库,点击1处,然后出现在2处的就是git协议地址了,
复制它拼接到命令git remote set-url origin 后面
git remote set-url origin [email protected]:someone/repository.git

三、二的最后几个部分这么麻烦

你还可以去仓库的 .git 目录下修改conf文件
前面我们说过了,每一次提交git都会最后来这个文件找各种信息,下图的 13 到15行就是,去你的github仓库复制过来配置到这儿

 11 
 12 [remote "origin"]
 13     url = [email protected]:s--enten--/--an.git
 14     fetch = +refs/heads/*:refs/remotes/origin/*
 15     pushurl = [email protected]:s--enten--/--an.git.git
 16 [branch "master"]
 17     remote = origin
 18     merge = refs/heads/master                                  

当然你还可以使用git remote --help

       git remote set-url [--push] <name> <newurl> [<oldurl>]
       git remote set-url --add [--push] <name> <newurl>
       git remote set-url --delete [--push] <name> <url>

git remote set-url --push origin <你的github仓库url>

这样去设置解决也是可以哒!

当然如果你遇到如下问题:去我下篇博客找吧!
执行git clone [email protected]:accountName/repository.git命令时不出错,

运行git push时出错,提示如下

Permission denied(publickey).
fatal: Could not read from remote repository.
Please make sure you have the correct access rights and the repository exists.

猜你喜欢

转载自www.cnblogs.com/shiqi17/p/9416014.html