Configuring VNC to automatically start on boot at the login screen on Ubuntu 12.04

As you may know, the standard VNC server that comes with Ubuntu requires the user to be logged in before it will start, and will only allow you to remote in while that session is active. But what if you want to be able to log in as any user and switch between users without your session breaking? What if you have a machine that you don’t want to connect a keyboard, mouse and monitor to and only want to be able to use remotely? This tutorial will show you how to do just that.

It is important that you disable any existing VNC servers you have running on the system before trying this tutorial. If you need to remote into the machine to set this up then use ssh!

Installing x11vnc

To achieve this we will use x11vnc, so first of all lets install it:

sudo apt-get install x11vnc

Ok, now lets try a quick test to see if it works:

/usr/bin/x11vnc -auth /var/run/lightdm/root/:0 -display :0

If it works, you should see a lot of output and the program will have control of the terminal (i.e. there will be no prompt). If it hasn’t worked it will drop back to the prompt. You then need to ensure you have turned off the default VNC server, give the machine a reboot and try again.

Once it is running you can then try connecting from another machine using any VNC client. Ubuntu 12.04 has one called Remmina but you could use something like TightVNC from Windows.

Configuring x11vnc to start on boot

Ok, so what we have is the ability to connect to the machine at any point as long as the X server is running, but we want the service to start at boot automatically and run for as long as the machine is up. To do this we will create a simple upstart script which will listen for the login-session-start event which is triggered by lightdm. So lets create the script:

sudo vi /etc/init/x11vnc.conf

Now place the following into the script:

start on login-session-start
script
/usr/bin/x11vnc -xkb -noxrecord -noxfixes -noxdamage -auth /var/run/lightdm/root/:0 -display :0 -passwd "MyPasswd" -forever -bg -o /var/log/x11vnc.log
end script

Be sure that you change MyPasswd for your own password. This will ensure that not just any old random Joe can connect to your system. This will also send the output to the /var/log/x11vnc.log log file.

Because your upstart file contains a password in it we need to make it secure as the default permissions are 644, which means any user on the system can read this file and that’s not what we want. So to stop this and ensure that only root has access to it do the following:

sudo chown root:root /etc/init/x11vnc.conf
sudo chmod 600 /etc/init/x11vnc.conf

Here we have set the owner/group to root and set it so that only root can read and write to the file. As these upstart services are started by root there is no issue with permissions and it shouldn’t affect anything else as nothing else has the need to read this file.

Once you have done this, give the system a reboot:

sudo reboot

When the system comes back up you should be able to remote desktop to your machine using any good VNC client. It should also prompt your for the password you supplied in the config.

VNC Server running at login

VNC Server running at login

That’s it, enjoy!

7 thoughts on “Configuring VNC to automatically start on boot at the login screen on Ubuntu 12.04

  1. I used /etc/gdm/Init/Default in older ubuntu versions to start x11vnc at the login screen, but this did not work in 12.04.
    Thanks to your solution it is working again.

    Additional I use an password-file:
    sudo x11vnc -storepasswd ‘MyPasswd’ /etc/x11vnc.pass
    and start x11vnc with parameter
    -rfbauth /etc/x11vnc.pass
    insteat of
    -passwd “MyPasswd”

  2. I’m a Linux newbie and hate the standard VNC server, I could never connect to it when I setup a password, would only connect without a password given. This worked perfectly, thank you! The only thing I did differently was to use gedit instead of the vi editor, a bit confusing for someone like myself but its on the list of things to learn. :-)

  3. Hello, your solution is very helpful, thank you!

    Is it possible to start another VNC server at login screen this way (vino, realvnc)?

  4. Hi Paul,
    Yes it should be possible. Simply change the line that starts it (/usr/bin/x11vnc) to another VNC server binary and it should work in much the same way. You might also want to call the script something more appropriate.

  5. Hi Paul,
    Nice post , i moved from vino to x11vnc as vino just kept crashing on 12.04 , but i have a copy paste issue with x11vnc , I’ve tried every option -noprimary – noset primary etc. just can’t get it right,
    Have you any inspired words on this …. they are needed…
    ps I know this isn’t a support forum, just thought I’d ask anyway ;-)

Leave a Reply

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

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>