bash脚本为sudo但用户变量

时间:2013-01-26 03:39:46

标签: bash shell

我有一个需要用sudo调用的脚本,但有一部分我必须克隆一个GIT仓库git clone foo:repo path/to/files,为此,它需要我的普通用户ssh密钥,这是所有在我的常规用户$HOME/.ssh/config文件中定义。

.ssh/config:
host foo
    HostName foo.com
    User myuser
    IdentityFile ~/.ssh/id_rsa

有什么方法可以在调用此脚本时保留我的ssh配置数据,以便克隆repo?我尝试将-E标志传递给sudo,但它似乎没有任何效果。

1 个答案:

答案 0 :(得分:0)

所有信用都归于@ that-other-guy从评论到原帖。

而不是:

if [[ 'svn' == $repoType ]]; then
    svn co ...
elif [[ 'git' == $repoType ]]; then
    git clone ...
fi

由于我是sudo-ing,导致我的克隆无法访问我的用户.ssh /我的密钥配置数据

切换到:

if [[ 'svn' == $repoType ]]; then
    (sudo -u $SUDO_USER svn co ...)
elif [[ 'git' == $repoType ]]; then
    (sudo -u $SUDO_USER git clone ...)
fi