To understand my problem, here's my current setup:

I'm working with a user "foo", that user has a $GOPATH as /foo/go/.

This user is in the sudo'ers group.

I am trying to work in a git directory on a go project. If I use sudo I can edit the file, the problem is that if I try to build my project from vim, or run anything from my gopath (such as :GoMetaLinter) then it runs in the context of the root user, since I used sudo, but this user has no gopath set.

I don't really understand how to solve this properly. I've restored to opening the file as read only to use my linter and then using vim to sudo and write, but that's stupid, and I know it's not "correct". What is the proper way for using sudo so my local users context/variables are used and not roots?

  • 2
    Sounds like the directory isn't owned as foo. If so do the work as this user exclusively. That means the dir ownership and the git clone and the running of vim should only be foo. Don't do any sudos in this directory. – slm Jul 1 '18 at 16:36
  • 1
    or if multiple users need write access to the project, Make all new files in a directory accessible to a group – steeldriver Jul 1 '18 at 19:29
  • have you tried or otherwise looked at sudo -E? – GracefulRestart Jul 2 '18 at 23:08

sudo will not keep environment variables due to security concerns. To override this, adding something like

Defaults env_keep += "HOME GOPATH"

to /etc/sudoers should help, listing the environment variables you whish to preserve.

https://stackoverflow.com/questions/8633461/how-to-keep-environment-variables-when-using-sudo#8633575 may contain additional hints.

| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.