如何使用非root用户为git部署配置ssh代理服务和密钥?

时间:2016-01-18 22:20:35

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

我在部署时遇到问题(使用非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.

2 个答案:

答案 0 :(得分:0)

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

Host hostname
 IdentityFile ~/.ssh/deploymapr

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

答案 1 :(得分:0)

创建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