search by tags

for the user

adventures into the land of the command line

setting up vnc server

VNC (Virtual Network Computing) is a graphical desktop sharing system that uses the Remote Frame Buffer protocol (RFB) to remotely control another computer.

I had to set this up the other day in order to connect to a remote server and use something that was only possible with a GUI. No cli unfortunately. This is how to do it.

First, we need to install VNC Server

$ sudo -i
$ apt-get install gnome-core xfce4 firefox
$ apt-get install vnc4server

The installed packages will look like this

$ dpkg -l | grep vnc

ii  vnc4server          4.1.1+xorg4.3.0-37ubuntu5        amd64        Virtual network computing server software

Add a user for VNC.

$ adduser vncuser

Specify a vnc password.

$ su - vncuser
$ vncserver
password: xxxx

Just in case backup the xstartup file.

$ cp ~/.vnc/xstartup ~/.vnc/xstartup.bak

Edit the configuration file.

$ vim ~/.vnc/xstartup

#!/bin/sh
unset SESSION_MANAGER
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 &

Kill vncserver with this command.

$ vncserver -kill :1

Paste this start script.

$ sudo vim /etc/init.d/vncserver

#!/bin/bash

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 "
"
 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

Make it executable.

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

Set up some vnc configurations

$ mkdir -p /etc/vncserver
$ vim /etc/vncserver/vncservers.conf

VNCSERVERS="1:vncuser"
VNCSERVERARGS[1]="-geometry 1024x768"

Add vnc to start on boot

$ update-rc.d vncserver defaults 99

Restart the system.

$ reboot

VNC Server listens on port 5901 by default. You can connect to it via a VNC Client with something like:

vnc://server_ip:5901