Skip to content

Instantly share code, notes, and snippets.

@csghone
Last active October 23, 2020 14:32
Show Gist options
  • Select an option

  • Save csghone/433615be1923ea437504da4316f1248d to your computer and use it in GitHub Desktop.

Select an option

Save csghone/433615be1923ea437504da4316f1248d to your computer and use it in GitHub Desktop.

Tested mostly with 14.04 - but should work with minor tweaks on higher versions

Graphical Environment Setup

# In case your EC2 installation didn't have any GUI packages/X server/etc.
# Ref: https://stackoverflow.com/questions/25657596/how-to-set-up-gui-on-amazon-ec2-ubuntu-server
sudo apt-get install gnome-panel gnome-settings-daemon metacity nautilus gnome-terminal gnome-desktop-environment gnome-theme* gnome-icon-theme

TurboVNC Installation steps

sudo apt-get remove tightvncserver
sudo apt-get remove vnc4server
# Change to latest version as needed from https://sourceforge.net/projects/turbovnc/
wget https://sourceforge.net/projects/turbovnc/files/2.1.2/turbovnc_2.1.2_amd64.deb/download -O turbovnc_2.1.2_amd64.deb
sudo dpkg -i turbovnc_2.1.2_amd64.deb
sudo update-alternatives --install /usr/bin/Xvnc Xvnc /opt/TurboVNC/bin/Xvnc 60
sudo update-alternatives --install /usr/bin/vncpasswd vncpasswd /opt/TurboVNC/bin/vncpasswd 60  
sudo update-alternatives --install /usr/bin/vncserver vncserver /opt/TurboVNC/bin/vncserver 60  
sudo update-alternatives --install /usr/bin/vncviewer vncviewer /opt/TurboVNC/bin/vncviewer 60
# Install java if needed
sudo apt-get install default-jre

Create /etc/init.d/vncserver

  • Ensure this file has +x permissions
#!/bin/bash

unset VNCSERVERARGS
VNCSERVERS=""
[ -f /etc/vncserver/vncservers.conf ] && . /etc/vncserver/vncservers.conf
prog=$"VNC server"

start() {
        #. /lib/lsb/init-functions
        [ -f /etc/vncserver/vncservers.conf ] && . /etc/vncserver/vncservers.conf
        REQ_USER=$2
        echo -n $"Starting $prog: "
        ulimit -S -c 0 >/dev/null 2>&1
        RETVAL=0
        for display in ${VNCSERVERS}
        do
                export USER="${display##*:}"
                if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
                        echo -n "${display} "
                        unset BASH_ENV ENV
                        DISP="${display%%:*}"
                        export VNCUSERARGS="${VNCSERVERARGS[${DISP}]}"
                        su ${USER} -c "cd ~${USER} && [ -f .vnc/passwd ] && vncserver :${DISP} ${VNCUSERARGS}"
                fi
        done
}

stop() {
        . /lib/lsb/init-functions
        REQ_USER=$2
        echo -n $"Shutting down VNCServer: "
        for display in ${VNCSERVERS}
        do
                export USER="${display##*:}"
                if test -z "${REQ_USER}" -o "${REQ_USER}" == ${USER} ; then
                        echo -n "${display} "
                        unset BASH_ENV ENV
                        export USER="${display##*:}"
                        su ${USER} -c "vncserver -kill :${display%%:*}" >/dev/null 2>&1
                fi
        done
        echo -e "\n"
        echo "VNCServer Stopped"
}

case "$1" in
start)
start $@
;;
stop)
stop $@
;;
restart|reload)
stop $@
sleep 3
start $@
;;
condrestart)
if [ -f /var/lock/subsys/vncserver ]; then
stop $@
sleep 3
start $@
fi
;;
status)
status Xvnc
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1
esac

Create /etc/vncserver/vncservers.conf

# Set up as many VNC sessions as needed
VNCSERVERS="1:csghone 2:csghone"
VNCSERVERARGS[1]="-geometry 1280x720 -geometry 1920x1080 -geometry 3840x1080 -depth 16"
VNCSERVERARGS[2]="-geometry 1280x720 -depth 16"

Create /etc/systemd/system/vncserver.service

[Unit]
Description=VNC Server
After=network.target

[Service]
Type=forking
ExecStart=/etc/init.d/vncserver start
ExecStop=/etc/init.d/vncserver stop
RemainAfterExit=yes
Restart=always
RestartSec=10s

[Install]
WantedBy=multi-user.target

Create Startup file in USER home directory ~/.vnc/xstartup.turbovnc

  • Ensure this file has +x permissions
#!/bin/sh

export XKL_XMODMAP_DISABLE=1
unset SESSION_MANAGER
unset DBUS_SESSION_BUS_ADDRESS
unset XDG_RUNTIME_DIR

[ -x /etc/vnc/xstartup ] && exec /etc/vnc/xstartup
[ -r $HOME/.Xresources ] && xrdb $HOME/.Xresources
xsetroot -solid grey

vncconfig -iconic &
#python2 `which terminator` -p startup &

gnome-panel &
gnome-settings-daemon &
metacity &
nautilus --force-desktop &
gnome-terminal &

Start VNC service

# Run following commands for 16.04+
systemctl enable vncserver.service
systemctl daemon-reload
systemctl restart vncserver.service

# If you are on older Ubuntu
/etc/init.d/vncserver start

VNC does not start

  • Check log files in ~/.vnc/

Make UI look cleaner

  • Reset UI configs (After logging to VNC): dconf reset -f / (Restart VNC after this)
  • sudo apt-get install gnome-tweak-tool
  • Run gnome-tweak-tool and set up as needed.

Change VNC resolution on-the-fly

# TurboVNC server/viewer combination allows VNC session screens to be resized dynamically
xrandr -s 1280x720
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment