How to install VNC Server on Ubuntu 14.04+

Ubuntu-Logo

This Guide will show you how to Install a GUI or Desktop on a Headless/Server machine. This will work with ubuntu 14.04+

As usual I always update my system before I install anything. It’s a good practice to keep our servers/machines secured also

apt-get update && apt-get upgrade

 

Install GUI Desktop

I’m using Ubuntu 14.04 64-bit Server for this tutorial so I will install a GUI of my choice, XFCE is always my favorite Desktop Environment. You can install other but for the shake of this tutorial I will just use XFCE.

apt-get install xubuntu-desktop xfce4 firefox nano

 

Install VNC Server on Ubuntu 14.04

Installing VNC Server on Ubuntu 14.04 is simple, but configuration is a different story.

apt-get install vnc4server

 

Configure VNC Server to work on Ubuntu

Adding VNC User
You can always run your system with root, but it’s not a good security practice. If you have a regular/privileged user, you can skip this step. If you are using root, I strongly recommend you to create a regular/privileged user and use this user rather than root. For the purpose of this tutorial, I’m creating “puremedia” user, but you can choose whatever user/username you want to use or even your exiting privileged user.

adduser puremedia

Lets let them add them to the sudo list to allow for installing and updating applications

adduser puremedia sudo

Now lets login as that user to generate the default configuration for VNC

su puremedia

Lets start VNCserver as this user

vncserver

First time you startup vncserver, you will be asked to provide vnc’s password to access to your VNC Server. This password can be the same as your user’s password or different, it’s up to you but I highly recommend you to set this password different to your user’s password for better security measure.

The next step is to turn off vncserver to modify the xstartup file (startup script) to make vncserver start with xfce4 (we installed xfce4 for this tutorial)

vncserver -kill :1

Now lets edit the xstartup for vncserver

cd

nano .vnc/xstartup

This is the original configuration file

#!/bin/sh
 
# Uncomment the following two lines for normal desktop:
# unset SESSION_MANAGER
# exec /etc/X11/xinit/xinitrc
 
[ -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 &

What you need to do is to uncomment two lines “unset SESSION_MANAGER” and “exec /etc/X11/xinit/xinitrc”, then add “startxfce4 &” to the file. The modified xstartup file should look like this

#!/bin/sh
 
# Uncomment the following two lines for normal desktop:
unset SESSION_MANAGER
exec /etc/X11/xinit/xinitrc
 
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 &

Save xstartup when you are done.

CTRL + X

Create VNC Server statup script

sudo nano /etc/init.d/vncserver

Now add

#!/bin/bash
### BEGIN INIT INFO
# Provides:          tightvncserver
# Required-Start:    $syslog
# Required-Stop:     $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: vncserver
#
### END INIT INFO
 
unset VNCSERVERARGS
VNCSERVERS=""
[ -f /etc/vncserver/vncservers.conf ] && . /etc/vncserver/vncservers.conf
prog=$"VNC server"
start() {
. /lib/lsb/init-functions
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 [email protected]
;;
stop)
stop [email protected]
;;
restart|reload)
stop [email protected]
sleep 3
start [email protected]
;;
condrestart)
if [ -f /var/lock/subsys/vncserver ]; then
stop [email protected]
sleep 3
start [email protected]
fi
;;
status)
status Xvnc
;;
*)
echo $"Usage: $0 {start|stop|restart|condrestart|status}"
exit 1
esac

Let enable this file to execute

sudo chmod +x /etc/init.d/vncserver

 Create VNC Server configuration file

We are going to create vncservers.conf file in /etc/vncserver directory to set VNC’s port, screen resolution for each user.

mkdir -p /etc/vncserver

nano /etc/vncserver/vncservers.conf

 

# VNC port:user
# 1 for port 1 and 5901, 2 for port 2 and 5902, 3 for port 3 and 5903, and so on
VNCSERVERS="1:puremedia"
 
# VNC screen resolution
#GEOMETRY="<WIDTH>x<HEIGHT>"
VNCSERVERARGS[1]="-geometry 1024x768"
 
# Color depth (choose 8, 16, or 32)
DEPTH="32"

Save again

CTRL + X

 Set VNC to start on boot

update-rc.d vncserver defaults 99

 

 

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.