git ssh认证
2013-10-01 03:31 youxin 阅读(4138) 评论(0) 编辑 收藏 举报一般新手用git时,使用HTTPS都需要输入用户名和密码,这是一个很低效的开发过程。(虽然有时可以让开发人员减少push的次数)。github提供了几种连接方式,其中以https:开头的代表https连接,以git开头代表ssh连接。所以用ssh连接时要确保
你客户端的版本库url设置的ssh的url,而不是https的url。如何查看客户端的连接设置,使用下面的命令:
$ git config --list
显示中有一个
remote.origin.url=xxxxxx
如果url不是git开头的,去项目网址复制下ssh地址,然后设置url为新的地址
$git config remote.origin.url 新地址
。
生成ssh的步骤官网有详细说明:https://help.github.com/articles/generating-ssh-keys
大概如下:
1.转到目录(如果没有.ssh,就创建一个,不能用普通创建文件夹方式创建以.开头的,用命令行)
$ cd ~/.ssh 这个在win7上无效,~代表用户目录,win7一般为:C:\Users\Administrator\.ssh (管理员身份登录) )
2. 生成key:
ssh-keygen -t rsa -C "your_email@example.com" # Creates a new ssh key, using the provided email as a label # Generating public/private rsa key pair. # Enter file in which to save the key (/c/Users/you/.ssh/id_rsa): [Press enter] ssh-add id_rsa (输入文件的名字,一般输入id_rsa就可以了)
然后会提示输入2次密码,(这里我们直接输入回车,不然以后每次都要输入密码,麻烦)输入完成后就在.ssh文件夹下面生成了2个文件:id_rsa和id_rsa.pub
把id_ras.pub内容复制下。
3.去Account Settings 新增一个key,key名字随意,Key内容就粘贴下刚才复制的就可以了。
4.测试 ssh -vT git@github.com (输入这个,千万注意,邮箱不要改)
# Attempts to ssh to github 如果 不是默认端口,可以加上 ssh -vT -p 37600 xx.com -T 意思是说禁止分配伪终端。当用ssh或telnet等登录系统时,系统分配给我们的终端就是伪终端。 如果ssh使用此选项登录系统时,由于禁用,将无法获得终端;但仍能够获得shell,只不过看起来像在本地,也没有很多应有的环境变量,例如命令提示符,PS1等。 当使用命令ps -ef|grep [b]ash时看到root 22082 22080 0 11:51 ? 00:00:00 -bash显示终端那里是一个问号。 -T 不显示终端,只显示连接成功信息 http://blog.chinaunix.net/uid-687654-id-2075867.html
You may see this warning:
# The authenticity of host 'github.com (207.97.227.239)' can't 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)?
Don't worry, this is supposed to happen. Verify that the fingerprint matches the one here and type "yes".
# Hi username! You've successfully authenticated, but GitHub does not # provide shell access.
If that username is correct, you've successfully set up your SSH key. Don't worry about the shell access thing, you don't want that anyway.
遇到了几个错误:
permission denied,一般都会遇到这种错误,看官网说明:
https://help.github.com/articles/error-permission-denied-publickey
错误:Could not open a connection to your authentication agent.
解决方法】需要ssh-agent启动bash,或者说把bash挂到ssh-agent下面。
【具体方法】
islue@localhost $ ssh-agent bash --login -iislue@localhost $ ssh-add
(如果上面还是报错:
Could not open a connection to your authentication agent.则
http://funkaoshi.com/blog/could-not-open-a-connection-to-your-authentication-agent
SSH private-keys are usually stored encrypted on the computers they are stored on. A pass-phrase is used to decrypt them when they are to be used. Since most people use SSH public-private key-pairs to get around typing in passwords all the time, the ssh-agentdaemon exists to store decrypted private-keys you plan on using in a given session. The thing most people get tripped up on when using ssh-agent is that what the program outputs, some borne or csh shell commands, needs to be run. It may look like ssh-agent has set some variables for you, but it has in fact done no such thing. If you call ssh-add without processing ssh-agent’s output, it will complain it is unable to open a connection to your authentication agent. The most straightforward way to run ssh-agent on the command line is as follows: eval `ssh-agent`. After doing this, calls to ssh-add should succeed without error.
执行ssh-add ~/.ssh/rsa
报标题上的错误
先执行 eval `ssh-agent` (是~键上的那个`) 再执行 ssh-add ~/.ssh/rsa成功
ssh-add -l 就有新加的rsa了
ssh-add出现Error connecting to agent: No such file or directory的解决方法
Windows环境下执行
ssh-add ~/.ssh/id_rsa
报错:
Error connecting to agent: No such file or directory
解决方法:【以管理员身份运行】在 PowerShell 执行
Set-Service ssh-agent -StartupType Manual
Start-Service ssh-agent
)
【ssh-agent介绍】
ssh-agent就是一个管理私钥的代理,受管理的私钥通过ssh-add来添加,所以ssh-agent的客户端都可以共享使用这些私钥。
好处1:不用重复输入密码。
用 ssh-add 添加私钥时,如果私钥有密码的话,照例会被要求输入一次密码,在这之后ssh-agent可直接使用该私钥,无需再次密码认证。
好处2:不用到处部署私钥
假设私钥分别可以登录同一内网的主机 A 和主机 B,出于一些原因,不能直接登录 B。可以通过在 A 上部署私钥或者设置 PortForwarding 登录 B,也可以转发认证代理连接在 A 上面使用ssh-agent私钥登录 B。
islue@localhost $ ssh -A HOST_Aislue@HOST_A $ ssh HOST_B
islue@HOST_B $
ssh-add完后,可以用ssh-add -l来查看结果:
客户端第一次push会在.ssh生成一个known_hosts文件:
这样,以后就不用输入用户名和密码了。
如果出现:
git clone git@x.x.x.x:test.git
Permission denied (publickey,gssapi-with-mic).
fatal: The remote end hung up unexpectedly.
原因是没有起到ssh。
运行:
ssh-agent bash .
或者不从cmd运行,直接从git bash运行。
Git有一个工具被称为git config,它允许你获得和设置配置变量;这些变量可以控制Git的外观和操作的各个方面。这些变量可以被存储在三个不同的位置:
1./etc/gitconfig 文件:包含了适用于系统所有用户和所有库的值。如果你传递参数选项’--system’ 给 git config,它将明确的读和写这个文件。
2.~/.gitconfig 文件 :具体到你的用户。你可以通过传递--global 选项使Git 读或写这个特定的文件。
3.位于git目录的config文件 (也就是 .git/config) :无论你当前在用的库是什么,特定指向该单一的库。每个级别重写前一个级别的值。因此,在.git/config中的值覆盖了在/etc/gitconfig中的同一个值。
在Windows系统中,Git在$HOME目录中查找.gitconfig文件(对大多数人来说,位于C:\Documents and Settings\$USER下)。它也会查找/etc/gitconfig,尽管它是相对于Msys 根目录的。这可能是你在Windows中运行安装程序时决定安装Git的任何地方。
4、git config -l查看所有的配置信息,依次是系统级别、用户级别、仓库级别
Could not open a connection to your authentication agent.
git 协议的 url 无法clone
当使用git clone命令来下载git协议的url时,在我本机会出现如下的错误:
$ git clone git://github.com/schacon/grit.git Cloning into 'grit'... fatal: unable to connect to github.com: github.com[0: 192.30.252.131]: errno=No error
答案见这里:http://stackoverflow.com/questions/16298986/unable-to-connect-to-github-com-for-cloning
这是由于防火墙导致的,Git协议要求防火墙开放 9418 端口,而企业级防火墙一般不允许对这个非标准端口的访问。大型企业级防火墙通常会封锁这个少见的端口 (参见ProGit-Chapter4-Section4.1)。
将git 协议换为 https,就可以了。
$ git clone https://github.com/schacon/grit.git
也可以参考 http://blog.csdn.net/greenqingqingws/article/details/11808745 中的做法,修改.gitconfig设置:
git config --global url."https://".insteadOf "git://"
在.gitconfig文件中添加了
[url "https://"] insteadOf = git://
https://www.cnblogs.com/zhcncn/p/3681209.html
git clone 提示输入git@xxx的密码
如果你输入gitlab 用户的密码,总是提示不对。原因一般是私钥认证失败。 解决办法:
提示:git每次pull提示需要密码
错误提示
Administrator@DEEP MINGW64 /f/code/xxx-service (master) $ git pull git@gitlab.xxx.com's password:
原因
只提示输入密码的情况,一般是秘钥验证失败。二种情况
1、公钥验证错误
解决方法如下,以github为例
在https://github.com/settings/keys 界面,将生成的公钥添加到SSH keys项中
如没有则使用ssh-keygen命令生成
ssh-keygen -t rsa -C "Your Email Address" -f "~/.ssh/gitlab_id_rsa"
2、私钥验证错误
git拉取代码时没有配置私钥认证
使用以下命令添加认证到ssh
ssh-add ~/.ssh/gitlab_id_rsa
使用以上命令,只能对当前环境生效,重启或新开终端会继续提示输入密码
永久解决:在~/.ssh/config
文件中(没有则新建),指定对应git服务使用的认证秘钥文件即可
# GitLab.com 密钥 Host github.com Preferredauthentications publickey IdentityFile ~/.ssh/github_id_rsa # GitLab.com 密钥 Host gitlab.xxx.com Preferredauthentications publickey IdentityFile ~/.ssh/gitlab_id_rsa
总结
- 直接提示输入密码的,为秘钥认证失败,使用以上方法即可解决
- 提示输入账号和密码的,为账号认证失败,使用
git config --global credential.helper store
命令,可输入一次密码后永久存储
https://blog.csdn.net/weixin_41302755/article/details/122995696
git配置多个SSH Key
当有多个git账号时,比如:
- 一个github,国际认可的仓库
- 一个gitee码云,国内仓库,速度快
这两者如果邮箱不同的话,在生成第二个key的时候会覆盖第一个的key,导致一个用不了。
解决办法就是:
生成两个(或多个)不同的公私密钥对,用config文件管理它们。
1 步骤
我们假设原来在
~/.ssh
目录下已经生成了一个密钥对:id_rsa id_rsa.pub1.1 生成第二个key
接下来我们生成第二个ssh key:
ssh-keygen -t rsa -C "yourmail@gmail.com"这里不要一路回车,我们自己手动填写保存路径:
Generating public/private rsa key pair.
Enter file in which to save the key (/c/Users/Gary/.ssh/id_rsa): /c/Users/Gary/.ssh/id_rsa_github
<剩下两个直接回车>这里我们用
id_rsa_github
来区别原有密钥对,避免被覆盖。完成之后,我们可以看到
~/.ssh
目录下多了两个文件,变成:id_rsa
id_ras.pub
id_rsa_github
id_rsa_github.pub
known_hosts
1.2 打开ssh-agent
这里如果你用的github官方的bash,用:
ssh-agent -s如果是其他的,比如msysgit,用:
eval $(ssh-agent -s)略过这一步的话,下一步会提示这样的错误:Could not open a connection to your authentication agent.
1.3 添加私钥
ssh-add ~/.ssh/id_rsa ssh-add ~/.ssh/id_rsa_github如果提示文件或目录不存在,就使用绝对地址。
1.4 创建config文件
在
~/.ssh
目录下创建名为config
的文件。添加一下内容:
# gitee and github # 注意:多个Host公用一个公钥时, # 对应的HostName和Port必须跟在Host后面 Host gitee.com HostName gitee.com Host github.com HostName github.com PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa # gerrit Host gerrit.awaimai.com HostName gerrit.awaimai.com Port 8000 PreferredAuthentications publickey IdentityFile ~/.ssh/id_rsa_gerrit# 其他的统一用id_rsa文件登陆,或者密码。 # 注意:这里的第二行后面有个password,就是同时允许密码登陆 # 不然第一次没加公钥,密码也登陆不了,会提示错误:Permission denied (publickey,password). Host * PreferredAuthentications publickey,password IdentityFile ~/.ssh/id_rsa其中,
Host
和HostName
填写git服务器的域名。
IdentityFile
指定私钥的路径。如果在Linux系统下提示错误:
Bad owner or permissions on /home/gary/.ssh/config
说明config权限过大,
chmod
命令调整:$ chmod 644 ~/.ssh/config然后在github和gitee码云上添加公钥即可,这里不再多说。
1.5 测试
然后用ssh命令分别测试:
ssh -T git@github.com2 调试
如果到这里你没有成功的话,别急,教你解决问题的终极办法
--debug
比如测试github:
ssh -vT git@github.com-v 是输出编译信息,然后根据编译信息自己去解决问题吧。
就我自己来说一般是config里的host那块写错了。
3 关于用户名
如果之前有设置全局用户名和邮箱的话,需要
unset
一下git config --global --unset user.name git config --global --unset user.email然后在不同的仓库下设置局部的用户名和邮箱
比如在公司的repository下git config user.name "yourname" git config user.email "youremail"在自己的github的仓库在执行刚刚的命令一遍即可。
这样就可以在不同的仓库,已不同的账号登录。
git多账号配置多账号配置
一、如何生成ssh密钥
1.1 设置Git的user.name和user.email
为了生成多账户,这里展示的局部的git配置,并不是全局的账户配置(全局配置:在config后面加上–list)。
//github账号 $ git config user.name "zhangsan" $ git config user.email "zhangsan@gmail.com" //gitlab账号 $ git config user.name "zhangsan" $ git config user.email "zhangsan@qq.com"
1.2 生成SSH密钥
//github账号 $ ssh-keygen -t rsa -C “zhangsan@gmail.com” //gitlab账号 $ ssh-keygen -t rsa -C “zhangsan@qq.com”
在~/.ssh目录下,增加config配置文件(注:无后缀名),配置规范如下(可配置多个git账号):
#Host host(Host简称,使用命令ssh host可连接远程服务器,如:ssh github) #User/Email 登录用户名(如:zhangsan/zhangsan@gmail.com) #HostName 主机名用ip或域名,建议使用域名(如:github.com) #Port 服务器open-ssh端口(默认:22,默认时一般不写此行 #IdentityFile 证书文件路径(如~/.ssh/id_rsa_*)
注意事项:
- 在配置文件中的,IdentityFile文件位置是rsa私钥,不是.pub公钥
- push代码的时候注意下,得check下本的user.name和user.email,若没有进行生成操作。建议最好设置一个全局的user.name和user.email,然后需要特定的配置的git仓库,就单独配置(当前配置查询命令:$ git config –list)
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 基于Microsoft.Extensions.AI核心库实现RAG应用
· Linux系列:如何用heaptrack跟踪.NET程序的非托管内存泄露
· 开发者必知的日志记录最佳实践
· SQL Server 2025 AI相关能力初探
· Linux系列:如何用 C#调用 C方法造成内存泄露
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 别再用vector<bool>了!Google高级工程师:这可能是STL最大的设计失误
· 单元测试从入门到精通