Autostart VNC instances for several users

I hava the following situation. There is a computer (in my case it's a HTPC running Ubuntu, but i think it should work on all distributions) running some kind of software on the primary display, which you can not disturb. It would be really annoying to pause any TV playback just to do some stuff on the computer.

Now you have some options how to solve this.

  1. SSH
    Don't get me wrong, i really like command shells. But there are definetly situations where a command shell is the second choice.
  2. SSH + X-forwarding
    This is definetly an option, but has the drawback that X need to be installed on the client machine.
    this is usually the case on linux boxes, but not on windows boxes. There are Windows X-Servers ports,
    but you would really need X installed on all of your windows machines you want to use to remotly controll
    your HTPC (or whatever)
  3. VNC
    So my choice was to use VNC to control my HTPC box. The only problem now is that the default ubuntu installation only enables you to install an vnc server that controls the currently active session, and as mentioned before we can't do that. So i decided to install tightvncserver.

sudo apt-get install tightvnserver

Testing seperate VNC sessions

Once tightvncserver is installed connect to your box using ssh, login with a different user than the one that is logged in on the primary display and run vnc server

vncpasswd
vncserver :1

Now you should be able to connect to your linux box with vnc on display 1 and get a seperate screen.
To stop vnc server run

vncserver -kill :1

Keep in mind that the vncpasswd command need to be run for each user you want to be able to login via vnc. This is quite nice but is not really that what we wanted. We wanted the vnc server(s) to start up at boot time for one or more users.

Start at boot time

First we need to define what vnc servers to start. We do this in /etc/default/vncserver. Open up the file in an editor and define the following

VNCSERVERS="1:userx 2:usery 3:userz

This list can be extended as you need it, just keep the format DISPLAYNUMBER:USERNAME seperated by whitespace

The next thing we need is a start up script which gets executed at boot time and starts the vnc servers. I have attached my startup script, have a look at it and put it to /etc/init.d/vncserver

Once the script is placed we need to activate it, to get run at boot time. I use rcconf for such tasks, run it and just select vnc server. After a reboot the vncserver should get started as desired and you should be able to immediatly connect to them.

Troubleshooting

I had the problem that the keyboard layout in the vnc session got completly messed up. After some research i found out that this bug can be fixed by adding
XKL_XMODMAP_DISABLE=1

to ~/.vnc/xstartup somewhere before /etc/X11/Xsession