When working with a Raspberry Pi, you may occasionally need to run graphical applications remotely, that is, from a machine different from the one running the software.
In these cases, it is not common to use a full remote desktop like VNC. However, there are certain situations where we cannot connect via a full remote desktop. For example, due to lack of permissions or not wanting to install additional software on the machine.
In these cases, X11 port forwarding can be an interesting alternative to redirect the graphical interfaces of your applications to your local device, allowing you to work with them as if they were running locally.
What is X11 Port Forwarding
X11 port forwarding is a technique that allows redirecting the graphical interface of applications running on a remote server to a local client. It uses the X Window System (X11) to display the graphical interface of applications on a remote machine.
X11 port forwarding via SSH (Secure Shell) allows redirecting X11 connections through a secure SSH tunnel. It is commonly used to run graphical applications from remote servers on Unix and Linux systems.
Prerequisites
Before getting started, make sure that:
- Raspberry Pi has an X11 server installed and configured.
- SSH Client (like
OpenSSH
) is installed on your local machine and on the Raspberry Pi. - X Server (like
XQuartz
on macOS orXming
on Windows) is installed on your local machine if you are using an operating system that does not include an X11 server by default.
Configuring X11 Port Forwarding
First, ensure that the SSH server on your Raspberry Pi is configured to allow X11 forwarding. We will check and modify this configuration in the SSH configuration file (/etc/ssh/sshd_config
).
Open the SSH configuration file with the following command:
sudo nano /etc/ssh/sshd_config
Make sure the following lines are uncommented and configured correctly:
X11Forwarding yes
X11DisplayOffset 10
Finally, save the changes and restart the SSH service to apply the new configuration with:
sudo systemctl restart ssh
Running Graphical Applications
On the other hand, the client machine must run an SSH application that is compatible with X11 Port Forwarding. Most of the ones we have seen, like Putty, mRemoteNG, and MobaXTerm are all compatible.
For example, in the case of Putty, it is an option that we need to check in Connection/SSH/X11.
Once connected, you can run graphical applications from the command line of your Raspberry Pi, and their interfaces will appear on your local machine. To test X11 Port Forwarding, you can run any application that has a graphical interface.
Or, if you want to do a test, run the X11 apps.
sudo apt install x11-apps
And then,
xclock &