【问题标题】:Git can't connect to remote using private ssh fileGit无法使用私有ssh文件连接到远程
【发布时间】:2018-01-27 15:56:50
【问题描述】:

我正在使用(相对)新的 Git sshCommand:

git config core.sshCommand  'ssh -i ~/Documents/keydir/key.pem'

这会产生以下配置:

sshCommand = ssh -i ~/Documents/keydir/key.pem

但是当我尝试推送到服务器时(顺便说一句,我使用的是主目录。它必须是 www 目录吗?)

[remote "origin"]
        url = ssh://ubuntu@myserver.com:22/home/ubuntu/myrepo.git

[remote "origin"]
            url = ssh://ubuntu@myserver.com/home/ubuntu/myrepo.git

它返回:

git push remote master
fatal: 'remote' does not appear to be a git repository
fatal: Could not read from remote repository.

...错误。

一直想不通。

我尝试过使用“~/myrepo.git”和“/home/ubuntu/myrepo.git”,也尝试过“url = ssh://ubuntu@myserver.com:/home/ubuntu/ myrepo.git"

我也尝试在 /var/www 文件夹中启动一个新的 git...

似乎没有任何效果。 myrepo.git 肯定在 ubuntu 主文件夹中。

有什么想法吗?

编辑:我完整的 .git/config 文件:

[core]
        repositoryformatversion = 0
        filemode = true
        bare = false
        logallrefupdates = true
        ignorecase = true
        precomposeunicode = true
        sshCommand = ssh -i ~/Documents/keydir/key.pem
[remote "origin"]
        url = ssh://ubuntu@myserver.com:22/home/ubuntu/mygit.git
        fetch = +refs/heads/*:refs/remotes/origin/*

编辑:将本地 (2.5.4) 和远程 (2.7.4) 版本的 Git 更新到最新版本 (2.16.1) 解决了这个问题。

【问题讨论】:

    标签: git ubuntu ssh


    【解决方案1】:

    您需要先检查是否可以 ssh 到该服务器:

    ssh -Tv -i ~/Documents/keydir/key.pem ubuntu@myserver.com
    

    换句话说,您必须确保 ~ubunutu/.ssh/authorized_keys 文件的 key.pub public 密钥。

    那么命令必须是:

     git push -u origin master
    

    这只是您第一次推送该分支:请参阅“Why do I need to explicitly push a new branch?

    URL 正确:添加或删除端口 22 无关紧要,因为它是默认的 SSH 端口。

    作为per discussion,两端(本地和远程)使用相当旧的 Git 版本(MacOS 上为 2.5.4,Ubuntu 上为 2.7.4)。
    升级它们都有帮助。

    【讨论】:

    • 我可以“ssh -i”进入服务器就好了。但是使用“git push -u origin master”我得到:“权限被拒绝(公钥)。”......我不知道我可以很好地ssh到服务器上。我需要制作与 pem 不同的密钥吗?或者是因为我在主文件夹中……还是……
    • 您是否设置了 GIT_SSH_COMMAND 环境变量? (git-scm.com/docs/git-config#git-config-coresshCommand) 或 GIT_SSH 之一 (git-scm.com/docs/git#git-codeGITSSHcode)
    • ssh 命令的设置与我在问题中的设置完全相同。 sshCommand = ssh -i ~/Documents/keydir/key.pem
    • @mysticcola 我的观点:您在问题中设置的内容将被环境变量覆盖。因此我的问题。
    • 在这种情况下,答案是“据我所知不是”。我会直截了当地说“不”......因为我从来没有设置过......然后它又是一台二手笔记本电脑,所以我不能确定。不过,我敢打赌“不”。有什么方法可以查吗?
    【解决方案2】:

    我认为您的git push 命令(“git push remote master”)不正确。

    尝试改用git push origin master

    (您可能还需要验证 myrepo.git 文件实际上是一个 git 存储库,由 git 命令初始化。您指定的 URL 似乎遵循 the docs 中指定的正确格式。)

    【讨论】:

      猜你喜欢
      • 2015-10-09
      • 2020-10-28
      • 2011-07-11
      • 1970-01-01
      • 2012-09-02
      • 2018-06-04
      • 1970-01-01
      • 2017-08-01
      • 2018-05-20
      相关资源
      最近更新 更多