For work, I have to ssh from my local machine through Machine A to get to Machine B. The problem is that Machine A doesn't have X11 forwarding, and I don't have superuser privileges to change that. I do have superuser privileges on Machine B, and it has X11 forwarding turned on.

Is there a way to forward X11 from B through A to my local machine, without X11 forwarding turned on on A?

I tried running ssh -t -X user@A ssh -X user@B and ssh -t user@A ssh -X user@B but Machine A still outputs X11 forwarding request failed on channel 0 on login.


add this to your ~.ssh/config:

Host BviaA
  HostName      hostB
  ProxyCommand  ssh user@hostA nc %h %p 2> /dev/null

Then ssh -X user@BviaA should work

Note in the config section above I've used Host BviaA to keep it more obvious what happens. You could also use Host hostB to let it work fully transparent: ssh -X user@hostB (As if no gateway is needed.)

Alternatively as a one-liner for testing without any config changes:

ssh -X -oProxyCommand="ssh user@hostA nc %h %p" user@hostB

You may be able to port-forward through A. Use

$ ssh -Nf -L2222:B:22 A

to start and background an ssh process which will forward from your local machine to B through A. Then connect to B using

$ ssh -p 2222 -X localhost

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.