Ssh – Forward X11 through two SSH tunnels

linuxsshssh-tunnelingx11

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.

Best Answer

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
Related Question