GUI using VNC with Amazon EC2 Instances

Arafat Khan
3 min readOct 20, 2017

--

Amazon ec2 Instances (a.k.a. Virtual Machines) are an amazing resource for any developer these days and regardless of why you use them it can’t be denied they are really fun and astonishingly powerful.

As someone who loves playing with cloud computing resources and virtual machines, I wanted to have a GUI and basically use my virtual machine as a computer. When I finally figured out a simple way It felt like discovering a whole new world. And I would like to share it with you.

You can use Virtual machines with GUI using these simple steps

  1. Set up an instance following these instructions. This example is tested on Ubuntu instance and hopefully should work on most other OS.
  2. Download and install Realvnc or any other VNC client.
  3. SSH into the instance and type these commands
sudo apt-get update
sudo apt-get upgrade
sudo apt-get install ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal xfce4 vnc4server

Now this will take some time but it’s totally worth the wait. After that do

vncserver

This, helps to set up the VNC configuration. It will ask you to create a VNC password. Choose your password and enter it at the prompt, then enter it a second time to verify.
Now edit ~/.vnc/xstartup file and replace all the contents with the lines below.


#!/bin/sh
# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc
unset DBUS_SESSION_BUS_ADDRESS
startxfce4 &
[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey
vncconfig -iconic &
gnome-panel &
gnome-settings-daemon &
metacity &
nautilus &
gnome-terminal &

Now logout of the instance and login into the instance again using port forwarding. Your command should look something like


ssh -L 5902:localhost:5902 -i amazon.pem ubuntu@ec2–52–90–172–228.compute-1.amazonaws.com

Now start a new vncserver


vncserver -geometry 1340x750

NOTE: Here I 1340x750 refer to my desktop dimensions. You can adjust these values based on your own screen size.

Now use Realvnc to connect to localhost:5902 and enter the password you wrote above.(there is a correlation between them (eg. display 1 = vnc port 5901, 6001 and display 2 = vnc port 5902 etc)

Now you can enjoy your virtual machine with GUI.

A screenshot of RealVnc when using the Virtual Machine in this example

NOTE: once you have performed these steps once all you need is ssh and Realvnc on your computer to work with this. So you can use VNC not just from your own computer but any computer anywhere across the world. All you need is a VNC client and your access key to use this from anywhere.

I hope you liked this post. Any suggestions and recommendations are very welcome.

Have a great day!!!

--

--