VNC Server to Boot on Logged In User


16 posts
by D.E.L.B. » Sat Jul 07, 2012 12:32 pm
There is a script that makes the script start on boot, but when I connect to the Pi from my PC it connects to the "root' desktop instead, what I want it to do is the script to boot up when I login to my username which are my initals "jjones."

So how can I do this? The script is this:
Code: Select all
### BEGIN INIT INFO
# Provides:          vncboot
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start VNC Server at boot time
# Description:       Start VNC Server at boot time.
### END INIT INFO

#! /bin/sh
# /etc/init.d/vncboot

USER=root
HOME=/root

export USER HOME

case "$1" in
 start)
   echo "Starting VNC Server"
   #Insert your favoured settings for a VNC session
   /usr/bin/vncserver :1 -geometry 1280x800 -depth 16 -pixelformat rgb565
   ;;

 stop)
   echo "Stopping VNC Server"
   /usr/bin/vncserver -kill :1
   ;;

 *)
   echo "Usage: /etc/init.d/vncboot {start|stop}"
   exit 1
   ;;
esac

exit 0


The tutorial is here:
http://elinux.org/RPi_VNC_Server
User avatar
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
by D.E.L.B. » Sat Jul 07, 2012 3:47 pm
I searched high and low on Google to get this to work, and I'll show you how I got it to work.

Instead of using the script in the Rasberry Pi wiki, use this one provided by "Penguin Tutors":
Code: Select all
#!/bin/sh
# /etc/init.d/tightvncserver
# Customised by Stewart Watkiss
http://www.penguintutor.com/linux/tightvnc
# Set the VNCUSER variable to the name of the user to start tightvncserver under
VNCUSER='*YOURUSERNAMEHERE*'
case "$1" in
  start)
    su $VNCUSER -c '/usr/bin/tightvncserver :1'
    echo "Starting TightVNC server for $VNCUSER "
    ;;
  stop)
    pkill Xtightvnc
    echo "Tightvncserver stopped"
    ;;
  *)
    echo "Usage: /etc/init.d/tightvncserver {start|stop}"
    exit 1
    ;;
esac
exit 0


Now, change the VNCUSER=pi to VNCUSER=*YOUR USERNAME HERE*.

That'll make it boot on the username of which you want it to boot on... but I then received the grey screen error when remotely accessing the Pi from my computer, now the way you fix this is, open up the xstartup file that was created when VNCSERVER executes on your desired username. Now the way you access it and edit it is by:
sudo nano .vnc/xstartup. .vnc is usually in the home directory.

Delete everything that is in xstartup (or not in as mine was), and add this:
Code: Select all
#!/bin/sh
xrdb $HOME/.Xresources
xsetroot -solid black
lxterminal &
/usr/bin/lxsession -s LXDE &


Now it should work.
Last edited by D.E.L.B. on Sat Jul 07, 2012 3:52 pm, edited 3 times in total.
User avatar
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
by geezbeez » Sat Jul 07, 2012 3:51 pm
your starting vncserver under root on startup for user root in home dir /root so that's what will happen.
You want vncserver running under jjones
You can have vncserver running under various users you just set them as :0 :1 :2
After you have started vncserver once under jjones you should have a .vnc dir in your home dir (if I remember correctly)

You can try using your script,
Edit and change USER=root and /root to your user and your home dir
Make sure you use the desired number :1 :2 :3 you want for your user. So you might want to change :1 to :2
and #!/bin/sh The convention is that this is first line of script not after other comments.

Could just have something like this in your startup instead of a script?

su -c "/usr/bin/vncserver :2 -geometry 1280x800 -depth 16 -pixelformat rgb565" - jjones
Posts: 28
Joined: Mon Jul 02, 2012 9:19 pm
by D.E.L.B. » Sat Jul 07, 2012 3:53 pm
geezbeez wrote:your starting vncserver under root on startup for user root in home dir /root so that's what will happen.
You want vncserver running under jjones
You can have vncserver running under various users you just set them as :0 :1 :2
After you have started vncserver once under jjones you should have a .vnc dir in your home dir (if I remember correctly)

You can try using your script,
Edit and change USER=root and /root to your user and your home dir
Make sure you use the desired number :1 :2 :3 you want for your user. So you might want to change :1 to :2
and #!/bin/sh The convention is that this is first line of script not after other comments.

Could just have something like this in your startup instead of a script?

su -c "/usr/bin/vncserver :2 -geometry 1280x800 -depth 16 -pixelformat rgb565" - jjones

I tried all of these but none of them worked, the script provided on the Pi wiki is not efficient enough to do what I wanted it to do. :). The post I made below the OP now works.

I appreciate the input though!
User avatar
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
by D.E.L.B. » Sat Jul 07, 2012 5:46 pm
I'd edit my post but I also ran into another problem, this one:
viewtopic.php?f=28&t=8462&p=101282

