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!
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
/usr/bin/x11vnc -xkb -noxrecord -noxfixes -noxdamage -auth /var/run/lightdm/root/:0 -display :0 -passwd "MyPasswd" -forever -bg -o /var/log/x11vnc.log
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:
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.
That’s it, enjoy!