GUI Desktop on EC2 instances using VNC
This article is based on this excellent post by Arafat Khan: GUI using VNC with Amazon EC2 Instances.
The original article was written in 2017 and following all the steps described couldn’t get me to connect to my instance properly. So here are the steps to follow in 2024 (tested on Ubuntu 22.04):
Prepare your EC2 instance
- Create your EC2 instance, preferably installing Ubuntu
- Make sure the port 5901 is open:
- Go to Instances under the EC2 Dashboard
- Scroll down and click on the Security tab
- Click on the Security Group listed for your instance
- In the Inbound rules tab, click on Edit inbound rules
- Add a Custom TCP rule with Port Range set to 5901. For the source field, choose My IP. If your IP address is not static, alternatively you can choose a custom range:
- Save the rule
- IMPORTANT! Make sure to keep your SSH rule to be able to connect to the machine later on.
Setup the required dependencies
- Connect to your instance (via SSH or directly from the instances panel in the AWS console) and run the following in the terminal
sudo apt update
sudo apt install ubuntu-desktop gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal xfonts-base xfonts-75dpi xfonts-100dpi xfce4 xfce4-goodies tightvncserver
touch ~/.Xresources
2. Then execute vncserver to setup your password. Keep in mind that it will be truncated to 8 characters:
vncserver
You should end up with something that looks like this:
3. You shouldn’t have to edit anything in your ~/.vnc/xstartup file but here’s mine for reference:
#!/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
4. Make sure to display the logs to ensure everything ran fine:
cat ~/.vnc/ip-<ip_address>:1.log
5. Restart the machine
sudo shutdown -r now
Connect to your Desktop
- Install a VNC Client on your local
2. Once the EC2 instance has restarted connect from your local machine’s terminal using SSH and forwarding the port:
ssh -L 5901:localhost:5901 -i '/path/to/pem/file' ubuntu@ec2-your-private-ip-address.compute-1.amazonaws.com
3. Run the vncserver again
vncserver :1
4. Then connect to the EC2 instance using the VNC Client
And that’s it!
Thanks again to Arafat for laying out the foundations 🙏.