My setup is weird and I can't change it now. I have two machines:

  • local-machine: it's my desktop running Ubuntu with Gnome
  • remote-machine: it's one virtual machine, also running Ubuntu but without X

In both machines I have my private and public SSH keys.

I need to run SSH from remote-machine to local-machine and run gedit (in local-machine, under the default $DISPLAY) but openning a file in remote-machine throught SFTP. Something like this:

myuser@remote-machine:~$ ssh local-machine "DISPLAY=:0.0 gedit sftp://remote-machine/some/file"

The command above doesn't work. gedit shows this message:

Could not open the file sftp://remote-machine/some/file.
gedit cannot handle sftp: locations.

Note that:

  • /some/file exists on remote-machine.
  • I can SSH normally from remote-machine to local-machine using my SSH key without any problems!
  • I can run the command DISPLAY=:0.0 gedit sftp://remote-machine/some/file in a terminal on local-machine and gedit opens the file on remote-machine without any problems - but the terminal in which I executed the command is running in DISPLAY :0 (really, it's gnome-terminal).
  • I also tried -t option of SSH client (to force pseudo-tty allocation) but it didn't work.
  • If I try to run DISPLAY=:0.0 gedit sftp://remote-machine/some/file in local-machine but under a tty (for example in tty1, by pressing <Ctrl>+<Alt>+<F1>) it doesn't not work - I get the same error when running from remote-machine.

I found that if I pass the environment variable DBUS_SESSION_BUS_ADDRESS with a correct value, it works! So, if I do something like that:

myuser@local-machine:~$ env | grep DBUS_SESSION_BUS_ADDRESS > env.txt
myuser@local-machine:~$ scp env.txt remote-machine:

and then:

myuser@remote-machine:~$ ssh local-machine "DISPLAY=:0.0 $(cat env.txt) gedit sftp://remote-machine/some/file"

it works! The problem is that I'm not on local-machine so I can't get the correct value for this env variable. Is there any other way to make this work?


I found another way to get environment vars instead of passing the entire env vars: send the PID of the process which started SSH session from local to remote to the remote machine, so we can get all the environment vars from that PID later.

When I run SSH from remote to local machine, I execute a command to get the environment from that PID (using /proc/<PID>/environ) and then execute the command wihtin that environment. Note that this works only with Linux (or OSes that have /proc).

It's something like that:

Sends the PID of running bash on local machine to a file on remote:

user@local-machine:~$ ssh other-user@remote-machine "echo $$ > \$HOME/.local_bash_pid"

Create the connection:

user@local-machine:~$ ssh other-user@remote-machine

Run SSH on remote to execute a command on local:

other-user@remote-machine:~$ ssh user@local-machine "source <(cat /proc/$(cat $HOME/.local_bash_pid)/environ | tr '\0' ' ') && command-I-want-to-execute-on-that-environment"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.