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
| improve this answer | |

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
| improve this answer | |

Thanks a lot @rudimeier for your answer.

I had a problem with nc option when using ssh, but this variation (based on what @aDroid explained here) did work for me. So this is essentially the same one-liner answer of @rudimeier for newer versions of OpenSSH:

$ ssh -X -oProxyCommand="ssh userA@host.address.A -W %h:%p" userB@host.address.B

@user4556274 answer did also work for me (thanks to you as well).

I just would add usernames in both steps to clarify how it works to newbies like me (after typing 1st command, localhost port 2222 will behave as if it was port 22 in machine B; so in 2nd command we pass username of machine B):

$ ssh -Nf -L2222:host.address.B:22 userA@host.address.A
$ ssh -p 2222 -X userB@localhost
| improve this answer | |

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.