【问题标题】:How to configure ssh agent service and keys for git deployments using non-root user?如何使用非 root 用户为 git 部署配置 ssh 代理服务和密钥?
【发布时间】:2016-04-24 05:42:36
【问题描述】:

我在进行部署时遇到了问题(使用非 root 用户):

$> git pull
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

经过一番挖掘,我找到了适合我的解决方法:

$> eval $(ssh-agent)
Agent pid 61080
$> ssh-add ~/.ssh/deploymapr
Identity added: /home/mapr/.ssh/deploymapr (/home/mapr/.ssh/deploymapr)
$> git pull
remote: Counting objects: 10, done.
remote: Compressing objects: 100% (10/10), done.
remote: Total 10 (delta 8), reused 0 (delta 0)
Unpacking objects: 100% (10/10), done.
⋮

现在我经常进行部署 - 每次登录部署服务器时都需要重复上述步骤,这有点麻烦。

我确信有更好、更优雅的方式来为普通用户配置它(我不是在谈论将这两行添加到~/.bashrc...)。有什么想法吗?

更新:根据下面的答案,我已经配置了我的~/.ssh/config 文件:

stat -c "%A %a %n" ~/.gitconfig ~/.ssh/deploymapr ~/.ssh/config
-rw-rw-r-- 664 /home/mapr/.gitconfig
-rw------- 600 /home/mapr/.ssh/deploymapr
-rw-r--r-- 644 /home/mapr/.ssh/config

但我仍然收到错误(当不运行解决方法时=上面的两个命令,手动):

$ git pull
Permission denied (publickey).
fatal: Could not read from remote repository.

Please make sure you have the correct access rights
and the repository exists.

【问题讨论】:

    标签: git ssh ssh-keys sshd ssh-agent


    【解决方案1】:

    将信息存储在~/.ssh/config:

    Host hostname
     IdentityFile ~/.ssh/deploymapr
    

    或将密钥移动到标准位置(~/.ssh/id_rsa 或相应的手册页)。

    【讨论】:

    • Bad owner or permissions on /home/mapr/.ssh/config fatal: Could not read from remote repository.
    • 设置chmod 644 ~/.ssh/config。 openssh 对权限非常严格。并检查该文件是否归您所有。
    • 还是不行。我认为这里的问题是您提供的解决方案不能满足 eval $(ssh-agent) 的要求,我也需要这样做……
    • 有什么用?你的意思是不起作用?将更多详细信息发布到已编辑的问题。当然,您需要将主机名调整为存储您的 git 存储库的真实主机(或完全删除该行)。
    • 不必是别名。你可以把直接的主机名放在那里。您可以在.git/config(在您的存储库中)找到您的 git 服务器的主机名。或者是你做git clone时使用的主机。
    【解决方案2】:

    创建 SSH 配置文件

    创建一个 SSH 配置文件机制来为您的各种身份创建别名。

    您可以使用许多参数和不同的方法来构建 SSH 配置文件。

    此示例中使用的别名条目的格式为:

    Host alias 
      HostName bitbucket.org 
      IdentityFile ~/.ssh/identity
    

    要为两个身份(workid 和 personalid)创建配置文件,您需要执行以下操作:

    Open a terminal window.
    Edit the ~/.ssh/config file. 
    

    如果您没有配置文件,请创建一个。
    为每个身份组合添加别名,例如:

    Host workid
    HostName bitbucket.org
    IdentityFile ~/.ssh/workid
    
    Host personalid
    HostName bitbucket.org
    IdentityFile ~/.ssh/personalid
    

    【讨论】:

    • 不幸的是不起作用。我认为这里的问题是您提供的解决方案不能满足eval $(ssh-agent) 的要求,我也需要这样做......
    • 如果需要启动 ssh 代理 'eval $(ssh-agent)',请注意
    • 是的,但我的问题实际上是如何配置系统,这样用户就不必每次都做eval $(ssh-agent)...
    猜你喜欢
    • 2017-10-12
    • 2011-01-27
    • 2016-08-20
    • 2014-06-26
    • 1970-01-01
    • 1970-01-01
    • 2019-08-24
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多