简体   繁体   English

在 sudo -u 下运行时记住 Git 的密码

[英]Remembering Password for Git when run under sudo -u

I have a small bash script which pulls down a Laravel git instance from gitlab.com: I have a small bash script which pulls down a Laravel git instance from gitlab.com:

        sudo -u www-data git config --global credential.helper store
        sudo -u www-data php artisan down
        sudo -u www-data git pull
        sudo -u www-data composer install --no-dev --no-interaction --prefer-dist --optimize-autoloader

When I run this (as root or any other user), I get the error当我运行它时(作为 root 或任何其他用户),我得到了错误

warning: unable to access '/root/.gitconfig': Permission denied警告:无法访问“/root/.gitconfig”:权限被拒绝

warning: unable to access '/root/.config/git/config': Permission denied警告:无法访问“/root/.config/git/config”:权限被拒绝

error: could not lock config file /root/.gitconfig: Permission denied错误:无法锁定配置文件 /root/.gitconfig:权限被拒绝

And then it asks me for my gitlab username and password.然后它询问我的 gitlab 用户名和密码。 If entered correctly, the script works as expected.如果输入正确,脚本将按预期工作。 How do I force my script to remember the username and password - or should I be going about this a different way?我如何强制我的脚本记住用户名和密码 - 或者我应该以不同的方式来解决这个问题? Currently I'm logging into a server as root or another sudoer created for this purpose, because the www-data user isn't or shouldn't be permitted to log in, as I understood.目前,我正在以 root 或为此目的创建的另一个 sudoer 身份登录服务器,因为据我所知,www-data 用户不允许或不应该被允许登录。

Using SSH keys doesn't seem to affect this.使用 SSH 密钥似乎不会影响这一点。 They are set up correctly on my account, but I run:它们在我的帐户上正确设置,但我运行:

root@andach-admin-demo:/var/www/html/demo# git pull
warning: redirecting to https://gitlab.com/andach/andach-admin.git/
Already up to date.
root@andach-admin-demo:/var/www/html/demo# sudo -u www-data git pull
Username for 'https://gitlab.com':

I don't understand how to do this with using sudo -u www-data - I think that's the issue.我不明白如何使用 sudo -u www-data 来做到这一点 - 我认为这就是问题所在。

You should use ssh keys to surprise with GitLab.您应该使用 ssh 密钥来惊喜 GitLab。

The SSH protocol provides this security and allows you to authenticate to the GitLab remote server without supplying your username or password each time. SSH 协议提供了这种安全性,并允许您向 GitLab 远程服务器进行身份验证,而无需每次都提供您的用户名或密码。

https://docs.gitlab.com/ee/ssh/ https://docs.gitlab.com/ee/ssh/

Now you have updated your question and you have moved to using SSH keys, you also have to update your git remote to use the SSH URL which is different from the HTTPS URL. Now you have updated your question and you have moved to using SSH keys, you also have to update your git remote to use the SSH URL which is different from the HTTPS URL.

git remote remove origin

git remote add origin git@gitlab.com:<username>/<respository>.git

Obviously adjust the above to fit your respository.显然调整以上内容以适合您的存储库。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM