Hey Everyone,
I am having trouble with running exec.Command() as a different user. I have tried a few different users, so I do not think that is the problem. The error I am getting is "fork/exec /bin/date: operation not permitted". I tried googling around, but couldn't find any mention of this exact problem. I have tried a few different simple unix commands other than date as well. Also, not sure if it is relevant, but I am using
Ubuntu 14.04 and
go version go1.4 linux/amd64.
Any ideas?
Stackoverflow Answer I followed to get here:
http://stackoverflow.com/questions/21705950/running-external-commands-through-os-exec-under-another-userCode:
package main
import (
"fmt"
"os/exec"
"os/user"
"strconv"
"syscall"
)
func main() {
cmd := exec.Command("date")
myuser, _ := user.Current()
uid, _ := strconv.Atoi(myuser.Uid)
gid, _ := strconv.Atoi(myuser.Gid)
cmd.SysProcAttr = &syscall.SysProcAttr{}
cmd.SysProcAttr.Credential = &syscall.Credential{Uid: uint32(uid), Gid: uint32(gid)}
out, err := cmd.Output()
if err != nil {
fmt.Printf("Run Command got an Error: %s\n", err)
return
}
fmt.Println(out)
}
Pastebin link:
http://pastebin.com/WetNsnvHFinal Output:
Run Command got an Error: fork/exec /bin/date: operation not permitted
Thanks so much,
AJ