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	git@github.com: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	git@github.com: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 "your_email@youremail.com"` 邮件地址为你注册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: # ............ your_email@youremail.com
现在你可以在你的 ~/.ssh 目录看是否有id_rsa id_rsa.pub 两个文件

2.2 github仓库中添加公钥
2.2.1 查看复制本地公钥:cat id_rsa.pub 或者 vim id_rsa.pub 打开直接copy 记得一点都不能省略从 ssh-rsa 开始 ........youre@email.com结束
root@zengyue:~/.ssh# cat id_rsa.pub
ssh-rsa AAAAB3NzaC1yc2EAAAADAQABAAABAQCbCLmkVonQAFjAgQVY7vH86NIsgw1kDV3dDYRXdmWoYSZ3gY4OEgzCqm8GWXRr+wNFUsSwhMmUabtEfRGGKlP/z2TBMzXFkcJ18zLAWXtgWWUH73tVfxXg+sdFpKhY0ppDYdU9Amk8ljVsge8+pKHVluk0ReBNtFsbp1w/seyrfUqsjigsnQHWeSPBns3CD7hs++7BXW5Mv4SH+ap30N+2tFBlqSuzitwLJCFed9isSFq2UQ4wiX25rs+eYpSloMY9kClMVVYuXN860EKCyF5V1ThS/TOcMoxDhGclDBoItS/oeTDGwopSTrsm5CFyUJFAd3qDYJ+yVy5fycVYfb6x youre@email.com

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 git@github.com
root@zengyue:~/.ssh# ssh -T git@github.com
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 git@github.com:[你的账户]/[你的github仓库].git
如下图进入你的仓库,点击1处,然后出现在2处的就是git协议地址了,
复制它拼接到命令git remote set-url origin 后面
git remote set-url origin git@github.com:someone/repository.git

三、二的最后几个部分这么麻烦
你还可以去仓库的 .git 目录下修改conf文件 前面我们说过了,每一次提交git都会最后来这个文件找各种信息,下图的 13 到15行就是,去你的github仓库复制过来配置到这儿
 11 
 12 [remote "origin"]
 13     url = git@github.com:s--enten--/--an.git
 14     fetch = +refs/heads/*:refs/remotes/origin/*
 15     pushurl = git@github.com: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 git@github.com: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.
posted @   G1733  阅读(498)  评论(0编辑  收藏  举报
编辑推荐:
· Linux系列:如何用 C#调用 C方法造成内存泄露
· AI与.NET技术实操系列(二):开始使用ML.NET
· 记一次.NET内存居高不下排查解决与启示
· 探究高空视频全景AR技术的实现原理
· 理解Rust引用及其生命周期标识(上)
阅读排行:
· DeepSeek 开源周回顾「GitHub 热点速览」
· 物流快递公司核心技术能力-地址解析分单基础技术分享
· .NET 10首个预览版发布:重大改进与新特性概览!
· AI与.NET技术实操系列(二):开始使用ML.NET
· 单线程的Redis速度为什么快?
点击右上角即可分享
微信分享提示