Start a VNC Server on Ubuntu on Boot

Ubuntu makes many things easy. For example, with Gnome, it's very simple to set up remote access to your desktop with the included Vino application. The only problem is that to get the VNC server running, you have to be logged in! Luckily, it's not too difficult to create a script which starts up the server as soon as your system boots. This is especially handy if you are running a headless server, or are using the primary display for other purposes. In this case, my server runs MythTV on the normal X server connected to my TV, but starts up Azureus and K3B in a VNC session so I can access them without bothering those watching TV.

To set it up, follow these steps:

  1. First, install the TightVNC server. This VNC server has excellent compatibility with clients, and provides reasonable compression for slow networks. It can be installed with Synaptic, or with sudo aptitude install tightvncserver.
  2. Set up the VNC server for the user you wish to log in as. When you run "vncserver" for the first time, it will ask you to set a password. VNC authentication is not the strongest encryption available, so be sure to firewall your server from all but trusted machines. Better yet, deny direct access to VNC and only allow SSH tunnelled or VPN connections. To launch programs or a session when your VNC session starts, modify ~/.vnc/xstartup. Here is my copy of xstartup: it runs an icewm session, Azureus, and K3B. For Gnome, try running "gnome-session", and for KDE, try "startkde".
    #!/bin/sh

    xrdb $HOME/.Xresources
    xsetroot -solid black
    /opt/azureus/azureus &
    k3b &
    icewm-session &

  3. Copy the following into /etc/init.d/vncserver. The easiest way to do it is to copy it to your clipboard, run sudo cat > /etc/init.d/vncserver in a terminal, paste it in, and type CTRL-D. Be sure to change the USER variable to whatever user you want the VNC server to run under.
    #!/bin/sh -e
    ### BEGIN INIT INFO
    # Provides:          vncserver
    # Required-Start:    networking
    # Default-Start:     S
    # Default-Stop:      0 6
    ### END INIT INFO

    PATH="$PATH:/usr/X11R6/bin/"

    # The Username:Group that will run VNC
    export USER="mythtv"
    #${RUNAS}

    # The display that VNC will use
    DISPLAY="1"

    # Color depth (between 8 and 32)
    DEPTH="16"

    # The Desktop geometry to use.
    #GEOMETRY="<WIDTH>x<HEIGHT>"
    #GEOMETRY="800x600"
    GEOMETRY="1024x768"
    #GEOMETRY="1280x1024"

    # The name that the VNC Desktop will have.
    NAME="my-vnc-server"

    OPTIONS="-name ${NAME} -depth ${DEPTH} -geometry ${GEOMETRY} :${DISPLAY}"

    . /lib/lsb/init-functions

    case "$1" in
    start)
    log_action_begin_msg "Starting vncserver for user '${USER}' on localhost:${DISPLAY}"
    su ${USER} -c "/usr/bin/vncserver ${OPTIONS}"
    ;;

    stop)
    log_action_begin_msg "Stoping vncserver for user '${USER}' on localhost:${DISPLAY}"
    su ${USER} -c "/usr/bin/vncserver -kill :${DISPLAY}"
    ;;

    restart)
    $0 stop
    $0 start
    ;;
    esac

    exit 0

  4. Make the script executable with sudo chmod +x /etc/init.d/vncserver.
  5. Then, run sudo update-rc.d vncserver defaults. This adds the appropriate symlinks to the vncserver script so that it is sent the start and stop commands at the appropriate time.
  6. To start the server without rebooting, run sudo /etc/init.d/vncserver start
  7. Finally, connect to your server with a VNC client on port 590X, where X is the value of "DISPLAY" in the vncserver script. On OS X, I like to use Chicken of the VNC. On Windows and Linux, the TightVNC client works nicely.

Of course, this script should work as expected on any Debian-based distribution. If you have problems with performance, try lowering the screen resolution. 800x600 is much faster than 1024x768 over slow connections.

Comments

Thank you for this good

Thank you for this good tutorial.
But the configuring of Runlevels is done with:

update-rc.d vncserver defaults

Good catch! I've updated the

Good catch! I've updated the article.

Finally!

Don't know why it was so hard to find *tight* vnc server + ubuntu + start before logging in... so many permutations of useless info before I came here :)

"Command not found"?

I'm trying to run the script and I can't get it to work properly. I've been running it and all I get is "sudo: /etc/init.d/vncserver: command not found"

Set executable!

I figured it out. You should amend your script to set the execute bit sudo chmod +c /etc/init.d/vnccserver

Updated the article

Thanks for the feedback - I've updated the article to note setting the execute bit.

One step closer

