Chr*_*fer 963 git ssh bash shell
也许是一个相当不寻常的情况,但我想指定一个私有SSH密钥,以便在从本地计算机执行shell(git)命令时使用.
基本上是这样的:
git clone git@github.com:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser"
Run Code Online (Sandbox Code Playgroud)
甚至更好(在Ruby中):
with_key("/home/christoffer/ssh_keys/theuser") do
sh("git clone git@github.com:TheUser/TheProject.git")
end
Run Code Online (Sandbox Code Playgroud)
我见过使用Net :: SSH连接到远程服务器的示例,它使用指定的私钥,但这是一个本地命令.可能吗?
Hey*_*his 1097
这些解决方案都不适合我.
相反,我详细阐述了@Martinv.Löwis提到config为SSH 设置文件.
SSH将查找用户的~/.ssh/config文件.我的设置如下:
Host gitserv
Hostname remote.server.com
IdentityFile ~/.ssh/id_rsa.github
IdentitiesOnly yes # see NOTES below
Run Code Online (Sandbox Code Playgroud)
我添加了一个远程git存储库:
git remote add origin git@gitserv:myrepo.git
Run Code Online (Sandbox Code Playgroud)
然后git命令对我来说正常工作.
git push -v origin master
Run Code Online (Sandbox Code Playgroud)
笔记
IdentitiesOnly yes需要防止SSH默认行为发送所述身份文件匹配每个协议的默认的文件名的.如果您有一个名为的文件~/.ssh/id_rsa将在~/.ssh/id_rsa.github没有此选项之前尝试.参考
Mar*_*wis 715
这样的东西应该工作(由orip建议):
ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git'
Run Code Online (Sandbox Code Playgroud)
如果您更喜欢子壳,您可以尝试以下(虽然它更脆弱):
ssh-agent $(ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git)
Run Code Online (Sandbox Code Playgroud)
Git将调用SSH,它将通过环境变量找到它的代理; 反过来,这将加载密钥.
另外,设置HOME也可以做的伎俩,只要你愿意建立一个仅包含一个目录.ssh的目录HOME; 这可能包含identity.pub或配置文件设置IdentityFile.
phi*_*reo 396
其他人的建议~/.ssh/config更复杂.它可以很简单:
Host github.com
IdentityFile ~/.ssh/github_rsa
Run Code Online (Sandbox Code Playgroud)
Rob*_*ill 379
从Git 2.3.0开始,我们也有简单的命令(不需要配置文件):
GIT_SSH_COMMAND='ssh -i private_key_file' git clone user@host:repo.git
Run Code Online (Sandbox Code Playgroud)
您可能需要重新启动计算机上的ssh服务.
Joe*_*ock 136
my_git_ssh_wrapper的内容:
#!/bin/bash
ssh -i /path/to/ssh/secret/key $1 $2
Run Code Online (Sandbox Code Playgroud)
然后你可以通过这样做来使用密钥:
GIT_SSH=my_git_ssh_wrapper git clone git@github.com:TheUser/TheProject.git
Run Code Online (Sandbox Code Playgroud)
Dan*_*scu 97
总结答案和评论,设置git以使用不同的密钥文件然后忘记它的最佳方法,它也支持同一主机的不同用户(例如个人GitHub帐户和工作帐户),适用于Windows同样,是编辑~/.ssh/config(或c:\Users\<your user>\.ssh\config)并指定多个身份:
Host github.com
HostName github.com
IdentityFile /path/to/your/personal/github/private/key
User dandv
Host github-work
HostName github.com
IdentityFile /path/to/your/work/github/private/key
User workuser
Run Code Online (Sandbox Code Playgroud)
然后,要将项目克隆为个人用户,只需运行常规git clone命令即可.
要将repo克隆为workuser,运行git clone git@github-work:company/project.git.
Von*_*onC 83
使用Git 2.10+(Q3 2016年公布的九月2D,2016),你必须设置一个可能配置的GIT_SSH_COMMAND(而不仅仅是一个环境变量中描述罗伯特·杰克威尔的回答)
见提交3c8ede3通过(2016年6月26日)阮泰玉维战(pclouds).
(由Junio C gitsterHamano合并- -在提交dc21164,2016年7月19日)
添加了一个新的配置变量,
core.sshCommand以指定每个存储库使用GIT_SSH_COMMAND的值.
core.sshCommand:
Run Code Online (Sandbox Code Playgroud)
如果这个变量被设置,
git fetch并且git push将使用指定的命令,而不是ssh当他们需要连接到远程系统.
该命令与GIT_SSH_COMMAND环境变量的格式相同,并在设置环境变量时被覆盖.
这意味着git clone可以是:
cd /path/to/my/repo
git config core.sshCommand 'ssh -i private_key_file'
# later on
git clone host:repo.git
Run Code Online (Sandbox Code Playgroud)
Dav*_*vid 67
如上所述:https://superuser.com/a/912281/607049
您可以按每个仓库配置它:
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -F /dev/null"
git pull
git push
Run Code Online (Sandbox Code Playgroud)
Ric*_*pie 67
最快、最简单的方法是:
使用 ssh 克隆您的存储库:
git -c core.sshCommand="ssh -i ~/.ssh/<your_key>" clone git@github.com:<user>/<repo>.git
然后cd进入您克隆的存储库并:
git config core.sshCommand 'ssh -i ~/.ssh/<your_key>'
要测试它是否有效:
git --git-dir=/path/to/repo/.git pull
所以你可能想知道:为什么我创建的 ssh 密钥在我将 .pub 植入 github 后不起作用,而 private 位于默认目录中?
该文档为我们提供了一个命令来澄清该问题:
ssh -vT git@github.com
输出显示 git 查找的 ssh 密钥名称列表。因此,您可能希望使用这些名称之一创建密钥,或者使用上述过程来包含您需要的名称。
Fli*_*imm 36
此命令克隆存储库并将 SSH 密钥配置为永久使用:
git clone -c "core.sshCommand=ssh -i ~/.ssh/<key>" git@github.com:<user>/<repo>.git
Run Code Online (Sandbox Code Playgroud)
现在,如果您运行git fetch、git pull或git push,它将使用在 中配置的 SSH 密钥core.sshCommand(保存在 中.git/config)。
tha*_*ter 33
更好的方法是将主机或ip添加到.ssh/config文件中,如下所示:
Host (a space separated list of made up aliases you want to use for the host)
User git
Hostname (ip or hostname of git server)
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_(the key you want for this repo)
Run Code Online (Sandbox Code Playgroud)
Jam*_*mie 30
我选择了GIT_SSH环境变量.这是我的包装器,类似于上面的Joe Block,但处理任意数量的参数.
文件〜/ gitwrap.sh
#!/bin/bash
ssh -i ~/.ssh/gitkey_rsa "$@"
Run Code Online (Sandbox Code Playgroud)
然后,在我的.bashrc中,添加以下内容:
export GIT_SSH=~/gitwrap.sh
Run Code Online (Sandbox Code Playgroud)
car*_*org 24
GIT_SSH_COMMAND="ssh -i /path/to/git-private-access-key" git clone $git_repo
Run Code Online (Sandbox Code Playgroud)
cie*_*ego 15
从 Git 版本 2.10.0 开始,您可以为每个 repo 或全局配置它
git config core.sshCommand "ssh -i ~/.ssh/id_rsa_example -o 'IdentitiesOnly yes'"
Run Code Online (Sandbox Code Playgroud)
这将为当前存储库指定将使用什么 ssh 密钥。我假设如果你想指定这个全局只需要设置--global选项。
小智 14
问题是当您在同一主机上有不同的远程存储库(例如github.com),并且您想使用不同的ssh密钥(即不同的GitHub帐户)与它们进行交互时。
为了做到这一点:
1)首先,您应该在〜/ .ssh / config文件中声明不同的密钥。
# Key for usual repositories on github.com
Host github.com
HostName github.com
User git
IdentityFile ~/.ssh/id_rsa
# Key for a particular repository on github.com
Host XXX
HostName github.com
User git
IdentityFile ~/.ssh/id_other_rsa
Run Code Online (Sandbox Code Playgroud)
这样,您将第二个密钥与github.com的新友好名称“ XXX”相关联。
2)然后,您必须更改特定存储库的远程来源,以便它使用您刚刚定义的友好名称。
在命令提示符下转到本地存储库文件夹,并显示当前的远程来源:
>git remote -v
origin git@github.com:myuser/myrepo.git (fetch)
origin git@github.com:myuser/myrepo.git (push)
Run Code Online (Sandbox Code Playgroud)
然后使用以下命令更改原点:
>git remote set-url origin git@XXX:myuser/myrepo.git
>git remote -v
origin git@XXX:myuser/myrepo.git (fetch)
origin git@XXX:myuser/myrepo.git (push)
Run Code Online (Sandbox Code Playgroud)
现在,您可以使用右键自动推,取...。
cgn*_*utt 11
如果这里没有其他解决方案对您有用,并且您已经创建了多个ssh-key,但仍然无法完成诸如
git pull
Run Code Online (Sandbox Code Playgroud)
然后假设您有两个ssh密钥文件,例如
id_rsa
id_rsa_other_key
Run Code Online (Sandbox Code Playgroud)
然后在git repo内部,尝试:
# Run these commands INSIDE your git directory
eval `ssh-agent -s`
ssh-add ~/.ssh/id_rsa
ssh-add ~/.ssh/id_rsa_other_key
Run Code Online (Sandbox Code Playgroud)
并通过以下方式确保您的github默认用户名和用户ID正确:
# Run these commands INSIDE your git directory
git config user.name "Mona Lisa"
git config user.email "mona.lisa@email.com"
Run Code Online (Sandbox Code Playgroud)
有关更多信息,请参见https://gist.github.com/jexchan/2351996。
当你需要一个正常的请求(连接到github上git pull origin master),设置主机为*在~/.ssh/config工作对我来说,任何其他主机(比如,"github上"或"国标")不能正常工作.
Host *
User git
Hostname github.com
PreferredAuthentications publickey
IdentityFile ~/.ssh/id_rsa_xxx
Run Code Online (Sandbox Code Playgroud)
其中许多解决方案看起来很诱人.但是,我发现以下链接中的通用git-wrapping-script方法是最有用的:
关键是没有如下git命令:
git -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git
Run Code Online (Sandbox Code Playgroud)
Alvin的解决方案是使用一个明确定义的bash-wrapper脚本来填补这个空白:
git.sh -i ~/.ssh/thatuserkey.pem clone thatuser@myserver.com:/git/repo.git
Run Code Online (Sandbox Code Playgroud)
在哪里git.sh:
#!/bin/bash
# The MIT License (MIT)
# Copyright (c) 2013 Alvin Abad
# https://alvinabad.wordpress.com/2013/03/23/how-to-specify-an-ssh-key-file-with-the-git-command
if [ $# -eq 0 ]; then
echo "Git wrapper script that can specify an ssh-key file
Usage:
git.sh -i ssh-key-file git-command
"
exit 1
fi
# remove temporary file on exit
trap 'rm -f /tmp/.git_ssh.$$' 0
if [ "$1" = "-i" ]; then
SSH_KEY=$2; shift; shift
echo "ssh -i $SSH_KEY \$@" > /tmp/.git_ssh.$$
chmod +x /tmp/.git_ssh.$$
export GIT_SSH=/tmp/.git_ssh.$$
fi
# in case the git command is repeated
[ "$1" = "git" ] && shift
# Run the git command
git "$@"
Run Code Online (Sandbox Code Playgroud)
我可以验证这解决了我用用户/密钥识别具有用于远程到位桶回购一个问题git remote update,git pull和git clone; 所有这些现在在一个cron工作脚本中工作正常,否则在导航有限shell时遇到问题.我也能从R中调用这个脚本,仍然可以解决完全相同的cron执行问题(例如system("bash git.sh -i ~/.ssh/thatuserkey.pem pull")).
不是R和Ruby一样,但如果R可以做到...... O :-)
很多很好的答案,但其中一些假设具有先前的管理知识。
我认为重要的是要明确强调的是,如果你通过克隆该网页的网址开始你的项目
-https://github.com/<user-name>/<project-name>.git
那么你需要确保该url 值下[remote "origin"]的.git/config 被改为SSH URL(见下面的代码块)。
除此之外,请确保添加sshCommmand如下所述:
user@workstation:~/workspace/project-name/.git$ cat config
[core]
repositoryformatversion = 0
filemode = true
bare = false
logallrefupdates = true
sshCommand = ssh -i ~/location-of/.ssh/private_key -F /dev/null <--Check that this command exist
[remote "origin"]
url = git@github.com:<user-name>/<project-name>.git <-- Make sure its the SSH URL and not the WEB URL
fetch = +refs/heads/*:refs/remotes/origin/*
[branch "master"]
remote = origin
merge = refs/heads/master
Run Code Online (Sandbox Code Playgroud)
在此处阅读更多相关信息。
小智 9
为什么不将身份密钥的位置添加到特定存储库的 git 配置文件中,如下所示:
cd .git
vi config
[core]
sshCommand = ssh -i <IDENTITY_KEY_LOCATION> -o IdentitiesOnly=yes
Run Code Online (Sandbox Code Playgroud)
这就是你所需要的。
如果您的路径中有要使用给定识别文件进行签名的目录,您可以通过 .ssh/config 文件指定使用特定识别文件,方法是设置ControlPath例如:
host github.com
ControlPath ~/Projects/work/**
HostName github.com
IdentityFile ~/.ssh/id_work
User git
Run Code Online (Sandbox Code Playgroud)
然后ssh在给定的工作路径下执行 git 命令时将使用指定的身份文件。
我只需要添加密钥然后再次运行 git clone。
ssh-add ~/.ssh/id_rsa_mynewkey
git clone git@bitbucket.org:mycompany/myrepo.git
Run Code Online (Sandbox Code Playgroud)
假设您在 aws 上有一个 ubuntu 服务器,您通常像这样连接到它:
% ssh -i blah/yourkeypair.pem ubuntu@test.fattie.com
Run Code Online (Sandbox Code Playgroud)
在终端只是
% export GIT_SSH_COMMAND="ssh -i /Users/fattie/Desktop/blah/yourkeypair.pem"
Run Code Online (Sandbox Code Playgroud)
在你这样做之后。然后就可以自由...
% git clone ubuntu@test.fattie.com:/home/ubuntu/teste.git
Run Code Online (Sandbox Code Playgroud)
这会将您服务器上的 repo 克隆到您的本地文件夹“teste”,
然后,您可以在 teste/ 中自由执行通常的命令,例如 ...
% git push origin master
Run Code Online (Sandbox Code Playgroud)
等等。
——
另请注意:https : //stackoverflow.com/a/67287133/294884
至于在服务器上,看来你基本上
] git clone --bare the-actual-folder teste.git
Run Code Online (Sandbox Code Playgroud)
然后在 teste.git
] git init --bare --shared
Run Code Online (Sandbox Code Playgroud)
在带有 Git Bash 的 Windows 中,您可以使用以下命令添加存储库
ssh-agent bash -c 'ssh-add "key-address"; git remote add origin "rep-address"'
Run Code Online (Sandbox Code Playgroud)
例如:
ssh-agent bash -c 'ssh-add /d/test/PrivateKey.ppk; git remote add origin git@git.test.com:test/test.git'
Run Code Online (Sandbox Code Playgroud)
哪个私钥在D盘,电脑的文件夹test。另外,如果你想克隆一个存储库,你可以git remote add origin用git clone.
将此输入到 Git Bash 后,它会要求您输入密码!
请注意,openssh 私钥和 putty 私钥是不同的!
如果您使用 puttygen 创建了您的密钥,您必须将您的私钥转换为 openssh!
这里给出的大部分答案都没有解释最基本的用法的细节。
linux在云中设置服务器(在本例中为服务器)后,您可以从终端使用 ssh 连接到它。
dyson-ubuntu-vm.pem在您的计算机上,将云服务提供商(例如 Azure、AWS 等)提供给您的私钥添加到本地计算机上的 .ssh 配置中,如下所示:
将文件复制.pem到该/home/ssenyonjo/.ssh文件夹,然后打开/home/ssenyonjo/.ssh/config文件并添加以下条目:
Host 20.85.213.44
HostName 20.85.213.44
User Dyson
IdentityFile /home/ssenyonjo/.ssh/dyson-ubuntu-vm.pem
IdentitiesOnly yes
Run Code Online (Sandbox Code Playgroud)
现在从您的终端访问云 Linux 服务器,如下所示:
ssh Dyson@20.85.213.44
Run Code Online (Sandbox Code Playgroud)
当它起作用时,在云服务器上创建一个 git 项目,如下所示:
Dyson@dyson-ubuntu-vm:~/projects$ git init --bare s2
Run Code Online (Sandbox Code Playgroud)
现在回到本地计算机并克隆该空存储库,如下所示:
ssenyonjo@ssenyonjo-pc:~/Projects/mastering-git$ git clone ssh://Dyson@20.85.213.44/home/Dyson/projects/s2
Run Code Online (Sandbox Code Playgroud)
如果您看到类似于以下内容的错误:fatal: Could not read from remote repository,则意味着您访问了错误的文件夹。确保您已经概述了从根到创建的存储库的正确路径。
如果您不想设置配置文件但想访问需要密钥的 ssh 服务器,您可以使用以下命令:
GIT_SSH_COMMAND='ssh -i ~/Projects/aws/keys/aws_ubuntu.pem' git clone ssh://ubuntu@15.207.99.158/home/ubuntu/projects/mastering-git/rand
Run Code Online (Sandbox Code Playgroud)
您可以导出该命令以继续将其用于其他任务,例如git push和git pull
export GIT_SSH_COMMAND='ssh -i ~/Projects/aws/keys/aws_ubuntu.pem'
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
953159 次 |
| 最近记录: |