How to Setup a VNC Server on Ubuntu

0
794
Ubuntu and Tight VNC

How to setup a remote connection to a Linux VPS server from your Windows box.

My Bash Script Code Won’t Run!

So you got a remote Linux VPS running in some far off land in the cloud.

You SSH to it regularly. If you have any coding to do, using text editors like nano and vim on the server sucks. Then you get the idea to try writing code on your Windows desktop with your favorite text editor. Then, FTP your code to the remote VPS server.

But there is a problem. It won’t work.

DOS/Windows /r/n != Linux /r

You scratch your head wondering why your legal syntax Bash scripts have errors. Then after spending too much time on it, you read about it here. DOS/PC text is not the same as Linux text. On a Linux text editor, the end of line is represented with a line feed character. Under Windows and DOS, it is represented as carriage return and line feed.

Compounding the problem, your Linux VPS likely does not have a GUI desktop environment installed. So you can’t go and launch a GUI text editor to do your coding – at least not until yet.

What you can do, is create a remote connection to your VPS and run a GUI text editor on the server. This way, when you save the text file on the server, its stored correctly with the proper newlines intact.

I Want My Desktop GUI Back!

Or, maybe you just hate the command line. You administer a remote Linux server and haven’t a clue on getting around the system. ls this. cat that. Barf foobar. Its like greek to you.

You’d rather just click on desktop applications to get your work done. You like the graphical user interface and live and die by it. You wan’t your desktop GUI back!

You research the topic and find that there are several popular GUI desktops for Linux available:

  • Gnome shell
  • Gnome desktop
  • Unity
  • KDE
  • Xfce
  • Cinnamon
  • Mate
  • Sugar
  • Rox
  • Xmonad
  • LXDE
  • awesome
  • Enlightment
  • AmiWM
  • Mezzo
  • UDE
  • Ratpoison
  • Etoile

There’s a whole lot to choose from and you want to try them all. But, since you are running on one of those hot SSD storage environments, disk space is costly and you need to conserve.

You heard out of the group that XFCE4 and LXDE are lightweight. You also know that there is a way to do remote connections to a Linux box and see it graphically on your desktop. It is called Virtual Network Connections (VNC) and you can’t wait to get it to work.

Ready? Ok, lets go for it.

Installing XFCE4 on Ubuntu

Let’s get right to it shall we? To get XFCE4 downloaded and installed on our Ubuntu server, type these commands:

sudo apt-get update
sudo apt-get install xfce4

If you want to install XFCE4 and it’s goodies, type this in instead:

sudo apt-get update
sudo apt-get install xfce4 xfce4-goodies

The Goodies package will take up more disk space and memory. They contain plugins that you may find useful for what you are trying to do on your Ubuntu server.

Virtual Network Connection (VNC)

So now you got your desktop GUI picked out and installed.

But how are you going to connect to your VPS server without SSH in terminal mode? How are you going to actually see your new desktop?

The answer is with a virtual network connection (VNC). You install a Vncserver on your Linux VPS. On the client side, you connect to the VNC server with a VNC client.

Lets move next to installing the VNC components.

TightVncServer and TightVncViewer

For the Linux platform, there are a few VNC distributions available:

  • Chicken of the VNC
  • Vinagre
  • TigerVNC
  • LibVNCServer
  • TightVnc
  • SPICE

Here I will explain how to install TightVnc on an Ubuntu server. This will allow us to connect from our Windows desktop with TightVncViewer.

First, get the server:

sudo apt-get update
sudo apt-get tightvncserver

Second, run it and setup the configuration:

vncserver

Next, create a ~/.vnc/xstartup file that when VNC server starts up, launches XFCE4:

xrdb $HOME/.Xresources
startxfce4 &

Allow xstartup to be executed

sudo chmod +x xstartup

Create the Vncserver Service Script

Now that we got all of this done, we must create a VNC service script. This script gets called whenever we need to start, stop, and restart vncserver.

There are three global constants that you can modify depending on the service name, color depth, and screen resolution of the framebuffer you want to send to the client. It is best to match the geometry setting to the full screen of the VNC client you are using.

VNCSERVER="Xtightvnc"
COLOR_DEPTH="16"
GEOMETRY="2048x1024"

This file should be created as /etc/init.d/vncserver. So go fire up a Linux text editor like nano or vim on your VPS server (don’t forget to do it on your Linux box) and copy and paste this bash code:

#!/bin/bash
 
# Script name: /etc/init.d/vncserver
# Purpose:
#  Command line interface to start, stop, and restart a vnc server
#
# Author: Kerry Kobashi
# Company: Kobashi Computing
# Date: March 7, 2015
#
# This init.d script conforms to Linux Standard Base (LSB) Init standard
#
# LSB Init standard scripts must provide actions to:
#   start - start the service
#   stop - stop the service
#   restart - stop and restart the service
#   force-reload - reload configuration; restart service if running
#   status - show service status
#
# LSB Init Optional actions:
#  reload - reload configuration without stopping and restarting
#  try-restart - restart service if already running
#
# LSB Init exit error codes:
#  0 - running
#  1 - dead, pid exists
#  2 - dead, lock exists
#  3 - not running
#  4 - status unknown
 
