Git 和 SSH,使用哪个密钥?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11093358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 13:57:34  来源:igfitidea点击:

Git and SSH, which key is used?

gitssh

提问by James Raitsev

Say your .sshdirectory contains 30 keys (15 private and 15 public).

假设您的.ssh目录包含 30 个密钥(15 个私有密钥和 15 个公共密钥)。

Where in Git can one check which one is used to connect to a given remote repository?

在 Git 中哪里可以检查哪个用于连接到给定的远程存储库?

采纳答案by James Raitsev

The following entry in .ssh/configfile solves the problem

.ssh/config文件中的以下条目解决了问题

  host git.assembla.com
  user git
  identityfile ~/.ssh/whatever

Where ~/.ssh/whateveris a path to your private key

~/.ssh/whatever你的私钥的路径在哪里

Additionally, user and host can be picked up from

此外,用户和主机可以从

git push [email protected]:repo_name.git
         ^__ ^_______________
         user host

回答by Vajk Hermecz

Executing ssh in verbose mode, aka ssh -v user@host, will print a huge load of debugging info, which also contains details on which keyfiles it is trying for login.

以详细模式执行 ssh,也就是ssh -v user@host,将打印大量调试信息,其中还包含有关尝试登录的密钥文件的详细信息。

debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /home/user/.ssh/id_rsa
debug1: Server accepts key: pkalg ssh-rsa blen 332
debug1: read PEM private key done: type RSA
debug1: Authentication succeeded (publickey).

Now if you combine this, with the Step 4 in Git's own SSH help page, ssh -vT [email protected]can give you the answer.

现在,如果您将其与 Git 自己的SSH 帮助页面中的第 4 步结合起来,ssh -vT [email protected]可以给您答案。

Note: You can also use the -iswitch to tell ssh during command execution, which keyfile to use.

注意:您还可以-i在命令执行期间使用该开关告诉 ssh 使用哪个密钥文件。

回答by Rodrigo Flores

Unless it is specified on the .ssh/configit will use the default private key file.

除非在 上指定,.ssh/config否则它将使用默认的私钥文件。

The default file is ~/.ssh/id_rsaor ~/.ssh/id_dsaor ~/.ssh/identitydepending on the protocol version.

默认文件是~/.ssh/id_rsa~/.ssh/id_dsa~/.ssh/identity取决于协议版本。

回答by sarnold

Since gitjust uses sshto connect, it will use whichever key sshwould use to connect to the remote host. See the ~/.ssh/configfile for details; the hostblock uses the IdentityFiledirective to specify the private key to use. The ssh_config(5)manpage contains full details.

由于git只是用于ssh连接,它将ssh使用用于连接到远程主机的任何密钥。详情见~/.ssh/config文件;该host块使用该IdentityFile指令指定要使用的私钥。该ssh_config(5)手册页包含了全部细节。

回答by Moak

This might be super edge, but after running ssh -vT [email protected]it showed me it was checking /root/.sshfor the keys, I was expecting it to check my home directory and then I realized I was logged in as root!

这可能是超级优势,但是在运行ssh -vT [email protected]它后,它向我显示它正在检查/root/.ssh密钥,我期待它检查我的主目录,然后我意识到我以 root 身份登录!

回答by poige

I'd say most practical to my taste would be:

我想说对我来说最实用的是:

GIT_SSH_COMMAND='ssh -v' git …

of course, depending on circumstances it might be beneficial just to export it to current SHELL's environment so that you won't have to prepend it manually each time. Then it'd be this way:

当然,根据情况,将其导出到当前的 SHELL 环境可能会有所帮助,这样您就不必每次都手动添加它。那么就会是这样:

export GIT_SSH_COMMAND='ssh -v'
git …

— As man gitsuggests there're a few of environmental variables that would influence on Git's operations with use of SSH. According to man sshyou can get some debugging info when deploying -voption (not only but also, check out the manual if you're curious for more).

— 正如man git建议的那样,有一些环境变量会影响使用 SSH 的 Git 操作。根据man ssh您可以在部署-v选项时获得一些调试信息(不仅如此,如果您想了解更多,请查看手册)。

which key is used?

使用哪个键?

In the output you would see smth like …

在输出中,您会看到类似……

debug1: Offering public key: …

… which is the answer to your q-n.

......这是你的qn的答案。

回答by seumasmac

On the remote server, edit the sshd_config file and change LogLevel from INFO to VERBOSE and restart ssh.

在远程服务器上,编辑 sshd_config 文件并将 LogLevel 从 INFO 更改为 VERBOSE,然后重新启动 ssh。

Now your log file will hold the fingerprint of the key that was used to authenticate each user.

现在您的日志文件将保存用于验证每个用户的密钥的指纹。

On Ubuntu, these files are:

在 Ubuntu 上,这些文件是:

/etc/ssh/sshd_config
/var/log/auth.log

but they may be different on another distro. Just google for their location (some use /var/log/secure for example).

但它们在另一个发行版上可能不同。只需谷歌搜索他们的位置(例如,有些人使用 /var/log/secure)。