github - systemctl 服务和 ssh

标签 github ssh systemd systemctl

我有一个简单的 bash 脚本,它调用 github 上的 git 存储库(/home/user/simple_git.sh):

#!/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 : %s\nRemote: %s\n" $local $remote

它提供以下输出:
Local : a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b
Remote: a10dc1d7d30ed67ed1e514a3c1ffc5a824cea14b

git 身份验证是通过 ssh key 完成的 - 以下是我的 .bashrc
# ssh
eval `ssh-agent -s`
ssh-add

该脚本作为用户以及使用 sudo (通过保留用户环境)运行得很好,即。
~/.simple_git.sh
or
sudo -E ~/simple_git.sh

但是,我还没有找到将脚本作为服务运行的方法(/etc/systemd/user/simple_git.service 或/etc/systemd/system/simple_git.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 未使用 session 中的环境变量运行服务。我会建议你

  • 使用 git 使用 https ,不需要身份验证(而不是 ssh )
  • 创建一个未 protected “部署 key ”,它将位于标准位置 (~alarmpi/.ssh/id_rsa),将由 git 获取自动没有 ssh-agent .
  • 关于github - systemctl 服务和 ssh,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42380133/

    相关文章:

    github - 使用 install_github 在分支上安装包时出错

    Mercurial-server:ssh 需要密码

    git - 使用 Git 重命名文件

    github - golang 中非原子类型的类型包装

    ssh - 如何防止 Vagrant 询问 ssh key 密码

    java - 达到较低的 Java 线程限制(503 个线程)

    ubuntu -/vagrant 挂载后如何运行 nginx.service

    linux - TYK Dashboard 和 Gateway 环境变量的使用

    python - 无法使用 git 安装私有(private) python 包

    python - ssh 到服务器并使用 Python 执行等效的curl