【问题标题】:Git call SSH with keys not workingGit调用SSH,密钥不起作用
【发布时间】:2017-03-20 21:00:38
【问题描述】:

我已将我网站的 git 存储库(托管在 CentOS 7 服务器上)克隆到本地文件夹(位于我的 Windows 7 PC 上)。我想使用git pull之类的git命令,而不必每次都输入用户的密码。

>git remote -v
origin  ssh://mylinuxuser@1.2.3.4:1234/home/mylinuxuser/domains/mydomain/public_html/ (fetch)
origin  ssh://mylinuxuser@1.2.3.4:1234/home/mylinuxuser/domains/mydomain/public_html/ (push)

我已经创建了一个密钥对。 id_rsa 文件在 C:\Users\mywindowsuser\.ssh 中,id_rsa.pub 我已发送到服务器并添加到 authorized_keys 文件中:

cat id_rsa.pub >> authorized_keys

authorized_keys 文件归mylinuxuser 所有,访问权限为600.ssh文件夹的访问权限为700

在我的本地机器上,我在.ssh 文件夹中的config 文件中填充了

Host *
  IdentityFile ~/.ssh/id_rsa

我也尝试在 linux 服务器上将 ssh service sshd restart 重新启动为 su。没有帮助。

还向许多路径添加了id_rsa,因为我在堆栈上找到了一些以这种方式引导的答案:

C:\Program Files\Git\.ssh
C:\Program Files (x86)\Git\.ssh
C:\Users\mywindowsuser\AppData\Local\VirtualStore\Program Files\Git\.ssh
C:\Users\mywindowsuser\AppData\Local\VirtualStore\Program Files (x86)\Git\.ssh

这些都不起作用。我想知道是否有其他文件在干扰。例如,我在C:\Users\mywindowsuser\.ssh 中有一个environment 文件和一个known_hosts 文件,但我对此表示怀疑。

不过,每当我尝试拨打 git pull 时,它都会要求输入 mylinuxuser 密码...

任何帮助将不胜感激!

[EDIT] cmets 中建议的结果:

ssh -v -p1234 mylinuxuser@1.2.3.4
OpenSSH_7.1p1, OpenSSL 1.0.2d 9 Jul 2015
debug1: Reading configuration data /c/Users/mywindowsuser/.ssh/config
debug1: /c/Users/mywindowsuser/.ssh/config line 1: Applying options for *
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Connecting to 1.2.3.4 [1.2.3.4] port 1234.
debug1: Connection established.
debug1: identity file /c/Users/mywindowsuser/.ssh/id_rsa type 1
debug1: key_load_public: No such file or directory
debug1: identity file /c/Users/mywindowsuser/.ssh/id_rsa-cert type -1
debug1: Enabling compatibility mode for protocol 2.0
debug1: Local version string SSH-2.0-OpenSSH_7.1
debug1: Remote protocol version 2.0, remote software version OpenSSH_6.6.1
debug1: match: OpenSSH_6.6.1 pat OpenSSH_6.6.1* compat 0x04000000
debug1: Authenticating to 1.2.3.4:1234 as 'mylinuxuser'
debug1: SSH2_MSG_KEXINIT sent
debug1: SSH2_MSG_KEXINIT received
debug1: kex: server->client aes128-ctr umac-128-etm@openssh.com none
debug1: kex: client->server aes128-ctr umac-128-etm@openssh.com none
debug1: SSH2_MSG_KEX_DH_GEX_REQUEST(1024<3072<8192) sent
debug1: got SSH2_MSG_KEX_DH_GEX_GROUP
debug1: SSH2_MSG_KEX_DH_GEX_INIT sent
debug1: got SSH2_MSG_KEX_DH_GEX_REPLY
debug1: Server host key: ssh-rsa SHA256:verylongkey
debug1: Host '[1.2.3.4]:1234' is known and matches the RSA host key.
debug1: Found key in /c/Users/mywindowsuser/.ssh/known_hosts:1
debug1: SSH2_MSG_NEWKEYS sent
debug1: expecting SSH2_MSG_NEWKEYS
debug1: SSH2_MSG_NEWKEYS received
debug1: Roaming not allowed by server
debug1: SSH2_MSG_SERVICE_REQUEST sent
debug1: SSH2_MSG_SERVICE_ACCEPT received
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: publickey
debug1: Offering RSA public key: /c/Users/mywindowsuser/.ssh/id_rsa
debug1: Authentications that can continue: publickey,password
debug1: Next authentication method: password

【问题讨论】:

  • 我假设您已将公共 ssh 密钥添加到 git-hub 帐户,但您可能需要将新私钥添加到 ssh-agent。试试ssh-add ~/.ssh/id_rsa。确保ssh-agent 正在运行。
  • 这不是我说的私人服务器的github帐户。我按照你的建议做了 - 在常规 cmd 中它不起作用,但它在 git bash 中起作用,我得到了 Identity added: /c/Users/mywindowsuser/.ssh/id_rsa (/c/Users/mywindowsuser/.ssh/id_rsa) 作为答案......但是当我尝试 git pull 时它仍然要求输入密码所以它不起作用,很遗憾
  • 通过详细尝试使用 ssh 登录来调试它。 ssh -v -v -p 1234 mylinuxuser@1.2.3.4
  • @Schwern 使用来自详细调用的数据进行编辑
  • 提供密钥,但被拒绝。仔细检查authorized_keys 是否在遥控器上的~mylinuxuser/.ssh 中,并且authorized_keys 在其自己的行中包含您的公钥;如果 authorized_keys 末尾没有换行符,那么 cat id_rsa.pub &gt;&gt; authorized_keys 将使其格式错误。

标签: git ssh ssh-keys


【解决方案1】:

我知道你面临的问题。

您需要提供访问权限。

试试 chmod 600,它会解决你的问题。

chmod 700/root/.ssh/authorized_keys
chmod 600 /root/.ssh/authorized_keys
restorecon -R -v /root/.ssh

上面的东西解决了你的问题,那么你不需要遵循这个。 确保其他用户无法修改您的目录。

chmod g-w,o-w /home/USERNAME

【讨论】:

  • 这里的含义是您允许以 root 身份远程登录。不要鼓励人们以 root 身份登录,这就像拿着上膛的枪四处走动,安全关闭,手指扣在扳机上。让他们使用 sudo。
  • @Schwern,你说得对,不鼓励以 root 身份登录。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2015-11-19
  • 2023-01-05
  • 2018-03-17
  • 1970-01-01
  • 2021-06-06
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多