Ubuntu Server: How to run VNC on startup
NOTE: If you’re jumping into this guide here then it’s worth mentioning that this guide has been tested on Lucid Lynx 10.04, Karmic Koala 9.10, 9.04 Jaunty, and 8.10 Intrepid.
Having installed VNC to enable us to administer Ubuntu Server remotely using a GUI (Graphical User Interface) we now need to make sure VNC is launched at boot time. We can either launch a Putty session and re-type the vncserver -geometry 1280×1024 -depth 24 command after each reboot of the server or we can get the server to issue this command automatically at boot time.
There are a few ways of doing this and we’ll go through each of them in turn below.
Add VNC to rc.local
Perhaps the simplest way of ensuring the VNC session is re-created on boot is to add the “vncserver -geom…..” command to a file called rc.local. Simply type the following commands to do this:
sudo vim /etc/rc.local
and type your Ubuntu password if prompted. This will open the file called rc.local which lives in the /etc folder.
Next, scroll up until your cursor is one line above the line which reads “exit 0” and then press the [Insert] key once (to go into “edit” mode). Next type the following command:
/usr/bin/vncserver -geometry 1280x1024 -depth 24
Once you’ve done that press the [Esc] key once and type the following:
:wq
This should save your changes and bring you back to the command line. Your VNC session should now be recreated each time you reboot the server. If you make a mistake editing the file then issue :q! instead of :wq to abort your changes.
Write a script to launch VNC
An alternative (some say “better”) way is to create a small script. A script is basically a file containing a list of commands which will be executed each time you run it. I personally put all my scripts in a directory below my “Home” folder in a folder called MyScripts.
So, either from a Putty session or a new Terminal Session within Ubuntu (Applications -> Accessories) type the following commands:
cd /home/xxxx
where xxx is your Ubuntu username. This command switches us into that directory.
Next we’ll create a folder called MyScripts below your home folder. So type:
mkdir MyScripts
Obviously the folder name is not important so feel free to pick something more suitable. To switch into the folder we’ve just created type:
cd MyScripts
Next type:
vim StartVNC.sh
This will create a new file called StartVNC.sh and open it for editing. Again, the name of the file is not important.
Now press the [Insert] key once and add a couple of blank lines by pressing the [Enter] key. Next copy and paste the following contents (or simply type them):
#!/bin/sh
echo "JOB RUN AT $(date)"
echo "============================"
echo ""
/usr/bin/vncserver -geometry 1280x1024 -depth 24
NOTE: change the 1280×1024 to match the screen resolution of your Desktop computer and omit the “-depth 24” argument if you’re using Precise Pangolin 12.04LTS
Now press the [Escape] key once and type the following:
:wq
and press [Enter] to save and quit. Next we need to make the file executable so, assuming you called your script StartVNC.sh then you’d type:
chmod a+x StartVNC.sh
Test the script by typing the following:
./StartVNC.sh
Once the script has been created we can ensure it gets run in a few different ways; one way is to add the script to one of the “runlevels” within the Ubuntu boot sequence.
Assign the VNC script to a runlevel
To do this type the following commands:
sudo cp StartVNC.sh /etc/init.d
and enter your password if prompted. This copies the script we created above into the /etc/init.d folder. Now type:
update-rc.d StartVNC.sh defaults
Our script will now be run on boot and so our VNC session will be re-created each time the server is rebooted.
If you ever change your mind and want to remove the script from the start-up, simply type the following command:
sudo update-rc.d -f StartVNC.sh remove
Create a cron job to run the script
My personal preference is to create a cron job to run a script at boot time. The easiest way to do this is to use Webmin. If you decide to go down the cron job route then from within Webmin you can very quickly see exactly what jobs are being run and can also easily enable and disable such jobs just by ticking a box.
So, within Webmin click on System and then Scheduled Cron Jobs. Then click the Create a new scheduled cron job option at the top of the screen that opens.
Click the button next to the Execute cron job as and choose the username you created when you installed Ubuntu. Hint: your username appears in a Putty/Terminal session prompt. eg. yourusernameappearshere@MyMediaServer.
Type or paste the name of your script including the full path eg. /home/htkh/MyScripts/StartVNC.sh >/dev/null 2>&1 into the Command box, replacing htkh with your own username, MyScripts with the name of the folder you created to store your scripts and StartVNC.sh with the script name. The >/dev/null 2>&1 parameter will discard any output the script may produce.
Enter a suitable description in the Description field.
In the When to Execute – Simple schedule drop-down list choose When system boots then click the Create button at the bottom of the screen.
Now’s probably a good time to test it. I’d recommend first testing that you’ve set the job up correctly within Webmin. You can do this by clicking on the job you’ve just created from the long list of cron jobs. Then click the Run Now button at the bottom of the screen. You should see a message similar to the one you saw when you tested it from a Putty/Terminal session.
Now let’s check the script works on boot. So from the System -> Bootup and Shutdown menu within Webmin scroll down to the bottom of the screen and click the Reboot System button. Alternatively type sudo reboot -h now in a Putty/Terminal session.
Wait for the server to reboot and try and initiate a new VNC session from your Desktop computer. If everything’s good you should see your Virtual Desktop once again. If the cron job has failed for any reason you can examine the cause of the failure by going into Servers -> Read User Mail in Webmin and clicking on your username. An “email” should be in there giving the reasons for the failure.