Shell – Understanding proper sudo usage to use local user $GOPATH

shellsudovim

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?

Best Answer

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.

Related Question