Home > Backend Development > Golang > Why Does \'sudo go run main.go\' Fail on Ubuntu?

Why Does \'sudo go run main.go\' Fail on Ubuntu?

Patricia Arquette
Release: 2024-11-16 11:11:03
Original
532 people have browsed it

Why Does

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
Copy after login

Alternatively, you can use go install and then execute the binary using:

sudo $GOPATH/bin/mycapt
Copy after login

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!

source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Articles by Author
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template