I'm trying to connect to a remote server using Golang SSH package, but there's a SOCKS between my workstation and this remote server.
I'm able to connect to the server by simply setting a LD_PRELOAD and LD_LIBRARY_PATH and then running:
$ export LD_PRELOAD="/path/to/lib"
$ export LD_LIBRARY_PATH="/path/to/lib"
$ ssh user@hostname
But when I set these variables within the Go code, it doesn't work:
os.Setenv("LD_PRELOAD", "/path/to/file")
os.Setenv("LD_LIBRARY_PATH", "/path/to/file")
If I set these variables within the Go code and try the following, it works:
ssh := exec.Command("ssh", "hostname")
output, _ := ssh.Output()
fmt.Println(string(output))
The ssh PermitUserEnvironment is set as yes
Is there any way to "force" the Golang SSH to use these environment variables?
(Edit: This answer does not necessarily apply to Go 1.8 and above. See comments for discussion)
LD_PRELOAD
and LD_LIBRARY_PATH
are environment variables processed by the dynamic linker when a program is starting up. If you set those environment variables inside the program, they don't have an effect on the program itself since the linker didn't see them.
On the other hand, the environment variables will affect external applications that you run (the ssh
command for example) from within the program, since the linker is given control to resolve the shared libraries that the application uses.
If you instead set those environment variables before running the Go program, I think you'll have the desired effect. (This is only applicable if the compiled program is linked to the shared standard C library. See @JimB's comments below for more details.)