这可能是一种非常不寻常的情况,但我想指定在本地计算机上执行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连接到远程服务器的示例,该服务器使用指定的私钥,但这是一个本地命令。有可能吗?


当前回答

将该主机或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)

其他回答

在使用GitBash的Windows中,您可以使用以下方法添加存储库

ssh-agent bash -c 'ssh-add "key-address"; git remote add origin "rep-address"'

例如:

ssh-agent bash -c 'ssh-add /d/test/PrivateKey.ppk; git remote add origin git@git.test.com:test/test.git'

哪个私钥在驱动器D中,计算机的文件夹测试。此外,如果您想克隆存储库,可以使用gitclone更改git远程添加源。

在Git Bash中输入此信息后,它会要求您输入密码!

请注意,openssh私钥和putty私钥是不同的!

如果您使用puttygen创建了密钥,则必须将私钥转换为openssh!

将该主机或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)

这样的方法应该有效(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

您需要创建一个~/.ssh/config,如下所示

Host <Your bitbucket server>
User <userid>
Hostname <Your bitbucket server as above>
IdentitiesOnly yes
IdentityFile ~/.ssh/id_rsa<file> This is your private key file

许可如下

-rw------- $HOME/.ssh/config

将公钥添加到git中(cat~/.ssh/id_rsa_b[或类似名称])

然后是git clone,如下所示

git clone ssh://blahblah@blah.com/userid/test.git