systemclt services and ssh



我有一个简单的bash脚本,可以在github(/home/user/simple_git.sh)上呼叫git存储库:

#!/bin/bash
# Change to the Git repoistory
cd /home/user/git/a_git_repo
remote=$(
    git ls-remote -h origin master |
    awk '{print $1}'
)
local=$(
    git rev-parse HEAD
)
printf "Local : %snRemote: %sn" $local $remote

它给出以下输出:

Local : a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b
Remote: a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b

git身份验证是通过ssh键完成的 - 以下是我的.bashrc

# ssh
eval `ssh-agent -s`
ssh-add

脚本运行良好,就像用户一样(通过保留用户环境),即。

~/.simple_git.sh
or
sudo -E ~/simple_git.sh

但是,我尚未找到一种将脚本运行的方法(/etc/systemd/user/simple_git.service or/etc/systemd/systemd/system/simple_git.service.service)

[Unit]
Description=TestScript
[Service]
Type=simple
ExecStart=/home/user/simple_git.sh

我已经尝试使用-user选项运行SystemCtl命令,并修改Visudo以包括行

Defaults env_keep += SSH_AUTH_SOCK

,但无济于事。每次我检查工作状态:

Feb 21 23:16:00 alarmpi systemd[484]: Started TestScript.
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: Permission denied (publickey).
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: fatal: Could not read from remote repository.
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: Please make sure you have the correct access rights
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: and the repository exists.
Feb 21 23:16:01 alarmpi simple_git.sh[15255]: Local : a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b

SystemD未从会话中使用环境变量运行服务。我建议您

  • 使用 https使用git,它不需要身份验证(而不是 ssh
  • 创建一个未保护的"部署密钥",该密钥将在标准位置(~alarmpi/.ssh/id_rsa),该位置将自动在没有ssh-agent的情况下自动拾取git

目前(工作练习策略)无法使用HTTPS连接到GitHub。虽然不受保护的部署键的想法很有用,但我最终使用systemctl -user import-environment(wiki.archlinux.org/index.php/systemd/user)来管理这个问题。

<</p> <</p>

最新更新