How to Create a GUI on headless machines

tutorial

#1

I highly recommend taking snapshots before trying any of this. In case anything breaks, you can always revert back to a working state. I typically take snapshots along the way too!

Prerequisites:
SSH
If you’re on Windows in your local machine download Putty
If you’re on Mac you can just use your terminal

VNC Viewer
Windows - TightVNC
If on mac you can just user Screen sharing (in your browser Vnc://ipaddress

Public IP - available from Paperspace’s console

The following steps are all done in your Paperspace terminal.

You can get the XFCE packages, along with the package for TightVNC, directly from Ubuntu’s software repositories using apt:
sudo apt-get update
sudo apt-get install xfce4 xfce4-goodies tightvncserver

To complete the VNC server’s initial configuration, use the vncserver command to set up a secure password:
vncserver

After putting in a password it will ask you if you want a view-only password - this is if you want others to view your desktop without being able to make changes - this is totally optional.

You are going to be changing how our VNC servers are configured, we’ll need to first stop the VNC server instance that is running on port 5901:
vncserver -kill :1

Before we begin configuring our new xstartup file, let’s back up the original in case we need it later:
mv ~/.vnc/xstartup ~/.vnc/xstartup.bak

Now we can open a new xstartup file with nano:
nano ~/.vnc/xstartup

Insert these commands into the file so that they are performed automatically whenever you start or restart your VNC server:

#!/bin/bash
xrdb $HOME/.Xresources
startxfce4 &

hit control X to exit and type Y and hit enter to Save it

To ensure that the VNC server will be able to use this new startup file properly, we’ll need to grant executable privileges to it:
sudo chmod +x ~/.vnc/xstartup

To easily control our new VNC server, we should set it up as an Ubuntu service. This will allow us to start, stop, and restart our VNC server as needed.

First, open a new service file in /etc/init.d with nano:
sudo nano /etc/init.d/vncserver

Copy and paste this:
#!/bin/bash
PATH="$PATH:/usr/bin/"
export USER="paperspace"
DISPLAY="1"
DEPTH="16"
GEOMETRY="1024x768"
OPTIONS="-depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY} -localhost"
. /lib/lsb/init-functions

(you can change the geometry for a different size screen if you’d like)

Hit Control+V and then paste:

case "$1" in
start)
log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
;;

Hit Control+V and then paste:
stop)
log_action_begin_msg "Stopping vncserver for user '${USER}' on localhost:${DISPLAY}"
su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
;;

Hit Control+V and then paste:
restart)
$0 stop
$0 start
;;
esac
exit 0
Hit Control+X to exit and Yes to save

Make this service script executable, so that you can use the commands that you just set up:
sudo chmod +x /etc/init.d/vncserver

Now try using the service and command to start a new VNC server instance:
sudo service vncserver start

Now move back to your local machine:

In Mac (or Linux) Open up Terminal and type:
ssh -L 5901:127.0.0.1:5901 -N -f -l paperspace <your public IP>

In Windows - Open Up Putty:
In the host name type your Public IP. On the left you will see Category. Under Connection you should see SSH which you can expand. Under that you will see Tunnes. In the middle you will see Source port - type 5901 in that box. Underneath you will destination - type localhost:5901 in that box and then hit ‘Add’. Now hit open on the bottom. It will ask you for your username which is paperspace and your password (which was emailed to you). You can now minimize Putty (Don’t close it).

Open up Your VNC viewer and for the remote host type in
localhost::5901
and hit connect.

It will ask you for your password which is the one you set up earlier in terminal which you had to type twice (not what was emailed to you). Hit ok/connect/whatever the button is

And now you should see a GUI