If you receive this error, look at the commands:
Code: Select all
sudo chmod 1777 /tmp/.X11-unix
sudo chown root:roo /tmp/.X11-unix


Add them in here, with the "Penguin Tutors" script, I've done this one for you:
Code: Select all
#!/bin/sh
# /etc/init.d/tightvncserver
# Customised by Stewart Watkiss
http://www.penguintutor.com/linux/tightvnc
# Set the VNCUSER variable to the name of the user to start tightvncserver under
VNCUSER='*YOURUSERNAMEHERE*'
case "$1" in
  start)
    su $VNCUSER -c '/usr/bin/tightvncserver :1'
    echo "Starting TightVNC server for $VNCUSER "
    sudo chmod 1777 /tmp/.X11-unix
    sudo chown root:root /tmp/.X11-unix
    ;;
  stop)
    pkill Xtightvnc
    echo "Tightvncserver stopped"
    ;;
  *)
    echo "Usage: /etc/init.d/tightvncserver {start|stop}"
    exit 1
    ;;
esac
exit 0


Done.
User avatar
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
by geezbeez » Sat Jul 07, 2012 11:28 pm
Yes this script is much better.
I didn't really see much point in starting a VNC server on boot.
But then I found there's a free android vnc viewer app which opens up a lot of possibilities.
Posts: 28
Joined: Mon Jul 02, 2012 9:19 pm
by bmwhite » Sun Jul 08, 2012 7:20 pm
Very much a beginner here but I got this to work just following the steps on the penguin tutor website. Note I think there is one error with the startup script that creates the script vncserver - Line 4 where he lists his web address.
Code: Select all
#!/bin/sh
# /etc/init.d/tightvncserver
# Customised by Stewart Watkiss
http://www.penguintutor.com/linux/tightvnc
# Set the VNCUSER variable to the name of the user to start tightvncserver under
<clip>
Should there be a # in front of that to make it a comment? When I run his start stop commands:
Code: Select all
sudo /etc/init.d/tightvncserver start
sudo /etc/init.d/tightvncserver stop
Line 4 seems to throw an error. When I comment it out, I get clean service start and service stop messages.
Posts: 5
Joined: Sun Jul 08, 2012 6:02 pm
by dktucson » Sun Jul 08, 2012 10:02 pm
The beauty of starting a VNC server at boot is so the pi can be run in "headless" mode with no monitor, keyboard or mouse physically attached to it. If you get wireless to work now all you have is the 5v power connector--no more "spaghetti pi" of wires going everywhere . The pi is more stable with fewer peripherals attached... I have mine setup headless with a vnc server at boot and a tp-link wrg-322 wireless fob
Posts: 36
Joined: Tue Jun 12, 2012 2:57 am
by D.E.L.B. » Sun Jul 08, 2012 10:05 pm
bmwhite wrote:Very much a beginner here but I got this to work just following the steps on the penguin tutor website. Note I think there is one error with the startup script that creates the script vncserver - Line 4 where he lists his web address.
Code: Select all
#!/bin/sh
# /etc/init.d/tightvncserver
# Customised by Stewart Watkiss
http://www.penguintutor.com/linux/tightvnc
# Set the VNCUSER variable to the name of the user to start tightvncserver under
<clip>
Should there be a # in front of that to make it a comment? When I run his start stop commands:
Code: Select all
sudo /etc/init.d/tightvncserver start
sudo /etc/init.d/tightvncserver stop
Line 4 seems to throw an error. When I comment it out, I get clean service start and service stop messages.

