Home Secure Shell (SSH) How To Configure X11 Forwarding Using SSH In Linux

How To Configure X11 Forwarding Using SSH In Linux

By sk
Published: Updated: 171.6K views

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
Configure X11 Forwarding Using SSH In Linux
Configure X11 Forwarding Using SSH In Linux

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 root@192.168.225.52

Here, -X option is used to enable X11 forwarding over SSH. Replace "root@192.168.225.52" 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

However, the .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:

# gedit

The gedit GUI window screen will now appear in your local system.

Run remote graphical applications over SSH session in Linux
Run remote graphical applications over SSH session in Linux

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:

Connect to remote system using Putty
Connect to remote system using Putty

Next, navigate to Connection -> SSH -> X11 and enable X11 forwarding option. Click Open to establish a new Putty session:

Configure X11 forwarding in Putty
Configure X11 forwarding in Putty

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.

Run graphical applications over Putty session
Run graphical applications over Putty session

Start interacting with the remote GUI application from your local system!

You May Also Like

13 comments

BC September 13, 2020 - 3:35 am

How does typing ‘cat .Xauthority’ “create” a file with text in it?
What if ‘echo $DISPLAY’ doesn’t return anything?

Reply
sk September 13, 2020 - 2:31 pm

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.

Reply
Jim Mills February 6, 2021 - 6:43 am

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.

Reply
sk February 6, 2021 - 12:32 pm

Good catch. Fixed now. Thank you.

Reply
Garima Raj July 6, 2021 - 7:28 pm

Thank you very much. It was the easiest tutorial I could find on X11Forwarding. I really appreciate it.
Thanks again

Reply
Rose March 10, 2022 - 2:03 am

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

Reply
sk March 10, 2022 - 12:05 pm

We haven’t tested this guide on Windows. Sorry, We couldn’t help at the moment.

Reply
Joe May 11, 2022 - 8:38 pm

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?

Reply
sk May 13, 2022 - 3:33 pm

Never heard about attachmate. So I can’t comment about it. As far as I tested, SSH and Putty works fine for me.

Reply
Michael Bolton July 20, 2022 - 4:15 am

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.

Reply
Bacon Ninja October 5, 2022 - 5:21 am

Need to edit sshd_config, not ssh_config.

Reply
sk October 5, 2022 - 12:53 pm

in CentOS 8, it is ssh_config.

Reply
Paul July 10, 2023 - 11:26 pm

Thankyou…. This is the first page I’ve found that has a clear guide of how X11 works…
So all I have to do is install a GUI on the remote server.

Reply

Leave a Comment

* By using this form you agree with the storage and handling of your data by this website.

This site uses Akismet to reduce spam. Learn how your comment data is processed.

This website uses cookies to improve your experience. By using this site, we will assume that you're OK with it. Accept Read More