简体   繁体   中英

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:

        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

warning: unable to access '/root/.gitconfig': Permission denied

warning: unable to access '/root/.config/git/config': Permission denied

error: could not lock config file /root/.gitconfig: Permission denied

And then it asks me for my gitlab username and password. 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.

Using SSH keys doesn't seem to affect this. 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.

You should use ssh keys to surprise with 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.

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.

git remote remove origin

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

Obviously adjust the above to fit your respository.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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