VNCSERVER="Xtightvnc"
COLOR_DEPTH="16"
GEOMETRY="2048x1024"
 
function getNumProcesses() {
  local procName=$1
  local numArgs=$#
 
  case $numArgs in
    # get total number of running processes for procName
    1)
       echo $(pgrep -c ${procName})
    ;;
    # get total number of running processes
    0 | *)
       echo $(ps -fe --no-headers | wc -l)
    ;;
  esac
}
 
# LSB compliant init scripts must call ./lib/lsb/init-functions
# $0 - this process name (i.e. ${VNCSERVER})
# $1 - action
# . invokes the currently installed shell script (bash)
. /lib/lsb/init-functions
 
 case $1 in
  start)
    echo "Starting ${VNCSERVER}"
    vncserver :1 -geometry ${GEOMETRY} -depth ${COLOR_DEPTH}
    if [ $? -ne 0 ]; then
      echo "Error (3): ${VNCSERVER} could not be started"
      exit 3
    fi
 
    echo "${VNCSERVER} started"
    exit 0
  ;;
 
  stop)
    echo "Stopping ${VNCSERVER}"
    $0 status
    if [ $? -eq 3 ]; then
      echo "Error (3): ${VNCSERVER} was not running"
      exit 3
    fi
 
    vncserver -kill :1
    if [ $? -ne 0 ]; then
      echo "Error (4): ${VNCSERVER} could not be stopped"
      exit 4
    fi
 
    echo "${VNCSERVER} stopped"
    exit 3
  ;;
 
  restart)
    echo "Restarting ${VNCSERVER}"
 
    $0 stop
    if [ $? -ne 0 ]; then
      echo "Error (4): ${VNCSERVER} restart failed on stop"
      exit 4
    fi
 
    $0 start
    if [ $? -ne 0 ]; then
      echo  "Error (3): ${VNCSERVER} restart failed on start"
      exit 3
    fi
 
    echo "$(VNCSERVER) restarted"
    exit 0
  ;;
 
  force-reload)
    echo "Force reload is not supported"
    $0 status
    exit $?
  ;;
 
  reload)
    echo "Reload is not supported"
    $0 status
    exit $?
  ;;
 
  try-restart)
    echo "Trying to restart ${VNCSERVER}"
 
    $0 status
    if [ $? -eq 3 ]; then
      echo "Error (3): ${VNCSERVER} was not running"
      exit 3
    fi
 
    $0 restart
    exit $?
  ;;
 
  status)
    num=$(getNumProcesses ${VNCSERVER})
    if [ $num -gt 0 ]; then
      echo "Status: ${VNCSERVER} is running"
      exit 0
    fi
 
    echo "Status: ${VNCSERVER} is not running"
    exit 3
  ;;
 
esac
 
exit 0

As written, you can use it as a template for other init daemon services you need to get up and running. I wrote it with the LSB standard in mind.

If I can do it, you can too. So walk through it and spend the time to learn. No excuses!

Now onto the good stuff.

Let’s Rock!

We have one more thing to do. We need to install a VNC client on our Windows computer. Unlike the small list of our VNC servers, there are a ton of VNC clients we can use. Some have a Windows VNC client, some don’t.

  • TigerVNC
  • Citrix XenApp
  • BeAnywhere
  • Bomgar
  • Logmein
  • FreeNX
  • Ulteo Desktop
  • X11vnc
  • Splashtop Remote
  • RealVNC
  • TeamViewer
  • TightVNC
  • xpra
  • ScreenConnect
  • RHUB
  • GoToMyPC

Let’s stick with the same vendor and use TightVNC client.

You can download this at TightVnc. I chose the TightVNC 1.3.10 version that is labeled as Self-installing for Windows.

After TightVncViewer is installed, launch it. For the remote address enter in the IP address of your Linux VPS server followed by port 5901:

your.ip.server.address:5901

The default port number for VNC connections is 5901. Don’t forget to add the : 5901 to the end of the host IP address.

DONT click the Connect button yet!

Before you hit the connect button, kill the previous vncserver instance we launched. To test my init.d script to see that it works, you can do this:

cd /etc/init.d
./vncserver status     #it should say it is presently running
./vncserver stop        #kill it
./vncserver status     #check again ;o)
./vncserver start       #run again

Now go and hit the connect button in VncClient and you should see XFCE4 running on your Windows box.

Cool eh? You now have full keyboard and mouse access from your Microsoft Windows box to your remote Linux server.

Its up to you to decide if you like the XFCE4 GUI look and feel. Play around with it. I’ll leave it as an exercise for you to install other GUI desktops. You can try LXDE which is lightweight and can be used on a Linux VPS too. If you can afford to have a Linux VPS server with lots of RAM on it (say at least 4GB) and you don’t have lots of public traffic hitting your server, go ahead and install the other GUIs. But in general, I’d not do so and stick to light desktop environments instead.

Oh and one more thing, before you do any desktop installations, don’t forget to remove the previous desktop installation before proceeding or you may just screw yourself over. Google it if you have to.

Of Interest