Yes, add a comment. As in add a #.
User avatar
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
by D.E.L.B. » Sun Jul 08, 2012 11:05 pm
Only just noticed, if you don't want lxterminal to boot when connecting to the Raspberry Pi desktop on launch, remove the "lxterminal &" from:
Code: Select all
#!/bin/sh
xrdb $HOME/.Xresources
xsetroot -solid black
lxterminal &
/usr/bin/lxsession -s LXDE &
User avatar
Posts: 92
Joined: Sat Jun 09, 2012 7:16 pm
Location: Wales, UK
by sco` » Mon Jul 09, 2012 8:52 pm
Hi,

I have always done things slightly different with my other *nix machines (still new and self taught)
but i'm unable to do a "pre-login" x11vnc server session. I think this is because there is no logon manager e.g lightdm ???

I have used ssh tunnels to start the vnc server when I wanted to connect (read that its better for security to start the server when you want it, not from boot) with this type of command....

ssh -p xxxx-t -L 5900:localhost:5900 user@RaspberryPI "x11vnc -localhost -display :0" -auth /var/lib/gdm/:0.Xauth

with the raspi i had to ssh in first and run "ps wwwwaux | grep auth" which pulled up
-auth /tmp/serverauth.qQALchRhXl

then to view -
vncviewer -encodings "copyrect tight zrle hextile" localhost:0

BUT like I said, without a login manager I cannot do this pre-login as i would with
ssh -p xxxx -t -L 5900:localhost:5900 admin-user@192.168.1.XX SUDO x11vnc -safer -localhost "-auth /var/run/lightdm/root/:0 -display :0"

is it possible to tunnel in with ssh pre login without a login manager?
Posts: 3
Joined: Mon Jul 09, 2012 6:47 pm
by largeduck » Thu Jul 19, 2012 3:46 pm
geezbeez wrote:Yes this script is much better.
I didn't really see much point in starting a VNC server on boot.
But then I found there's a free android vnc viewer app which opens up a lot of possibilities.

Thanks for the inspiration.....
http://www.youtube.com/watch?v=wqTAC2He9e8
Posts: 2
Joined: Thu May 31, 2012 3:54 pm
by Allen » Fri Jul 20, 2012 11:51 am
change the vncboot as listed in the wiki (http://elinux.org/RPi_VNC_Server) to


Code: Select all
### BEGIN INIT INFO
# Provides:          vncboot
# Required-Start:    $remote_fs $syslog
# Required-Stop:     $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: Start VNC Server at boot time
# Description:       Start VNC Server at boot time.
### END INIT INFO

#! /bin/sh
# /etc/init.d/vncboot

USER=pi
HOME=/home/pi

export USER HOME

case "$1" in
 start)
   echo "Starting VNC Server"
   #Insert your favoured settings for a VNC session
   su - $USER -c '/usr/bin/vncserver :1 -geometry 1680x1050 -depth 16 -pixelformat rgb565'
   ;;

 stop)
   echo "Stopping VNC Server"
   /usr/bin/vncserver -kill :1
   ;;

 *)
   echo "Usage: /etc/init.d/vncboot {start|stop}"
   exit 1
   ;;
esac

exit 0


that should get you a suitable VNC session at boot without having to delete other things and you can add whatever user you like (change USER and HOME as appropriate and of course make sure you have started the server by hand and set the password for the required user first).
Change the option for server to your own prefs of course.
Posts: 3
Joined: Fri Jul 20, 2012 11:44 am
by Dennisve » Tue Oct 01, 2013 7:00 am
Dear Allen

Thanx a lot for this works great!
I have one more question. Now I have auto startup of the Shell (startx) and login the pi user account, I did it via the magpi issue 3 page 3 method

No more connection to VNC...
When I logout using the mouse and keyboard (which I dont want) then I get back into the same shell (looks like it) and it works then again

Can you help me what is going wrong here?
Posts: 3
Joined: Tue Oct 01, 2013 6:57 am
by Dennisve » Tue Oct 01, 2013 12:58 pm
I'd like to add some extra details to my previous post, I cannot get my issue fixed and I really hope someone has an idea.
-I used the script of Allen to have a VNCserver at startup
-when I boot and log in manually with pi / raspberry: everything perfect I have access to the Pi desktop via my laptop
-then I wanted to have the RPi login automatically (with pi / raspberry) and do startx automatically as well.
-I used something I found in the 3rd magpi issue:

Code: Select all
After logging in type:
sudo nano /etc/inittab
Scroll down to the line:
1 :2345: respawn: /sbin/getty 1 1 5200 tty1
Change it to:
#1 :2345: respawn: /sbin/getty 1 1 5200 tty1
(Put a # at the beginning to comment it out)
Add a line under it:
1 :2345: respawn: /bin/login -f pi tty1 </dev/tty1 >/dev/tty1 2>&1
Press CTRL+X and Y to save changes
Then type:
sudo nano ~/.bash_profile
Type in:
startx
Press CTRL+X and Y to save changes
Then
sudo reboot


this works also I get the Raspberry pi Gui right away without typing in my username and password
-but...then VNC wont connect anymore from my laptop: connection refused.
-what I did next is logout from the GUI. What happens is I get logged out but after that I see the same GUI again. I know too little about Linux to figure out how it is logged in this time. I suspect I am logged in twice, when I logout 1 account I get into the 2nd one and this one connects to my laptop VNC again
But this way I still need the HDMI, keyboard and mouse before I am able to connect to the RPi via my laptop and VNC
Posts: 3
Joined: Tue Oct 01, 2013 6:57 am
by Dennisve » Tue Oct 01, 2013 6:45 pm
Guys I fixed it, here is the answer in case anybody else encounters similar problems.
instead of putting 'startx' in the .bash_profile put 'startx &' this runs startx in the background and tightvnc seems to need this?

The answer was not directly on the internet I just saw somebody fix another problem by doing this and it works!
Posts: 3
Joined: Tue Oct 01, 2013 6:57 am