Lei Mao bio photo

Lei Mao

Machine Learning, Artificial Intelligence, Computer Science.

Twitter Facebook LinkedIn GitHub   G. Scholar E-Mail RSS

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 did not install those software previously, installing those software and setting up the connection correctly via SSH are impossible. Therefore, a more general way is to set up the remote Linux desktop entirely via SSH.


In this blog post, I would like to talk about how to set up such 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.

$ 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.

$ sudo apt update
$ sudo apt install -y xauth

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

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

Restart OpenSSH.

sudo systemctl restart sshd

Confirm X forwarding is enabled.

$ echo $DISPLAY
localhost:10.0

Quick test X forwarding with X applications.

$ 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.

$ 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.

$ 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.

$ 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.

$ sudo apt install -y tightvncserver

Then we could start a virtual desktop with an id of 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.

$ vncserver -kill :1

We will install lxde on the remote Linux computer.

$ 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.

$ 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.

$ 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.

$ vncserver -kill :1

We will install autocutsel on the remote Linux computer.

$ sudo apt install -y autocutsel

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

$ 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.

$ vncserver :1 -geometry 1920x1080 -depth 24

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

References