github - 如何防止 SourceTree/SSH 忘记 SSH key ? (我必须手动重新运行 'ssh-add' 才能让它再次工作!)

标签 github ssh bitbucket atlassian-sourcetree

更新-又发生了!!!

好的,所以这又发生了!男人真是令人沮丧!但这一次我挖得更深了,发现由于某种原因,我的私钥被卸载了。

具体来说,当我称之为...

ssh-add -l -E md5

我明白这个...
The agent has no identities.

但是,如果我再运行这个......
ssh-add /Users/[username]/.ssh/[private key]

一切都恢复正常! SourceTree 就像它应该的那样连接。

问题是为什么我必须继续运行“ssh-add”命令?!为什么它总是忘记我的 key ?!

正如其他地方所提到的,不确定这是否会有所不同,但我正在运行带有 High Sierra 的 MacBook Pro,尽管这也发生在 Sierra 上。

原帖:

这个让我既难过,又恼火!! SourceTree(或 ssh 之类的!)每天都在忘记/不应用/忽略我的 SSH key !我不知道为什么。

Note: Updated to use BitBucket's info instead of GitHub.



这是我当前 config 的相关部分文件
# --- Sourcetree Generated ---
Host MarqueIV-Bitbucket
    HostName bitbucket.org
    User MarqueIV
    PreferredAuthentications publickey
    IdentityFile /Users/MarqueIV/.ssh/MarqueIV-Bitbucket
    UseKeychain yes
    AddKeysToAgent yes
# ----------------------------

这是我的 ~/.ssh 文件夹的“ls”(截断)
-rw-r--r--@ 1 MarqueIV  staff   421 Dec 14 11:25 config
-rw-r--r--@ 1 MarqueIV  staff  1808 Dec  9 14:20 known_hosts
-rw-------  1 MarqueIV  staff  3243 Dec  6 23:33 MarqueIV-Bitbucket
-rw-r--r--  1 MarqueIV  staff   781 Dec  6 23:33 MarqueIV-Bitbucket.pub

这是我的known_hosts文件(已编辑 key )
bitbucket.org,104.192.143.3 ssh-rsa [redacted]
bitbucket.com,104.192.143.9 ssh-rsa [redacted]
104.192.143.2 ssh-rsa [redacted]

Note: Not sure if this matters, but you can see lines 1 and 2 seem to be duplicates.



这是 ssh -Tv git@bitbucket.org 的输出
OpenSSH_7.6p1, LibreSSL 2.6.2
debug1: Reading configuration data /Users/MarqueIV/.ssh/config
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: /etc/ssh/ssh_config line 48: Applying options for *
debug1: Connecting to bitbucket.org port 22.
debug1: Connection established.
debug1: key_load_public: No such file or directory
debug1: identity file /Users/MarqueIV/.ssh/id_rsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/MarqueIV/.ssh/id_rsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/MarqueIV/.ssh/id_dsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/MarqueIV/.ssh/id_dsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/MarqueIV/.ssh/id_ecdsa type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/MarqueIV/.ssh/id_ecdsa-cert type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/MarqueIV/.ssh/id_ed25519 type -1
debug1: key_load_public: No such file or directory
debug1: identity file /Users/MarqueIV/.ssh/id_ed25519-cert type -1
debug1: Local version string SSH-2.0-OpenSSH_7.6
debug1: Remote protocol version 2.0, remote software version conker_1.0.315-a08d059 app-153
debug1: no match: conker_1.0.315-a08d059 app-153
debug1: Authenticating to bitbucket.org:22 as 'git'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: algorithm: curve25519-sha256@libssh.org
debug1: kex: host key algorithm: ssh-rsa
debug1: kex: server->client cipher: aes128-ctr MAC: hmac-sha2-256-etm@openssh.com compression: none
debug1: kex: client->server cipher: aes128-ctr MAC: hmac-sha2-256-etm@openssh.com compression: none
debug1: expecting SSH2_MSG_KEX_ECDH_REPLY
debug1: Server host key: ssh-rsa SHA256:zzXQOXSRBEiUtuE8AikJYKwbHaxvSc0ojez9YXaGp1A
debug1: Host 'bitbucket.org' is known and matches the RSA host key.
debug1: Found key in /Users/MarqueIV/.ssh/known_hosts:1
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: rekey after 4294967296 blocks
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey
debug1: Next authentication method: publickey
debug1: Offering public key: RSA SHA256:h+6zCXg32Uw4fYxSUMwYst3zee8RFb9Z47H1QUTz58E /Users/MarqueIV/.ssh/MarqueIV-GitHub
debug1: Authentications that can continue: publickey
debug1: Trying private key: /Users/MarqueIV/.ssh/id_rsa
debug1: Trying private key: /Users/MarqueIV/.ssh/id_dsa
debug1: Trying private key: /Users/MarqueIV/.ssh/id_ecdsa
debug1: Trying private key: /Users/MarqueIV/.ssh/id_ed25519
debug1: No more authentication methods to try.
git@bitbucket.org: Permission denied (publickey).

