Understanding X11-Forwarding: Enabling Remote GUI Access on Linux Servers

ssh client Remotely Use Pycharm on Linux Host with GUI

While modifying the sshd configuration file, I stumbled upon the X11-forwarding option. This option supports remotely invoking graphical interface programs on a Linux host. I used to have a misconception that GUI programs could only run on Linux systems with a graphical interface installed, and that Linux Servers without a GUI inherently did not support GUI programs. It wasn’t until I discovered X11-forwarding that I realized this idea was incorrect. Because the graphical interface on Linux is merely software, not supported by the system kernel. Since it is software, any Linux system can install and implement a graphical interface.

What is X11-forwarding?

The X in X11 refers to the X protocol; 11 refers to the 11th version of the X protocol.

In simple terms, X11-forwarding allows you to use an SSH client that supports X Server, such as MobaXterm, to connect to a remote Linux server and run graphical interface programs on the remote Linux server locally through MobaXterm.

Overview of X Protocol Principles

Linux itself does not have a graphical interface; the so-called graphical interface system is just an application under Linux. This is different from Windows. Since Windows 95, the graphical interface has been implemented directly in the system kernel and is an indispensable part of the operating system. The graphical interface of Linux is based on the X protocol at the bottom layer.

 X11-forwarding

The X protocol consists of X server and X client:

The X server manages the hardware settings related to display on the host (such as graphics card, hard disk, mouse, etc.), it is responsible for drawing and displaying the screen, and notifying the X client of input settings (such as keyboard and mouse) actions. The X client (i.e., X application) is mainly responsible for event handling (i.e., program logic).

Test Environment

  • centos7 Server (any CVM opened on Tencent Cloud)
  • MobaXterm v21.5 (xshell can also be used, theoretically any SSH client that supports X11-forwarding can be used)

Initially, I wanted to use weterm, but unfortunately, it does not support X11-forwarding.

Enable X11-forwarding in sshd_config

 X11-forwarding vim /etc/ssh/sshd_config

 systemctl restart sshd    // Restart sshd after modification to take effect

Install xorg* Graphical Programs and Dependencies

yum install xorg-x11-xauth xorg-x11-fonts-* xorg-x11-font-utils xorg-x11-fonts-Type1 xclock -y

Execute xclock to test if successful

Install Firefox with X11-forwarding

 yum install -y  firefox dbus-x11
yum groupinstall 'Fonts'

Install QQ Music

 wget https://dldir1.qq.com/music/clntupate/linux/AppImage/qqmusic-1.1.3.AppImage &&chmod +x qqmusic-1.1.3.AppImage
./qqmusic-1.1.3.AppImage --no-sandbox

Running QQ Music on SSH client

Install Pycharm for Coding

It was commonly believed that running an IDE on a non-desktop Linux environment was unlikely. However, testing shows that it is indeed possible to run an IDE on Linux.

 wget https://download.jetbrains.com.cn/python/pycharm-professional-2021.3.2.tar.gz && tar xvf pycharm-professional-2021.3.2.tar.gz
cd ./pycharm-2021.3.2/bin && chmod +x pycharm.sh
./pycharm.sh

Conclusion

Any Linux application on a virtual machine or remote computer can be connected using SSH and X11 Forwarding.

Using SSH and X11 Forwarding can solve many remote desktop-related issues. For example, we often install Linux systems on virtual machines not to use the Linux desktop environment but to use the Linux command line and some GUI programs.

For command line programs, SSH can fully solve the problem. For GUI programs, we can also solve it through the X11 Forwarding introduced in this article.

Compared to normal connections to Linux and then operating, SSH and X11 Forwarding have obvious advantages:

  • Avoid entering the Linux GUI environment and use various Linux functions in our familiar Windows/macOS environment
  • Avoid configuring Virtualbox Guest software, Hyper-V’s Enhanced Session, and other virtual machine enhancement software
  • Since it bypasses the graphical interface simulation, SSH and X11 Forwarding usually have better performance

This article uses a CVM on Tencent Cloud for testing. Considering latency and bandwidth, there may occasionally be stuttering (such as when opening a browser to browse the web), but in most cases (such as opening an IDE to write code), there are no issues. Using X11-forwarding can also increase the playability of Linux.