Go fork/exec permission denied error
Asked Answered
S

8

22

I recently installed Go onto our server with CentOS 6.3. The install appears to have gone fine. However I made a test "hello world" script, and when I run I get the following output.

fork/exec /tmp/go-build967564990/command-line-arguments/_obj/a.out: permission denied

Now running go env or other go commands seem to work. At first I figured it was a permission issue, however running as root user I get the same thing. An

Softener answered 18/2, 2013 at 19:32 Comment(0)
U
18

Just guessing: Your nix perhaps disables for security reasons executing programs in /tmp. It might be configurable in CentOS, but I don't know that.

The alternative solution: It seems you're trying go run to execute a Go program (which is as script as C is a script). Try (assuming $GOPATH=~, the easy possibility) instead a normal build, i.e. instead of

me:~/src/foo$ go run main.go

try

me:~/src/foo$ go build # main.go should not be necessary here
me:~/src/foo$ ./foo

This approach will still use /tmp-whatever to create the binary, IIRC, but it will not attempt to execute it from there.

PS: Do not run these command as root. No need for that with correct setup.

Unesco answered 18/2, 2013 at 19:48 Comment(3)
@user387049, looks like jnml is correct: try running mount and see if the output for /tmp mentions the noexec option.Ours
running mount did show that /tmp had the noexec option. Not wanting to disable it, building and then running fixed the issue. Thanks!Softener
I made a quick Go script that does go build and then execs the program after. I prefer reducing two steps into one:) github.com/rjbishop/gobeGoatish
E
26

I encountered this issue today but the solutions above did not work. Mine was fixed by simply running:

$ export TMPDIR=~/tmp/

then I was able to get the script to run with:

$ go run hello.go
hello, world

The only downside is you have to run export TMPDIR every time you want to run an application.

Kudos to Adam Goforth

Enthymeme answered 22/10, 2014 at 18:20 Comment(2)
you don't need to export every time you want to run.. just put the export on your ~/.profile or ~/.bashrcRoma
if you want to avoid affecting other programs, you can alias go='TMPDIR=~/tmp go'Brassica
U
18

Just guessing: Your nix perhaps disables for security reasons executing programs in /tmp. It might be configurable in CentOS, but I don't know that.

The alternative solution: It seems you're trying go run to execute a Go program (which is as script as C is a script). Try (assuming $GOPATH=~, the easy possibility) instead a normal build, i.e. instead of

me:~/src/foo$ go run main.go

try

me:~/src/foo$ go build # main.go should not be necessary here
me:~/src/foo$ ./foo

This approach will still use /tmp-whatever to create the binary, IIRC, but it will not attempt to execute it from there.

PS: Do not run these command as root. No need for that with correct setup.

Unesco answered 18/2, 2013 at 19:48 Comment(3)
@user387049, looks like jnml is correct: try running mount and see if the output for /tmp mentions the noexec option.Ours
running mount did show that /tmp had the noexec option. Not wanting to disable it, building and then running fixed the issue. Thanks!Softener
I made a quick Go script that does go build and then execs the program after. I prefer reducing two steps into one:) github.com/rjbishop/gobeGoatish
J
2

Instead of settings TMPDIR which might affect other programs, you can set GOTMPDIR:

mkdir /some/where/gotmp
export GOTMPDIR=/some/where/gotmp

Then add export GOTMPDIR=/some/where/gotmp to your profile (i.e. .bash_profile) to make the variable permanent.

Jori answered 8/2, 2023 at 16:13 Comment(0)
B
1

I am using Fedora 31 and got a similar error which brought me here. I could not run the Go debugger used by Jetbrains IntelliJ Ultimate/GoLand without fork/exec & permission denied error. The solution was this:

setsebool deny_ptrace 0

See https://fedoraproject.org/wiki/Features/SELinuxDenyPtrace for details.

Boom answered 3/3, 2020 at 9:1 Comment(0)
L
0

exec.Command return this sturct: type Cmd

standard comment:

// Dir specifies the working directory of the command.  
// If Dir is the empty string, Run runs the command in the
// calling process's current directory.
Dir string

so resolve, you can pass the exec cmd's path to exec yourself command:

cmd := exec.Command(xxxxx)
cmd.Dir = xxxxPath

and then you can call Run() or other Output() func

Ledda answered 17/11, 2021 at 6:36 Comment(1)
Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.Gustie
Z
0

In my case, my GOTMPDIR was already set, but TMPDIR wasnt. So in order to solve this just set the TMPDIR:

export TMPDIR=/tmp

Save after restart

echo 'export TMPDIR=/tmp' >> ~/.bashrc
Zecchino answered 22/3 at 11:46 Comment(0)
F
-2

To fix this issue on my Chromebook I just remounted /tmp as executable. There may be security implications in doing this, but since go run works on other platforms I figure maybe it's not that bad (especially on a local dev machine):

sudo mount -i -o remount,exec /tmp/

I added this to my .bash_profile script.

Fineable answered 29/5, 2016 at 10:25 Comment(0)
F
-4

Consider trying:

sudo mount -o remount exec /tmp
Fuqua answered 13/7, 2013 at 23:55 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.