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 | $ ssh -XC leimao@192.168.0.23 |
We install xauth
if it has not been installed on the remote Linux computer.
1 | $ sudo apt update |
Make sure X11Forwarding
is yes
in /etc/ssh/sshd_config
.
1 | $ cat /etc/ssh/sshd_config | grep X11Forwarding |
Restart OpenSSH.
1 | sudo systemctl restart sshd |
Confirm X forwarding is enabled.
1 | $ echo $DISPLAY |
Quick test X forwarding with X applications.
1 | $ sudo apt install -y x11-apps |
The GUI of the X applications will show up on our local computer.
We could also try FireFox browser.
1 | $ sudo apt install -y 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 | $ sudo touch /dev/fuse |
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
.
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 | $ cat ~/.vnc/xstartup |
Finally, restart VNC server.
1 | $ vncserver :1 -geometry 1920x1080 -depth 24 |
This time, the remote Linux desktop shows correctly in Remmina.
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 | $ cat ~/.vnc/xstartup |
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
Remote Linux Desktop