How to Setup a Desktop Environment on a Remote Linux Machine?

Are you using a remote Linux server and reminiscing the days you could interact with your system using a GUI? Well, the truth is you are not alone. Using terminal has its own challenges, and it might take you some time to learn to work your way around it efficiently. I have recently started using an AWS EC2 Linux instance, and I found myself wondering if only I could operate my remote server using a desktop environment. After spending days, I finally learned how to do that, I consolidated my findings into a few easy to follow painless steps. Upon following it religiously, you’ll have your desktop environment ready in just a couple of minutes.

Ferrari SF90 Suzuka’19

In this post I will show you how to set up the XFCE Desktop environment on a Bionic 18.04 Ubuntu remote server. Why XFCE, you may ask? XFCE is one of the most lightweight desktop environment for Linux, it is highly customizable and aesthetically pleasing.

Installing XFCE Desktop Environment

First, you have to update the download package information from all configured sources. Next, you need to install the desktop environment along with its dependencies.

sudo apt-get update 
sudo apt-get install xfce4 xfce4-goodies xorg dbus-x11 x11-xserver-utils

Installing a VNC Server

Now, you need to install a Virtual Network Computing (VNC) server, which lets you control a system remotely using the Remote Frame Buffer protocol. The server you will be downloading is vnc4server. There are other alternatives to vnc4server like TightVNC and TigerVNC, feel free to check them out. Once installed, you will start the server.

sudo apt-get install vnc4server 
vncserver

You need to kill the vncserver as it will create a xstartup file in ~/.vnc/ directory, which you have to configure. Using nano, open the xstartup file.

vncserver -kill :1 
nano ~/.vnc/xstartup

Copy the below code in the opened xstartup file and save it.

Configure XSTARTUP File

#!/bin/sh

unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
unset DBUS_SESSION_BUS_ADDRESS
exec startxfce4
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
x-terminal-emulator -geometry 80x24+10+10 -ls -title "$VNCDESKTOP Desktop" &
x-window-manager &
exec ck-launch-session xfce4-session

Configure Execution Rights

By default, the user is restricted from executing the xstartup file, so you have to give access by running the following command, chmod 755 ~/.vnc/xstartup.

Configure Desktop Settings

To configure the desktop settings, you have to create a config file in ~/.vnc/ directory. It can be created automatically by running the following command nano ~/.vnc/config. Copy the below code in the config file and save it.

geometry=1920x1080
dpi=96

Connecting to the Remote Server via Port 5901

Now, you need to add Port 5901 to your local system’s security group, which will let it interact with the remote server via a tunnel. Upon doing it, ssh to your local machine.

ssh -L 5901:Localhost:5901 -i your_pem_file.pem server_user_name@server_public_ip

Now start a server by running the following command, vncserver. Voila! Your vncserver is up and running, you can now access the desktop environment on your local machine.

macOS has built-in support to access the VNC server. In the Finder on your Mac, choose Go > Connect to Server. Insert the following address, vnc://localhost:5901 and enter the password to login to the server.

Congratulations on taking the chequered flag, you have been splendid.

Abhijeet Dhupia
Abhijeet Dhupia
Research Assistant @ Spectrum Lab

Areas I’m broadly interested include Machine learning, Image Processing and Computer Vision.