Debugging "sudo go run main.go" Execution Error on Ubuntu
Executing "sudo go run main.go" in a Ubuntu 16.04 environment often leads to the error "executable file not found in $PATH." This stems from the fact that environment variables are not preserved for the root user by default. To address this issue, follow the steps below:
Disable Environment Reset:
1. Edit the file /etc/sudoers.
2. Find the line that reads "Defaults env_reset" and change it to "Defaults !env_reset".
However, this method may not resolve the problem.
Correct Approach:
The recommended approach is to build the binary without sudo and execute the binary with sudo. Steps to do so:
1. Navigate to the directory containing main.go.
2. Build the binary using go build.
3. Execute the binary using sudo, e.g.:
sudo ./mycapt
Alternatively, you can use go install and then execute the binary using:
sudo $GOPATH/bin/mycapt
By following these steps, environment variables will be preserved and you will be able to successfully execute the Go binary with sudo permissions.
The above is the detailed content of Why Does \'sudo go run main.go\' Fail on Ubuntu?. For more information, please follow other related articles on the PHP Chinese website!