How to Use X Forwarding to Run GUI Apps via SSH

X Forwarding
(Image credit: Tom's Hardware)

If you know how to use SSH, you’ve probably employed it for connecting to the command prompt on a remote machine. But did you know that, using a process known as X forwarding, SSH can open remote GUI applications on your desktop? 

Sometimes we need to run a GUI application on a remote machine. We could use VNC, which would send the entire desktop over a network connection. Or we could just forward the application using X forwarding which reroutes the display from the remote machine, over SSH to an X server on our machine. This brings the remote application to our working desktop, instead of it being trapped inside a VNC window.

Here’s how to use X forwarding via SSH.

How to use X Forwarding in Windows

1. Download Cygwin to your Windows machine. Cygwin is a collection of Open Source tools which provide similar functionality to Linux distros.

2. Install Cygwin and click Next to navigate through the installation process.

(Image credit: Tom's Hardware)

3. Choose to install from the Internet and click Next.

(Image credit: Future)

4. Install Cygwin for all users and then click Next.

(Image credit: Tom's Hardware)

5. Select a location to store downloaded packages and click Next.

(Image credit: Tom's Hardware)

6. Select the Internet connection settings that match your setup. System Proxy or Direct Connection are the most likely.

(Image credit: Tom's Hardware)

7. Select a mirror for your download then click Next. Some mirrors are faster than others, but there is no way to know that here. We chose the top most https mirror.

(Image credit: Tom's Hardware)

8. Wait for the downloads to finish, then click Next.

(Image credit: Tom's Hardware)

9. Select X11 from the list, then click on the dropdown menu and select Install. Finally click Next. This will download all of the X11 tools, including those necessary for X forwarding.

(Image credit: Tom's Hardware)

10. Confirm the list of tools to install and click Next to start the installation.

(Image credit: Tom's Hardware)

11. The install process will take some time, when done click Next to move onwards.

(Image credit: Tom's Hardware)

12. Create an icon and Start Menu entry, then click Finish to close the installation script.

(Image credit: Tom's Hardware)

13. Open the Settings menu and search for Firewall. We need to open a port for the connection from our remote device to the X server running on Windows.

(Image credit: Tom's Hardware)

14. Click on “Allow an app through the firewall.”

(Image credit: Tom's Hardware)

15. Click on Change Settings and scroll down to Xming X Server and set it to allow over a Private network. Click OK to save.

(Image credit: Tom's Hardware)

16. From the Start Menu, open XLaunch.

(Image credit: Tom's Hardware)

17. Select Multiple Windows and click Next. This means that every application opened via SSH will have its own window.

(Image credit: Tom's Hardware)

18. Click on Start no client and click Next. This will just start the X Server, nothing else.

(Image credit: Tom's Hardware)

19. Ensure that Clipboard and Native OpenGL are ticked. Then click Next.

(Image credit: Tom's Hardware)

20. Click on Finish to start the X Server. You can save the configuration if you wish, but it is not essential.

X Forwarding

(Image credit: Tom's Hardware)

The X Server is now running and in the System Tray.

(Image credit: Tom's Hardware)

21. Open PuTTY and enter the username and hostname / IP address of the remote device, in this case a Raspberry Pi.

X Forwarding

(Image credit: Tom's Hardware)

22. From the Category menu, click on SSH, and then X11.

(Image credit: Tom's Hardware)

23. Tick “Enable X11 Forwarding” and then click Open to start the SSH connection.

(Image credit: Tom's Hardware)

24. Enter your password and press Enter. Note that this will not echo your keystrokes, this is a security feature.

25. From the SSH session, run a command for a GUI application. In our example we launched Geany, a lightweight text editor. The app will take a few moments to open, but once ready we can work on files and directories as if we were sitting at the remote machine.

(Image credit: Tom's Hardware)

How to Use X Forwarding in Linux

Linux users have a much easier time with X forwarding, as they already have some form of X server installed on their machine.

1. Open a terminal and SSH into the remote machine. Add “-X” to the end of the command to use X forwarding. We can place the -X at the very end or just after “ssh”m the syntax is pretty loose in this case.

ssh -X username@remotemachine.local

(Image credit: Tom's Hardware)

2. From the SSH session, run a command for a GUI application. In our example we launched Geany, a lightweight text editor. The app will take a few moments to open, but once ready we can work on files and directories as if we were sitting at the remote machine.

(Image credit: Tom's Hardware)

How to Use X Forwarding in macOS

Apple’s macOS shares lots of Unix / BSD tools, but in order to use X forwarding we need to install some extra software.

1. Download XQuartz.

2. Open the XQuartz DMG file in your Downloads folder.

(Image credit: Tom's Hardware)

3. Double click on the XQuartz.pkg file to start the installer.

(Image credit: Tom's Hardware)

4. Click Continue to move through the installation process.

(Image credit: Tom's Hardware)

5. Click Continue and agree to the License.

(Image credit: Tom's Hardware)

6. Set your install location and click Install, then Continue Installation. Note that you will be logged out of your session and all other applications will be closed.

(Image credit: Tom's Hardware)

7. Reboot your mac and then log back in to macOS. This isn’t strictly necessary but it provides us with a fresh start, and an opportunity for backend configuration files to be correct.

8. Open a terminal and use the SSH command along with “-X” to connect to the remote machine. Note that you will need to use the IP address, we were unable to use the hostname of the device.

ssh -X username@ip address

(Image credit: Tom's Hardware)

9. From the SSH session, run a command for a GUI application. In our example we launched Geany, a lightweight text editor. The app will take a few moments to open, but once ready we can work on files and directories as if we were sitting at the remote machine.

(Image credit: Tom's Hardware)
Les Pounder

Les Pounder is an associate editor at Tom's Hardware. He is a creative technologist and for seven years has created projects to educate and inspire minds both young and old. He has worked with the Raspberry Pi Foundation to write and deliver their teacher training program "Picademy".

  • w_barath
    This was fine in the 90's when X protocol was lightweight. Back then we didn't have huge truecolour bitmaps, 100MB fonts, client-side rendered widgets, etc. Also, apps were being built to run on servers and have their display on your workstation, so developers were actually considering the X protocol bandwidth.

    Today, running modern GTK or KDE apps over a forwarded X session is really not an option. Not only is the UI using orders of magnitude more data, but it's expecting to animate it with a local GPU, which doesn't get forwarded!

    You're infinitely better off using Xpra instead. Xpra runs your app in a nested X server on the remote host, and then pipes its video and audio back to your thin client using modern audio/video codecs like HEVC/Opus, and it lets you scale the app, set the quality/framerate tradeoffs to achieve good latency and bandwidth.

    Also, if your network isn't 100% reliable, you don't crash the application. You can power cycle your thin client, reconnect to the app later and it will be exactly how you left it.

    If you absolutely must use X11 forwarding, use NXclient protocol 4 or later. It uses differential X protocol coding, compression, and caching to make the experience much more usable.
    Reply
  • icmn223
    For the Windows case, it's a million times easier to use MobaXterm. Like 3 simple steps vs. 25, just sayin':
    Install MobaxTerm.
    Use MobaXterm to SSH to your target.
    Launch GUI app from your SSH session.
    Reply
  • otalado
    Admin said:
    Forwarding an X session over SSH brings a remote GUI application to your desktop, so now all of your apps are in one place, and not in random VNC sessions.

    How to Use X Forwarding to Run GUI Apps via SSH : Read more

    I was always remotely accessing my RPIs and an Intel Linux PC using the RDP protocol. So when I saw your article, I gave X session forwarding a try. The first glitch was after I installed the Cygwin. There was no Xming X Server offered to be let through the firewall. So I temporarily disabled it and proceeded. But after I configured the putty and started the SSL session, the whole thing worked as if I did not have an X server available. I removed the Cygwin and retried the whole thing just to make sure I did not do something wrong or missed anything with the same result.

    Then I saw the icmn223 suggestion about using MobaxTerm. I tried it and it worked on the first attempt. It even detected that it needs to be let through the firewall and offered to install the rule for it.

    Later I'll also try the w_barath suggestion about using the Xpra and decide which to use if there are any differences worth mentioning.
    Reply
  • otalado
    Ok, I've tried to install just the Xming, rather than installing the Cygwin (and stuff). No issues with the firewall. I was able to start things like the lxterminal, pcmanfm. But I wasn't able to start the VS Code and the Chromium browser. So, until I figure out how & why I vote for the MobaxTerm .
    Reply