Remote Linux Desktop

Introduction

Sometimes, for some stupid reasons, we will have to use a remote Linux desktop. If we have previously installed some GUI commercial remote desktop software, such as NoMachine and TeamViewer, connecting to the remote Linux desktop is simple and straightforward. However, often the time, we are working on a new setup computer, installing some of those software and setting up the connection correctly via SSH becomes prohibitive. In addition, if the remote Linux machine is headless, i.e., has no physical display, using the commercial remote desktop software is sometimes problematic (there are workarounds though).

In this blog post, I would like to talk about how to set up a headless remote desktop correctly via SSH.

Set Up Remote Linux Desktop

X Forwarding

We have to first enable X forwarding.

We SSH to the remote Linux computer with -XC where X is the X service and C allows data compression.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
$ ssh -XC leimao@192.168.0.23
Welcome to Ubuntu 18.04.5 LTS (GNU/Linux 4.9.201-tegra aarch64)

* Documentation: https://help.ubuntu.com
* Management: https://landscape.canonical.com
* Support: https://ubuntu.com/advantage
This system has been minimized by removing packages and content that are
not required on a system that users do not log into.

To restore this content, you can run the 'unminimize' command.



Last login: Sat May 15 09:18:28 2021 from 192.168.0.19
/usr/bin/xauth: file /home/leimao/.Xauthority does not exist

We install xauth if it has not been installed on the remote Linux computer.

1
2
$ sudo apt update
$ sudo apt install -y xauth

Make sure X11Forwarding is yes in /etc/ssh/sshd_config.

1
2
$ cat /etc/ssh/sshd_config | grep X11Forwarding
X11Forwarding yes

Restart OpenSSH.

1
sudo systemctl restart sshd

Confirm X forwarding is enabled.

1
2
$ echo $DISPLAY
localhost:10.0

Quick test X forwarding with X applications.

1
2
$ sudo apt install -y x11-apps
$ xclock

The GUI of the X applications will show up on our local computer.

We could also try FireFox browser.

1
2
$ sudo apt install -y firefox
$ firefox

Linux Desktop Forwarding

We have run single X applications successfully. Can we forward the entire remote Linux desktop? Sure, we can.

We will have to install the following dependencies.

1
2
$ sudo touch /dev/fuse
$ sudo apt install -y xfce4 xfce4-goodies gnome-icon-theme libcanberra-gtk-module

Run the following command on the remote Linux computer while we are still ssh -X in.

1
$ xfce4-session

The forwarded desktop will show up on our local computer.

Opening X applications from the forwarded desktop is fine. But we were not able to open terminal to run commands.

Linux Desktop Forwarding via VNC

A better way to forward the Linux Desktop is to use VNC service.

To install the VNC server, we will install tightvncserver on the remote Linux computer.

1
$ sudo apt install -y tightvncserver

Then we could start a virtual desktop with an id of 1.

1
$ vncserver :1 -geometry 1920x1080 -depth 24

We could then connect to the remote Linux desktop using VNC client software on our local computer.

Remmina is installed by default on my local Ubuntu 20.04 LTS. Here we just have to input the remote Linux computer IP address with the VNC virtual desktop id, and press Enter.

Remmina

The connection was successful. However, we only see gray screen and could not do anything.

To fix this problem, we disconnect the VNC connection and kill the virtual desktop.

1
$ vncserver -kill :1

We will install lxde on the remote Linux computer.

1
$ sudo apt install -y lxde

We will modify the ~/.vnc/xstartup by adding /usr/bin/startlxde to it. The modified file will look like this.

1
2
3
4
5
6
7
8
9
10
11
$ cat ~/.vnc/xstartup
#!/bin/sh

xrdb $HOME/.Xresources
xsetroot -solid grey
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
# Fix to make GNOME work
export XKL_XMODMAP_DISABLE=1
/etc/X11/Xsession
/usr/bin/startlxde

Finally, restart VNC server.

1
$ vncserver :1 -geometry 1920x1080 -depth 24

This time, the remote Linux desktop shows correctly in Remmina.

Remote Linux Desktop

There are some inconvenience that copy and paste text between the local computer and the remote desktop does not work. To fix this problem, again, we disconnect the VNC connection and kill the virtual desktop.

1
$ vncserver -kill :1

We will install autocutsel on the remote Linux computer.

1
$ sudo apt install -y autocutsel

We will modify the ~/.vnc/xstartup by adding autocutsel -fork to it. The modified file will look like this.

1
2
3
4
5
6
7
8
9
10
11
12
$ cat ~/.vnc/xstartup
#!/bin/sh

xrdb $HOME/.Xresources
xsetroot -solid grey
autocutsel -fork
#x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
#x-window-manager &
# Fix to make GNOME work
export XKL_XMODMAP_DISABLE=1
/etc/X11/Xsession
/usr/bin/startlxde

Finally, restart VNC server.

1
$ vncserver :1 -geometry 1920x1080 -depth 24

Now, we could copy and paste easily between the local computer and the remote desktop.

References

Author

Lei Mao

Posted on

06-30-2021

Updated on

06-02-2022

Licensed under


Comments