git 指定要提交的ssh key
问题描述
ssh
具有-i
选项,用于告知在验证时使用哪个私钥文件:
-i identity_file
- Selects a file from which the identity (private key) for RSA or DSA authentication is read. The default is
~/.ssh/identity
for protocol version 1, and~/.ssh/id_rsa
and~/.ssh/id_dsa
for protocol version 2. Identity files may also be specified on a per-host basis in the configuration file. It is possible to have multiple-i
options (and multiple identities specified in configuration files).
有没有类似的方法告诉git
哪个私钥文件在~/.ssh
目录中有多个私钥的系统上使用?
最佳解决方案
在~/.ssh/config
中,添加:
- host github.com
- HostName github.com
- IdentityFile ~/.ssh/id_rsa_github
- User git
现在你可以做git clone git@github.com:username/repo.git
。
注意:验证IdentityFile的权限是否为400.SSH将以不清楚的方式拒绝太可读的SSH密钥。它只会看起来像一个凭证拒绝。在这种情况下,解决方案是:
chmod 400 ~/.ssh/id_rsa_github
次佳解决方案
环境变量GIT_SSH_COMMAND
:
从Git版本2.3.0可以使用环境变量GIT_SSH_COMMAND
,如下所示:
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example" git clone example
请注意,-i
有时可以被您的配置文件覆盖,在这种情况下,您应该给SSH一个空配置文件,如下所示:
GIT_SSH_COMMAND="ssh -i ~/.ssh/id_rsa_example -F /dev/null" git clone example
配置core.sshCommand
:
从Git版本2.10.0,您可以配置每个repo或全局,所以您不必再设置环境变量!
- git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
- git pull
- git push
第三种解决方案
没有直接的方法告诉git
哪个私钥要使用,因为它依赖于ssh
进行存储库认证。但是,仍有几种方法可以实现您的目标:
选项1:ssh-agent
您可以使用ssh-agent
临时授权您的私钥。
例如:
$ ssh-agent sh -c 'ssh-add ~/.ssh/id_rsa; git fetch user@host'
选项2:GIT_SSH_COMMAND
使用GIT_SSH_COMMAND
环境变量(Git 2.3.0+)传递ssh参数。
例如:
- $ GIT_SSH_COMMAND='ssh -i ~/.ssh/id_rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no'
- git clone user@host
您可以在一行中输入所有内容,省略。
选项3:GIT_SSH
使用GIT_SSH
环境变量传递ssh参数。
例如:
- $ echo 'ssh -i ~/.ssh/id_rsa -o UserKnownHostsFile=/dev/null -o StrictHostKeyChecking=no $*' > ssh
- $ chmod +x ssh
- $ GIT_TRACE=1 GIT_SSH='./ssh' git clone user@host
注意:上面的行是你应该粘贴到你的终端的shell(终端)命令行。他们将创建一个名为ssh
的文件,使其可执行,并(间接)执行它。
选项4:~/.ssh/config
使用其他答案中建议的~/.ssh/config
文件,以指定您的私钥的位置。
第四种方案
编写一个使用所需参数调用ssh
的脚本,并将脚本的文件名放在$GIT_SSH
中。或者将您的配置放在~/.ssh/config
中。
第五种方案
在与$GIT_SSH
斗争之后,我想分享一下对我有用的东西。
通过我的例子,我会假设你的私钥位于/home/user/.ssh/jenkins
错误避免:GIT_SSH值包括选项
$ export GIT_SSH="ssh -i /home/user/.ssh/jenkins"
或者任何类似的将失败,因为git将尝试将该值作为文件执行。因此,您必须创建一个脚本。
$ GIT_SSH脚本/home/user/gssh.sh
的工作示例
脚本将被调用如下:
$ $GIT_SSH [username@]host [-p <port>] <command>
工作示例脚本可能如下所示:
- #!/bin/sh
- ssh -i /home/user/.ssh/jenkins $*
注意$*
到底是它的重要组成部分。
甚至更安全的选择,这将防止任何与您的默认配置文件中的任何可能的冲突(加上明确提及要使用的端口)将是:
- #!/bin/sh
- ssh -i /home/user/.ssh/jenkins -F /dev/null -p 22 $*
假设脚本在/home/user/gssh.sh
中,那么你将:
$ export GIT_SSH=/home/user/gssh.sh
所有人都应该工作。
第六种方案
如果您不想在每次运行git时指定环境变量,则不要再使用另一个包装器脚本,不要/不能运行ssh-agent(1),也不想为此下载另一个包,请使用git-remote-ext(1 )外部运输:
- $ git clone 'ext::ssh -i $HOME/.ssh/alternate_id git.example.com %S /path/to/repository.git'
- Cloning into 'repository'
- (...)
- $ cd repository
- $ git remote -v
- origin ext::ssh -i $HOME/.ssh/alternate_id git.example.com %S /path/to/repository.git (fetch)
- origin ext::ssh -i $HOME/.ssh/alternate_id git.example.com %S /path/to/repository.git (push)
我认为这个解决方案是优越的,因为:
它是存储库/远程特定的
避免包装脚本膨胀
不需要SSH代理 – 如果您想要无人值守的克隆/推/拉(例如在cron)
当然,没有外部工具需要
第七种方案
您可以使用ssh-ident而不是创建自己的包装器。
您可以阅读更多:https://github.com/ccontavalli/ssh-ident
它首次需要加载ssh密钥,一次,即使是多个登录会话,xterms或NFS共享的家庭。
使用一个微小的配置文件,它可以自动加载不同的密钥,并根据您需要做的事情将它们分隔在不同的代理(代理转发)中。
第八种方案
在~/.ssh/config
中使用自定义主机配置,如下所示:
- Host gitlab-as-thuc
- HostName git.thuc.com
- User git
- IdentityFile ~/.ssh/id_rsa.thuc
- IdentitiesOnly yes
然后使用您的自定义主机名:
git remote add thuc git@gitlab-as-thuc:your-repo.git
欲了解更多详情,请阅读:http://itblog.study.land/how-to-specify-different-ssh-keys-for-git-push-for-a-given-domain/
参考文献
git 指定要提交的ssh key的更多相关文章
- git的安装以及生成ssh key
安装git 在ubuntu系统下输入以下命令安装git软件: sudo apt-get install git 输入以下命令查看git是否安装成功: git --version 如下图所示则表示安装成 ...
- 管理git生成的多个ssh key
http://www.bootcss.com/p/git-guide/ 问题阐述 当有多个git账号的时候,比如一个github,用于自己进行一些开发活动,再来一个gitlab,一般是公司内部的git ...
- Git Bash for Windows add ssh key时报Could not open a connection to your authentication agent.
$ ssh-add id_rsa_bitbucketCould not open a connection to your authentication agent. 运行: $ ssh-agent ...
- git生成ssh key步骤并添加到github网站
0: 查看是否已经有了ssh密钥 执行命令:cd ~/.ssh 如果没有密钥则不会有此文件夹,有则备份删除 1:使用 Git Bash生成新的ssh key ssh-keygen -t rsa -C ...
- git 创建SSH key
可以自己搭建一台运行Git的服务器,不过现阶段,为了学Git先搭个服务器绝对是小题大作.好在这个世界上有个叫GitHub的神奇的网站,从名字就可以看出,这个网站就是提供Git仓库托管服务的,所以,只要 ...
- git生成公钥public key并添加SSH key。git乌龟gerrit下推送git【server sent :publickey】
一.key 码云链接:http://git.mydoc.io/?t=180845#text_180845 博客链接: 方式一:https://blog.csdn.net/xb12369/article ...
- Mac下配置多个SSH KEY访问远程Git服务
第一步 生成对应的ssh key 1 后面输入你的用户名 或者 邮箱 2 输入一个独立的ssh key名字 区别之前的名字 第二步 编辑 config文件 在.ssh/目录下面 在config文件配 ...
- Git安装及SSH Key管理之Windows篇
一.安装环境 1.本机系统:Windows 10 Pro(64位)2.Git版本:Git-2.11.0-64-bit.exe(64位) 二.Git安装 去官网下载完后一路下一步完成安装,如下图: ...
- Git安装以及配置SSH Key——Windows
安装 安装 Git 官网下载一个Windows版本的Git. 然后一直下一步即可,如下图 环境变量自动配好的,可以去检查一下环境变量中PATH中有没有Git的环境变量 然后在桌面右击鼠标,选择Git ...
随机推荐
- LightOj 1030 - Discovering Gold(dp+数学期望)
题目链接:http://lightoj.com/volume_showproblem.php?problem=1030 题意:在一个1*n 的格子里,每个格子都有相应的金币数,走到相应格子的话,就会得 ...
- grunt学习三-bower(二)
一.通过bower help 来展开bower的命令 Usage: bower <command> [<args>] [<options>] Commands: c ...
- 简单利用gulp-babel搭建es6转es5环境
es6是什么?借着这个话题,我想说:身为web前端的工作者连es6没听说,转行吧. demo的代码如下: 源码下载 或者 gitclone地址: git@git.oschina.net:sisheb/ ...
- 廖威雄: 思维导图:利用__attribute__((section()))构建初始化函数表与Linux内核init的实现
版权声明:本文为博主原创文章,未经博主同意不得转载. https://blog.csdn.net/juS3Ve/article/details/79049404 本文具体解说了利用__attribut ...
- Mint linux中调整屏幕亮度的方法
/********************************************************************* * Author : Samson * Date ...
- 006-springboot2.0.4 配置log4j2,以及打印mybatis的sql
一.pom配置 普通项目 <!-- log4j2 --> <dependency> <groupId>org.apache.logging.log4j</gr ...
- java-小技巧-001-Long序列化到前端js不支持
1.引入:jackson-mapper-asl-1.9.2.jar 2.导入: import org.codehaus.jackson.map.annotate.JsonSerialize;impor ...
- vue学习六之vuex
由于状态零散地分布在许多组件和组件之间的交互中,大型应用复杂度也经常逐渐增长.为了解决这个问题,Vue 提供 vuex. 什么是Vuex Vuex 是一个专为 Vue.js 应用程序开发的状态管理模式 ...
- latex 安装和使用
1:下载 texlivewindows 版 http://tug.org/texlive/acquire-netinstall.html 2:双击exe文件进行安装,安装时选择 将路径添加到环境变量 ...
- [py]彻底细究web框架的wsgi+逻辑处理模块
wsgi逻辑结构初探 参考: 这里图很精彩,wsgi写的不错 web框架 = wsgi+逻辑处理app 接收请求,返回对应的内容 python wsgiref实现了wsgi规范. from wsgir ...