简体   繁体   English

Git 和 SSH,使用哪个密钥?

[英]Git and SSH, which key is used?

Say your .ssh directory 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 在哪里可以检查哪一个用于连接到给定的远程存储库?

The following entry in .ssh/config file solves the problem .ssh/config文件中的以下条目解决了该问题

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

Where ~/.ssh/whatever is a path to your private key ~/.ssh/whatever是你的私钥的路径

Additionally, user and host can be picked up from此外,用户和主机可以从

git push git@git.assembla.com:repo_name.git
         ^__ ^_______________
         user host

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 git@github.com can give you the answer.现在如果你把它和 Git 自己的SSH 帮助页面中的第 4 步结合起来, ssh -vT git@github.com可以给你答案。

Note: You can also use the -i switch to tell ssh during command execution, which keyfile to use.注意:您还可以使用-i开关在命令执行期间告诉 ssh,使用哪个密钥文件。

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.当然,根据具体情况,将其导出到当前 SHELL 的环境可能会有所帮助,这样您就不必每次都手动添加它。 Then it'd be this way:那么它会是这样的:

export GIT_SSH_COMMAND='ssh -v'
git …

— As man git suggests there're a few of environmental variables that would affect Git's operations with use of SSH. — 正如man git所暗示的,有一些环境变量会影响 Git 在使用 SSH 时的操作。 According to man ssh you can get some debugging info when deploying -v option (not only but also, check out the manual if you're curious for more).根据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 question. ……这就是你问题的答案。

Unless it is specified on the .ssh/config it will use the default private key file.除非在.ssh/config中指定,否则它将使用默认的私钥文件。

The default file is ~/.ssh/id_rsa or ~/.ssh/id_dsa or ~/.ssh/identity depending on the protocol version.默认文件是~/.ssh/id_rsa~/.ssh/id_dsa~/.ssh/identity ,具体取决于协议版本。

这可能是超级优势,但是在运行ssh -vT git@github.com之后它向我显示它正在检查/root/.ssh的密钥,我期待它检查我的主目录,然后我意识到我登录为根!

Since git just uses ssh to connect, it will use whichever key ssh would use to connect to the remote host.由于git仅使用ssh进行连接,因此它将使用ssh用于连接到远程主机的任何密钥。 See the ~/.ssh/config file for details;有关详细信息,请参阅~/.ssh/config文件; the host block uses the IdentityFile directive to specify the private key to use. host块使用IdentityFile指令来指定要使用的私钥。 The ssh_config(5) manpage contains full details. ssh_config(5)联机帮助页包含完整的详细信息。

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)。

You can check which the key is being used by trying to connect to git@github.com:您可以通过尝试连接到 git@github.com 来检查正在使用哪个密钥:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type -1
> debug1: identity file /Users/you/.ssh/id_rsa-cert type -1
> debug1: identity file /Users/you/.ssh/id_dsa type -1
> debug1: identity file /Users/you/.ssh/id_dsa-cert type -1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Trying private key: /Users/you/.ssh/id_rsa
> debug1: Trying private key: /Users/you/.ssh/id_dsa
> debug1: No more authentication methods to try.
> Permission denied (publickey).

In that example, we did not have any keys for SSH to use.在那个例子中,我们没有任何可供 SSH 使用的密钥。 The "-1" at the end of the "identity file" lines means SSH couldn't find a file to use. “身份文件”行末尾的“-1”表示 SSH 找不到要使用的文件。 Later on, the "Trying private key" lines also indicate that no file was found.稍后,“尝试私钥”行也表明未找到文件。 If a file existed, those lines would be "1" and "Offering public key", respectively:如果文件存在,这些行将分别为“1”和“提供公钥”:

$ ssh -vT git@github.com
> ...
> debug1: identity file /Users/you/.ssh/id_rsa type 1
> ...
> debug1: Authentications that can continue: publickey
> debug1: Next authentication method: publickey
> debug1: Offering RSA public key: /Users/you/.ssh/id_rsa

Verify the public key is attached to your account验证公钥已附加到您的帐户

You must provide your public key to GitHub to establish a secure connection.您必须向 GitHub 提供您的公钥才能建立安全连接。

  1. Open Terminal.打开终端。

  2. Start SSH agent in the background.在后台启动 SSH 代理。

    $ eval "$(ssh-agent -s)" $ eval "$(ssh-agent -s)"

    Agent pid 59566代理pid 59566

  3. Find and take a note of your public key fingerprint.查找并记下您的公钥指纹。

    $ ssh-add -l -E sha256 $ ssh-add -l -E sha256

    2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA) 2048 SHA256:274ffWxgaxq/tSINAykStUL7XWyRNcRTlcST1Ei7gBQ /Users/USERNAME/.ssh/id_rsa (RSA)

  4. In the upper-right corner of any github page, click your profile photo, then click Settings.在任何 github 页面的右上角,单击您的个人资料照片,然后单击设置。

  5. In the user settings sidebar, click SSH and GPG keys.在用户设置侧栏中,单击 SSH 和 GPG 密钥。

  6. Compare the list of SSH keys with the output from the ssh-add command.将 SSH 密钥列表与 ssh-add 命令的输出进行比较。

  7. If you don't see your public key in GitHub, you'll need to add your SSH key to GitHub to associate it with your computer.如果您在 GitHub 中没有看到您的公钥,则需要将您的 SSH 密钥添加到 GitHub 以将其与您的计算机相关联。

Follow this link for details点击此链接了解详情

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM