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:
- 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. - 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/shxrdb $HOME/.Xresources
xsetroot -solid black
/opt/azureus/azureus &
k3b &
icewm-session & - Copy the following into
/etc/init.d/vncserver. The easiest way to do it is to copy it to your clipboard, runsudo -i && cat > /etc/init.d/vncserve && exitin a terminal, paste it in, and typeCTRL-D. Be sure to change theUSERvariable 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 INFOPATH="$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
;;
esacexit 0
- Make the script executable with
sudo chmod +x /etc/init.d/vncserver. - 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. - To start the server without rebooting, run
sudo /etc/init.d/vncserver start - 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/vncserverworks. However:sudo /etc/init.d/vncserver startresults in :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
-eparameter. Try removing it, or changing/bin/shto/bin/bashor/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 ;-)
Same Problem
I am having the same problem it sounds like. I am running 10.04 and this is what I get when I run the vncserver start command:
sudo /etc/init.d/vncserver start
* Starting vncserver for user 'shon' on localhost:1...
bash: -c: line 0: unexpected EOF while looking for matching `''
bash: -c: line 1: syntax error: unexpected end of file
Any suggestions?
Does the file have Windows
Does the file have Windows line endings instead of Unix line endings? From those errors it seems like the script file is corrupted somehow.
Thanks!
I was upset that it was so hard to run a headless machine with Ubuntu 9.10, but this procedure worked great for me!
Works! But...
This works perfectly, except that once Im into the desktop, it seems to think I have a German or french keyboard, instead of a british. any way to solve? under keybord, it says i have a british one, but it's not typing as one
wrong keyboard characters too...
I'v got the same problem like Dave...
This sounds like the bug you
This sounds like the bug you are running into:
http://bugs.debian.org/cgi-bin/bugreport.cgi?bug=476727
https://bugs.launchpad.net/ubuntu/+source/vino/+bug/112955/comments/55
http://dertompson.com/2010/02/09/tightvnc-messes-up-keyboard-layout-afte...
How do I connect to display :0
Excellent article. I did some small modification for my user name and screen resolution and everything is still working. I have one issue. how do I VNC into display "0" instead of "1"? The reason is to access programs which are already running on display "0". Display "0" has already been assigned to my session that I logged in on the server.
If I leave Thunderbird mail running on the server on display "0", I cannot see it on display "1" when I am accessing the server from remote desktop. I usually leave some programs running and just lock the screen.
Either use hostname:1, where
Either use hostname:1, where 1 is the display number, or hostname:5901, where 5901 is the port. By default, :1 automatically maps to 5901, and increments up.
No /etc/init.d/vncserver file or directory...
This looks exactly like what I am looking for. I need to run a headless machine that I can setup 2-3 remote users to login with their own accounts. So I need to start VNC on a restart and not have to log in as each one and start the service manually.
I am trying this but when I run sudo cat> /etc/init.d/vncserver I get the error "cat: /etc/init.d/vncserver: No such file or directory"
Any ideas on what this could be? I am running Ubuntu 10.04.
Also is there a way to modify this to allow different VNC servers to run for different users on different ports?
Thanks for your input!
You should be able to modify
You should be able to modify the script easily, either by having one script for each user, or adding in some additional code to handle your case with a few loops.
When you do
sudo <cmd> > /path/to/file, only the command, and not the shell redirection, is run as root. Try doingsudo -iand then the cat, or just dosudo vimand paste in the code.Edit: Which makes me wonder why it worked when I first wrote the article! I've updated the article to use a functional method.
Just what I needed
Thanks a lot for this init script! It's just what I needed for my dedicated OpenVZ torrenting container. You've allowed me to put off learning more about bash scripting a little longer.
Tightvnc Server crash
Hi!
I installed VNC as in this tutorial described! Thank you very much it works great!
As desktop environment I installed the package "gnome-core". It works fine until a certain moment when the connection is used over serveral days (also using KDE programs). Then suddenly the server crashes and I have to delete some temporary files from /tmp: ".X101-lock" and ".X11-unix". then the server has to be restarted. Is this a known problem ? Has anyone got an idea how to fix this? Does the vnc process have too little memory assigned?
Thank you very much for any help!
Accidently gave vnc user root priveleges
Fresh install of Ubuntu 10.04 - first thing I did was install tightvncserver as above (great piece, thank you).
Logging in with TightVNC I was surprised to see the user was "root". Scared the crap out of me.
Best guess is the first time I ran the script (that creates the .vnc directory and password file) I must have typed "sudo".
Reasoning: ownership of ~/.vnc and ~/.Xauthority was root:root.
Fixed it by changing the permissions of ~/.vnc and ~/.vnc/passwd, deleting the .Xauthority files, deleting the ~/.vnc/server:1.log and server:1.pid files; stopped the service, then reran the startup script.
Still confused on the difference between running "/etc/init.d/vncserver start" and simply "vncserver".
Thank you.
Thanks
Worked like a charm on Lucid Lynx (Ubuntu 10.04)
Thanks!
Everything mentioned here
Everything mentioned here also works on Ubuntu 10.04.
Knowing little about bash scripting I got my set up with multiple VNC servers for multiple users working by using
the last script on the page here .
with just one small change. Changed /bin/sh to /bin/bash. This is because in ubuntu the default shell is not bash
thus requiring you to be expllicit.
Edit: Broken link fixed.
Post new comment