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.

  • I got A and B switched around even after trying to read carefully. Maybe naming them "middle (A)" and "destination (B)", dropping the A,B letters in your code, would be even clearer and not mess up the answers below. Or like Abu's answer below
    – CPBL
    Commented Mar 10, 2022 at 19:40

3 Answers 3


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

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 [email protected] -W %h:%p" [email protected]

@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:destination.host-B.address:22 [email protected]
$ ssh -p 2222 -X userB@localhost
  • 1
    I got A and B switched around even after trying to read carefully. Maybe naming them "middle" and "destination" would be even clearer?
    – CPBL
    Commented Mar 9, 2022 at 1:06
  • I agree: done. But you should also tell that to original poster. I just followed the same naming as everybody else (used in OP's question, and also followed in all remaining answers)
    – abu
    Commented Mar 10, 2022 at 17:02
  • Oh, right. Done. Thanks.
    – CPBL
    Commented Mar 10, 2022 at 19:42

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

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .