Today, we are going to learn a lesser known, interesting and useful feature about Linux. I am sure most of you know about SSH. We can access remote Linux systems securely via SSH. But, did you know that it is possible to forward X over SSH to run graphical applications remotely? Yes! This mechanism is known as X11 forwarding. In this guide, we are going to configure X11 forwarding using SSH in Linux.
What is X11 forwarding?
X11 forwarding is method of allowing a user to start a graphical applications installed on a remote Linux system and forward that application windows (screen) to the local system. The remote system need not to have X server or graphical desktop environment. Hence configuring X11 forwarding using SSH enables the users to securely run graphical applications over SSH session.
To put this in layman terms,
- We connect to a remote system via SSH,
- And then, we launch a GUI application, which is installed in the remote system, from that SSH session,
- Now, the GUI application runs on the remote system, but the application window appears on our local system. So we can use this remote GUI program on your local system as the way we use a locally installed program.
Clear? Now let us go ahead and see how to run remote GUI applications over SSH session.
Configure X11 Forwarding Using SSH In Linux
For the purpose of this guide, I will be using the following two systems:
- Remote server - CentOS 8 minimal server (No GUI).
- Local system - Ubuntu 20.04 LTS with Gnome DE.
Configuring X11 forwarding is trivial.
Make sure you have installed "xauth" on your remote serer system. If it is not installed, run the following command as root or sudo user:
# dnf install xorg-x11-xauth
Go to your remote system (in my case it is CentOS 8) and edit "/etc/ssh/ssh_config" file using your favorite editor:
# nano /etc/ssh/ssh_config
Add/modify following line:
[...] X11Forwarding yes
Save and close the file. Restart sshd service to effect the changes:
# systemctl restart sshd
Next, create file ".Xauthority" using command:
# cat .Xauthority
You will see some random letters in the output like below:
Finally, verify that your "DISPLAY" variable is not empty by running the following command:
# echo $DISPLAY
You should an output like below:
Done! We have configured X11 forwarding.
Now go to your client system (in my case, it is Ubuntu 20.04) and access the remote server via SSH using command:
$ ssh -X [email protected]
Here, -X option is used to enable X11 forwarding over SSH. Replace "[email protected]" with your remote server's username and IP address.
Next, launch any GUI applications from this SSH session. I already have installed Gedit graphical text editor, so I am going to launch it using command:
The gedit GUI window screen will now appear in your local system.
You can interact with the Gedit application installed on the remote server using your local system's display, mouse, and keyboard.
Even though my CentOS 8 server doesn't have GUI desktop, I can be able to open Gedit graphical editor and use it from my local Ubuntu system. Here is the visual demo:
Configure X11 forwarding in Putty
If you use Putty client to access remote systems, enter your remote system's IP or hostname in the Session tab:
Next, navigate to Connection -> SSH -> X11 and enable X11 forwarding option. Click Open to establish a new Putty session:
Enter the username and password of the remote server. Once you connected to the remote system via Putty, launch any X application installed in the remote server.
Start interacting with the remote GUI application from your local system!