It's working now, however on reboot It does not launch the commands in ~/.vnc/xstartup, the curious thing is that if I run it manually, it works fine.

Perhaps you need to set the

Perhaps you need to set the execute bit on your xstartup file as well? Though it should be created automatically when you first run the server.

Can't log in after connection.

I completed the installation procedure and everything seems to be OK. But after I made connection, using a tightvnc client, I get a blank screen. At that moment there is no user logged in at the host yet. When I start the vncserver at the host after there is logged in a user there is no problem. Can anybody tell me what is wrong.

xstartup?

It sounds like it might be caused by the contents of your xstartup file. When the VNC server starts, there should be whatever processes you asked the VNC server to start. Try running:

ps auxww | grep <vncserver-user>

to see if processes are running as expected or not.

great write-up, I tried to do

great write-up, I tried to do this in 8.04 LTS and got the same issue of just a blank grey screen, no login

myusername@ubuntu:~$ ps auxww | grep myusername
myusername 6011 0.0 0.1 7428 5168 ? S 09:59 0:00 Xtightvnc :1 -de/.Xauthority -geometry 1024x768 -depth 16 -rfbwait 120000 -rfbauth /home/myusername1R6/lib/X11/fonts/Type1/,/usr/X11R6/lib/X11/fonts/Speedo/,/usr/X11R6/lib/X11/foni/,/usr/X11R6/lib/X11/fonts/100dpi/,/usr/share/fonts/X11/misc/,/usr/share/fonts/,/usr/share/fonts/X11/100dpi/ -co /etc/X11/rgb
myusername 6032 0.2 0.6 79828 21324 ? S 09:59 0:00 x-terminal-emulmy-vnc-server Desktop
myusername 6033 0.1 0.3 20120 11544 ? S 09:59 0:00 x-window-manage
myusername 6130 0.1 0.1 6984 4064 ? S 09:59 0:00 /usr/lib/libgco
myusername 6528 0.0 0.0 3008 600 ? S 09:59 0:00 dbus-launch --ab427 --binary-syntax --close-stderr
myusername 6529 0.0 0.0 2568 868 ? Ss 09:59 0:00 /usr/bin/dbus-dress 7 --session
myusername 6534 0.0 0.0 2796 752 ? S 09:59 0:00 gnome-pty-helpe
myusername 6535 0.0 0.1 5708 3120 pts/0 Ss+ 09:59 0:00 /bin/bash
root 6552 0.0 0.1 11384 3840 ? Ss 10:01 0:00 sshd: myusername [p
myusername 6554 0.0 0.0 11384 1872 ? R 10:01 0:00 sshd: myusername@pt
myusername 6555 0.0 0.1 5720 3152 pts/1 Ss 10:01 0:00 -bash
myusername 6578 0.0 0.0 2644 1012 pts/1 R+ 10:04 0:00 ps auxww
myusername 6579 0.0 0.0 3008 776 pts/1 S+ 10:04 0:00 grep myusername

anything look strange?

Definitely sounds like your

Definitely sounds like your xstartup is broken somehow. There should be processes from whatever desktop environment or programs you're trying to run. You can start with simple steps by running just /usr/bin/xterm, or try installing icewm and just use the xstartup file I have in the article.

Thank you!

Thank you for this! I've been looking for a seemless way to start X and VNC all in one. Maybe you should add a part about changing the username from "mythtv" to something else, but I guess everyone can figure it out. Tested working 100% as the guide said it on Ubuntu 8.10 x86_64.

Added note about changing the user

Glad to hear that the steps still work as expected on 8.10, as I haven't upgraded from 8.04 yet. I've updated the article with a note about changing the USER variable to something valid.

/etc/init.d/vncserver "No such file"

It's very strange. Followed guide, but got stuck here:

sudo cat /etc/init.d/vncserver works. However:
sudo /etc/init.d/vncserver start results in :
sudo: unable to execute /etc/init.d/vncserver: No such file or directory

any thoughts?

Finally Working! Thank god (and andrew)

re-did it from scratch. this by typing in on the terminal (before i did cut&paste from windows using vnc). I think this has something to do with the involvement of Windows somewhere in the process.
Anyway - after so 3 days of frustration it finally works!
Thank you!

Glad to hear you got it

Glad to hear you got it working. My guess is that Windows line endings got placed in the file somehow. I've had better success with such things by using SSH + putty, if you're stuck on Windows.

thanks

have been trying (unsuccessfully) to get this to work exactly as you did for a long time — thanks for the vncserver init.d script

awesome (;

Everytime I use the script, I got a prompt asking for password

if I input the password, the script runs well.

But when this script automatically run on boot, I do not get a chance for inputting password.
So the vncserver will not get started.

Can you tell me how can I prevent the password being asked when using the script?
Thank you in advance.

Do you have a password set

Do you have a password set for the user? You can change it with vncpasswd, and check that it exists by looking in ~/.vnc/passwd. vncserver should only ask for a password if one doesn't exist.

Or, is your startup script running the vnc server as the same user that you set up? Perhaps it's trying to run as root or some other user.

xstartup not being launched

I too am having a problem with xstartup not being launched. Not sure what to do.

Has Anyone Solved the xstartup issue

Whats the problem with the xstartup file not being read/executed on startup? When I start vncserver manually it works just fine, but when I log in with just the startup scripts the server is started, but gnome isn't.

If you DO get an X-server

If you DO get an X-server (gray with the old style mous-pointer) you might want to adding the following (for gnome) to the top of your xstartup file:

unset SESSION_MANAGER
exec sh /etc/X11/xinit/xinitrc

However, I (as well as others) am experiencing theme-switching. The theme-chooser will very shortly change the theme, but when I stop using the chooser itself it refers back to the default icon/theme appearance. Anyone got an idea/link?

How could I modify this

How could I modify this script for five users each running there own vnc window?

Is this possible?

For five users, it's probably

For five users, it's probably easiest just to copy the init.d script for each user, and modify the USER variable. Or, for a little more robustness, turn that variable into a list of users sourced from a configuration file, and loop over each user for the start and stop actions.

So copy the init.d script

So copy the init.d script just change the User,display and script name for each user.

I cannot get this to work on

I cannot get this to work on ubuntu 8.10 it works fine on 9.04.

First of all the 8.10 asked for the required-stop section from the init info so I created that section.

# Required-Stop: networking

I can start manually by "sudo /etc/init.d/vncserver start" but will not start automatically from boot.

Can anyone help?

That's rather strange. I'm

That's rather strange. I'm currently running 9.04, and I'm pretty sure that 8.10 loaded the session fine (though I only had 8.10 installed for a few hours). You're sure you ran sudo update-rc.d vncserver defaults? There should be symlinks in the following locations:

/etc/rc0.d/K20vncserver
/etc/rc1.d/K20vncserver
/etc/rc2.d/S20vncserver
/etc/rc3.d/S20vncserver
/etc/rc4.d/S20vncserver
/etc/rc5.d/S20vncserver
/etc/rc6.d/K20vncserver

Yes on 8.10 I only have

Yes on 8.10 I only have symlinks in these locations

/etc/rc0.d/K20vncserver1 -> ../init.d/vncserver
/etc/rc1.d/K20vncserver1 -> ../init.d/vncserver
/etc/rc6.d/K20vncserver1 -> ../init.d/vncserver
/etc/rcS.d/S20vncserver1 -> ../init.d/vncserver

Well that's the problem.

Well that's the problem. Either create the symlinks manually, or re-run the update-rc.d command to create them.

/bin/sh: Illegal option -

Thanks for this write-up. I have followed all the steps but cannot get "sudo /etc/init.d/vncserver start" to work. It returns: "/bin/sh: Illegal option -". I am running Ubuntu 9.10. I have created the script twice but it does not solve the problem. Even if I reduce the script to e.g. only the first 5 lines, it returns the same error.
Manually (via command "/usr/bin/vncserver -name my-vnc-server -depth 24 -geometry 1280x1024 :1") I get it to work without problems and can connect. Any idea?
Thanks.

I wonder if 9.10's /bin/sh

I wonder if 9.10's /bin/sh has changed; perhaps it doesn't like the -e parameter. Try removing it, or changing /bin/sh to /bin/bash or /bin/dash?

I have tried the

I have tried the following:
1) Removing the -e parameter and "sudo /etc/init.d/vncserver start" returns: "/bin/sh: Can't open".
2) Changing to "/bin/bash -e" returns "/bin/bash: Illegal option -"
3) Changing to "/bin/bash" returns ": file or folder does not exist"
4) Same for "/etc/dash -e" and "/etc/dash"

The only thing I can think of is that directory "/usr/X11R6/bin/" does not exist on my machine, nor does folder "X11R6" (in another location). I am still open for other ideas ;-)

Post new comment

The content of this field is kept private and will not be shown publicly.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Allowed HTML tags: <a> <em> <strong> <blockquote> <cite> <q> <code> <ul> <ol> <li> <dl> <dt> <dd> <strike>
  • You may post code using <code>...</code> (generic) or <?php ... ?> (highlighted PHP) tags.
  • Lines and paragraphs break automatically.

More information about formatting options