这可能是一种非常不寻常的情况,但我想指定在本地计算机上执行shell(git)命令时要使用的私有SSH密钥。

基本上是这样的:

git clone git@github.com:TheUser/TheProject.git -key "/home/christoffer/ssh_keys/theuser"

或者更好(用Ruby):

with_key("/home/christoffer/ssh_keys/theuser") do
  sh("git clone git@github.com:TheUser/TheProject.git")
end

我见过使用Net::SSH连接到远程服务器的示例,该服务器使用指定的私钥,但这是一个本地命令。有可能吗?


当前回答

此外,作为一个简单的“解决方法”,如果您不经常需要它:

删除所有ssh标识

ssh-add -D

重新添加您需要使用的

ssh-add ~/.ssh/MyProperProfile

做gits

(可选)通过添加其他标识(或所有标识)进行还原。

其他回答

对于gitlab RSA认证是

Host gitlab.com
  RSAAuthentication yes
  IdentityFile ~/.ssh/your_private_key_name
  IdentitiesOnly yes

医生来了

这样的方法应该有效(orip建议):

ssh-agent bash -c 'ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git'

如果您更喜欢子壳,可以尝试以下方法(尽管它更脆弱):

ssh-agent $(ssh-add /somewhere/yourkey; git clone git@github.com:user/project.git)

Git将调用SSH,SSH将通过环境变量查找其代理;这将反过来加载密钥。

或者,如果您愿意将仅包含.ssh目录的目录设置为HOME,那么设置HOME也可以达到目的;这可能包含identity.pub或配置文件设置IdentityFile。

2021。如果你在Mac上。

假设你在aws上有一个ubuntu服务器,你通常会这样连接:

% ssh -i blah/yourkeypair.pem ubuntu@test.fattie.com

仅在终端中

% export GIT_SSH_COMMAND="ssh -i /Users/fattie/Desktop/blah/yourkeypair.pem"

在你完成之后。然后你可以自由地。。。

% git clone ubuntu@test.fattie.com:/home/ubuntu/teste.git  

这会将服务器上的repo克隆到本地文件夹“teste”,

然后,在teste/执行通常的命令时,您可以自由地执行。。。

% git push origin master

等等

--

另请注意:https://stackoverflow.com/a/67287133/294884


至于在服务器上,似乎基本上

] git clone --bare the-actual-folder teste.git

然后在teste.git中

] git init --bare --shared

您可以使用GIT_SSH环境变量。但是您需要将ssh和选项打包到shell脚本中。

请参阅命令shell中的git-manual:man-git。

如果SSH端口号不是22(默认值),请在~/.SSH/config中添加端口xx

在我的情况下(同义词),

Host my_synology
    Hostname xxxx.synology.me
    IdentityFile ~/.ssh/id_rsa_xxxx
    User myname
    Port xx

然后使用配置中的主机标题进行克隆。(“my_synology”。避免@chopstik的“*”)

git clone my_synology:path/to/repo.git