Post

Running Linux GUI Apps Remotely on Windows with SSH and X Server

When working on remote development via SSH, you sometimes need a graphical user interface (GUI). For example, I use ROS with VS Code on a Windows machine via SSH to access a Linux host. This guide explains how to set up and use an X server on Windows to run graphical Linux applications remotely via SSH.


1. Install X Server for Windows

Download and install VcXsrv, a popular X Server for Windows, from the following link:

[VcXsrv Windows X Server downloadSourceForge.net](https://sourceforge.net/projects/vcxsrv/)

2. Start and Configure VcXsrv

Ensure that VcXsrv is running on your Windows machine and listening for incoming connections. Follow these steps to start and configure it:

  1. Open VcXsrv (search for VcXsrv in your Start Menu).
  2. Start a new X server session with the following options:
    • Display number: 0
    • Start no client: Leave this as it is.
    • Check the box for Disable access control (this allows all machines to connect to the X server without requiring special permissions).

Note: Disabling access control can pose security risks. Use this option only in trusted environments.


3. Connect to the Linux Host via SSH

Use the ssh command with the -X flag to enable X11 forwarding:

1
ssh -X <username>@<host_ip_address>

Replace <username> with your Linux username and <host_ip_address> with the IP address of the Linux machine.

Tip: If your Linux host’s IP address changes frequently, consider setting a static IP address or using a hostname with DNS.


4. Set the DISPLAY Environment Variable on Linux

On the Linux host, set the DISPLAY environment variable to point to your Windows machine’s IP address. Run the following command:

1
export DISPLAY=<local_ip_address>:0.0

Replace <local_ip_address> with the IP address of your Windows machine.

Tip: If your Windows machine’s IP address changes frequently, consider assigning it a static IP address.


5. Allow Connections with xhost

Run the following command on the Linux host to allow connections to the X server:

1
xhost +

Note: Allowing all connections with xhost + is not secure. For better security, use xhost +<specific_ip> to allow only your Windows machine.


6. Test Running an X Application

To verify that the setup is working, try running a simple X application like xclock:

1
xclock

If everything is configured correctly, the xclock application should appear on your Windows desktop.


7. Running GUI Applications in Docker Containers

To run GUI applications inside a Docker container and display them on your Windows machine, use the following command when creating the container:

1
2
3
4
5
6
docker run -it --rm \
  --env DISPLAY=$DISPLAY \
  --env QT_X11_NO_MITSHM=1 \
  --volume /tmp/.X11-unix:/tmp/.X11-unix:rw \
  your_image \
  bash
  • --env DISPLAY=$DISPLAY: Passes the DISPLAY environment variable to the container.
  • --env QT_X11_NO_MITSHM=1: Fixes potential issues with shared memory in Qt applications.
  • --volume /tmp/.X11-unix:/tmp/.X11-unix:rw: Mounts the X11 socket for communication between the container and the host.
  • your_image: Replace this with the name of your Docker image.

By following these steps, you can run graphical Linux applications on your Windows machine using an X Server and SSH.

This post is licensed under CC BY 4.0 by the author.