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
Press Ctrl+O to save and Ctrl+X to close the file. Restart sshd service to effect the changes:
# systemctl restart sshd
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@example.com
Here, -X option is used to enable X11 forwarding over SSH. Replace "firstname.lastname@example.org" with your remote server's username and IP address.
You will see the following warning message at first time.
/usr/bin/xauth: file /root/.Xauthority does not exist
.Xauthority file should be automatically generated during the first login, thus subsequent logins should be fine.
Next, launch any GUI applications from this SSH session. I already have installed Gedit graphical text editor in my CentOS 8 server, 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!
How does typing ‘cat .Xauthority’ “create” a file with text in it?
What if ‘echo $DISPLAY’ doesn’t return anything?
The .Xauthority file should be automatically generated during the first login, thus subsequent logins should be fine. So you don’t need to manually create the Xauthority file as mentioned in the guide. Also ignore the ‘echo $DISPLAY’ command. This was a mistake from my side. I updated the guide. Thanks.
In your instructions you are editing the ssh client config, and then restarting the server. You have to change the server’s config file, sshd_config. Not ssh_config as you stated.
Good catch. Fixed now. Thank you.
Thank you very much. It was the easiest tutorial I could find on X11Forwarding. I really appreciate it.
This makes sense, but it’s still not working for me. I am using putty from a windows PC to linux server. Do I need to install anything on the windows computer other than putty? I use putty with X11 forwarding enabled and the auth file did not get created
We haven’t tested this guide on Windows. Sorry, We couldn’t help at the moment.
I am trying to use attachmate on Linux to connect to a remote server then export the display back to my IP address. I start the script and it does not export the display. Are you saying I can just use Putty for the same purpose or do I have a configuration issue?
Never heard about attachmate. So I can’t comment about it. As far as I tested, SSH and Putty works fine for me.
For Windows you might need VcXsrv for displaying remote gui apps locally on your local Windows OS. Might also need to export DISPLAY=:0 or some IP address ex: export DISPLAY=:192.168.1.1:0 if your Windows IP is 192.168…
With Putty you would setup in setting>Connection>SSH>X11:X11 forwarding checked and X display location set.
Need to edit sshd_config, not ssh_config.
in CentOS 8, it is