看看它似乎没有应用 config 中定义的 key 。和 known_hosts ?好像这会是个问题,不是吗?

Note: I'm using macOS Sierra, and I have updated my path to include /usr/bin before /usr/local/bin as outlined here. If I don't do that, I get an error saying ssh doesn't recognize UseKeychain yes in the config.



结果,几乎每天都是我必须经历的例程。我将使用 GitHub 作为我的示例。
  • 我打开 SourceTree 并尝试从 GitHub 中获取最新信息。它以“git@github.com: Permission denied (publickey)”失败。信息。
  • 我从 SourceTree 中删除了我的 GitHub 帐户。
  • 我从我机器上的 .ssh 文件夹中删除了 GitHub 的公钥和私钥。
  • 我去 GitHub 并从我的帐户中删除我的旧公钥。
  • 回到 SourceTree,我再次使用我的用户名和密码登录 GitHub。
  • 登录后,我使用 SourceTree 为 GitHub 生成一个新的 SSH key 对。
  • 我将我的公钥复制到我的 GitHub 帐户中的 SSH 区域。 (有时我注意到它为我添加了它,但我喜欢安全并仔细检查。)
  • 现在我可以再次推拉就好了。

  • 我回家休息一天,然后在家登录。它再次失败。重复上述所有步骤。

    如何让 SourceTree/ssh/whatever 记住我的 da*n 键,这样我每次更改位置时都不必继续这样做?!我错过了什么步骤???

    那么任何人都可以提供有关如何使我的 SSH key “粘贴”的建议吗?

    最佳答案

    一、安装latest Git for Windows release ( 2.15.1.2 one ,只需将存档 PortableGit-2.15.1.2-64-bit.7z.exe 解压缩到您想要的任何位置,以及 adding it to your PATH )

    其次,确保您的 SourceTree is using the System Git

    三、test in command-line if your ssh key is recognized :

    ssh -T git@github.com
    
    Hi username! You've successfully authenticated, 
    but GitHub does not provide shell access.
    

    最后,确保 SourceTree / Tools / Option 将 OpenSSH 用作 SSH 客户端(不是腻子)

    然后 SourceTree 应该没有问题重用您的 ssh key 。

    从您的日志中,~/.ssh/config generate 是错误的:它提到 User 您的用户名。
    每当您建立与 github.com/bitbucket.org 的 SSH 连接时,它永远不会是“您”。它始终为 git .
    Host MarqueIV-Bitbucket
        HostName bitbucket.org
        User MarqueIV
        PreferredAuthentications publickey
        IdentityFile /Users/MarqueIV/.ssh/MarqueIV-Bitbucket
        UseKeychain yes
        AddKeysToAgent yes
    

    使用 ssh -Tv MarqueIV-Bitbucket 进行测试

    关于github - 如何防止 SourceTree/SSH 忘记 SSH key ? (我必须手动重新运行 'ssh-add' 才能让它再次工作!),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47804813/

    相关文章:

    github - 如何将 png 等图像上传到 GitHub 存储库?

    Git 注释说明

    Git pull - 涂抹过滤器lfs失败

    git - 是否能够 "break glass"并绕过 Github 分支保护以进行紧急修复?

    ubuntu - Vagrant Master 和 Slave Boxes - SSH key 设置

    git - 如何从 Sourcetree 的全局 .gitignore 列表中删除文件?

    github - git checkout错误:无法创建目录:权限被拒绝

    node.js - Ghost安装成功后出现403 Forbidden

    bash - 将参数传递给ssh中的远程shell

    git - 不懂 fork