hypriot / golang-armbuilds

Pre-compiled GOLANG tarballs for ARM devices.
MIT License
69 stars 12 forks source link

Cannot Run GO as Sudo #6

Closed fracturedpsyche closed 7 years ago

fracturedpsyche commented 8 years ago

Hello! I am having an issue where I cannot run GO as sudo. I am working on a project using bluetooth low energy (which requires running code with root access) and when I run "sudo go run ..." to execute my bluetooth code I get an error:

go: cannot find GOROOT directory: /usr/src/go

When I extracted the arm64 tarball I put it in /usr/local/go.

DieterReuter commented 8 years ago

What about setting GOROOT explicitly with GOROOT=/usr/local/go sudo go run ... or just in your shell or .bashrc with export GOROOT=/usr/local/go?

fracturedpsyche commented 8 years ago

That does not work either. Trying to do anything with go as sudo results in the same error. My test is to run "sudo go version" and that returns the same error.

DieterReuter commented 8 years ago

Strange. And when you just compile your GO code first with go build . And then running the binary with sudo?

fracturedpsyche commented 8 years ago

That does not work either. I have also tried installing go to the directory that sudo is expecting. When I do that, I can execute go commands, but I cannot build or run anything because sudo does not see GOPATH.

DieterReuter commented 8 years ago

Can you please give a little be more details about your device? Device type, OS, etc. You can also try to compile the latest Go 1.6.3 from source on this device, just following my tutorial at http://blog.hypriot.com/post/how-to-compile-go-on-arm/. Or looking for help at the GOLANG team, they do have now GO tarballs available at least for ARMv6 and then compile GO 1.6.3 from source again.

fracturedpsyche commented 8 years ago

I am actually going to rebuild the OS on this machine and start fresh. I will report my status after that and if I am still having issues I will post more specifics.

firecyberice commented 8 years ago

As sudo opens up a new subshell with root privileges: sudo GOROOT=/usr/local/go go version should work ;-) (pay attention to the position of sudo)

fracturedpsyche commented 8 years ago

OK. I have an update. I completely rebuilt the board I am using. I flashed the OS, downloaded the 1.6.2 arm64 build from your releases, extracted to /usr/local/go.

When I try to execute "go version" as any user I get "go: cannot find GOROOT directory: /usr/src/go".

I have not made any changes other than what I typed here. I have never attempted to set GOROOT. I exported the PATH and that is all.

Here is what I am working on:

Qualcomm Dragonboard 410c running the supported version of Linaro Linux.

firecyberice commented 8 years ago

Please post the output of printenv and sudo printenv By the way have you extended your PATH variable as described https://golang.org/doc/install

fracturedpsyche commented 8 years ago

@firecyberice your solution from 3 hours ago seems to work for sudo. Kind of a pain but it is what it is. Thank you both for your help!

roylee17 commented 8 years ago

Some updates to /etc/sudoers should work around this nicely.

  1. sudo overrides PATH with the "secure_path" when it execute the command. Include your path in secure_path, if it's not already there:
Defaults secure_path="$YOUR_GO_BIN_DIR:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin"
  1. Keep your GOPATH with the env_keep option:
Defaults env_keep += "GOPATH"
danesparza commented 8 years ago

If anybody is interested, you can automate this with a script (for CI) using the following syntax (note the tee command -- it's important):

echo 'Defaults env_keep += "GOPATH"' | sudo tee --append /etc/sudoers.d/env_keep

firecyberice commented 7 years ago

Seems to be solved. So I close this issue.