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

How to configure ssh agent service and keys for git deployments using non-root user?

我在部署时遇到问题(使用非根用户):

$> 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.

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

Host hostname
 IdentityFile ~/.ssh/deploymapr

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

创建 SSH 配置文件

Create a SSH config file mechanisms to create aliases for your various identities.

You can construct a SSH config file using many parameters and different approaches.

The format for the alias entries use in this example is:

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

To create a config file for two identities (workid and personalid), you would do the following:

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