Page 1 of 1

Starting VNC as user pi

Posted: Sun Aug 05, 2012 2:19 am
by TideMan
I followed this tutorial:
http://myraspberrypiexperience.blogspot ... p-vnc.html
to install VNC and it all worked, except for adding tightvnc to startup, which works, but gives me VNC in Root, but I want to be in pi.

My analysis of the problem (and I may be wrong) is that because it executes startup under sudo, I end up in Root, whereas if I execute this manually:
su pi -c 'tightvncserver :1'
and enter the pi password, I get VNC as user pi.

How can I start VNC in startup and get VNC in user pi?

Re: Starting VNC as user pi

Posted: Sun Aug 05, 2012 3:00 am
by spitecho
Using the entire command su pi -c 'tightvncserver :1' as your startup command should run it as pi, even if it's running it as sudo.

For example:
pi@raspberrypi:~$ sudo su pi -c "whoami"
pi

Re: Starting VNC as user pi

Posted: Sun Aug 05, 2012 3:49 am
by TideMan
OK, but where is the best place to put this?
I got into this muddle by loading the tightvncserver script (shown below) like this:
sudo update-rc.d tightvncserver defaults

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='pi'
    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

Re: Starting VNC as user pi

Posted: Tue Aug 07, 2012 12:23 am
by spitecho
You can add it to /etc/rc.local with a text editor as root. Should look something like this:

Code: Select all

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

su pi -c 'tightvncserver :1' &

exit 0

Re: Starting VNC as user pi

Posted: Tue Aug 07, 2012 1:47 am
by TideMan
Yes, adding:

Code: Select all

su pi -c 'tightvncserver :1' &
to /etc/rc.local starts the VNC server as user pi.

Thank you Spitecho!!

Re: Starting VNC as user pi

Posted: Thu Aug 09, 2012 4:02 pm
by vinntec
TideMan wrote:Yes, adding:

Code: Select all

su pi -c 'tightvncserver :1' &
to /etc/rc.local starts the VNC server as user pi.

Thank you Spitecho!!
Hi guys - I have carefully followed your final instructions above and have two problems:

1. The console tty1 login nows says "A VNC server is already running as :1"? I think this has been caused by the change to /etc/rc.local?

2. When I connect via VNC my default folder is "/" rather than "/home/pi"?

Have I made a booboo somewhere? Peter

Re: Starting VNC as user pi

Posted: Thu Aug 09, 2012 9:40 pm
by TideMan
I think a reboot will solve Question 1.
Question 2 is exactly the problem that I raised in this thread. When you get / as the default, it means you are logged on as root.

Re: Starting VNC as user pi

Posted: Thu Aug 09, 2012 9:49 pm
by TideMan
Actually, I think I've just figured out what your problem is Vinntech.
You need to remove the tightvncserver command from the startup using:
sudo update-rc.d tightvncserver remove

Re: Starting VNC as user pi

Posted: Fri Aug 10, 2012 9:09 am
by vinntec
TideMan wrote:Actually, I think I've just figured out what your problem is Vinntech.
You need to remove the tightvncserver command from the startup using:
sudo update-rc.d tightvncserver remove
Hi Tideman
That seems to have stopped the duplicate startup but I still connect as 'pi' (which I confirm with whoami command) but which starts off pointing to "/". I originally setup the vncboot file as shown in the "official" instructions then adapted them using additions to this and comment on how you solved your problem. These RPis one at present ;-( will be used in a school so putting students straight into "/" is not a good idea! I need them to have root authority with sudo like normal 'pi' so they can do controlled maintenance tasks later on.

I must have messed something up somewhere, so will go right back and redo all the steps on the Rpi again.

My first problem of not being able to login to real console turns out to be just messages appearing after the login prompt has appeared, it still works if I just start typing from the login name 'pi'. I am getting different messages now but they still appear after the login prompt...

I will come back in a few hours/days and report progress... Peter

Re: Starting VNC as user pi

Posted: Fri Aug 10, 2012 3:57 pm
by vinntec
I have been all around the internet and tried every combination I could find. The instructions on the RPI wiki http://elinux.org/RPi_VNC_Server and Penguin Tutors http://www.penguintutor.com/linux/tightvnc work up to an extent. They both log "pi" on as expected (confimed by "whoami") but have a slightly different desktop from normal and start in folder "/" - as these are for school computers I need to force starting folder to be /home/pi.

Adding the tightvncserver command to /etc/profile suggested on one website works as I want as I can login remotely and get the same desktop and start in /home/pi. Hooray! Except this doesn't run until I login to pi on the console, which is not much use for headless systems!

I am completely out of ideas and need some more help on this one... Peter :cry:

Re: Starting VNC as user pi

Posted: Fri Aug 10, 2012 7:44 pm
by pjc123
vinntec wrote:I have been all around the internet and tried every combination I could find. The instructions on the RPI wiki http://elinux.org/RPi_VNC_Server and Penguin Tutors http://www.penguintutor.com/linux/tightvnc work up to an extent. They both log "pi" on as expected (confimed by "whoami") but have a slightly different desktop from normal and start in folder "/" - as these are for school computers I need to force starting folder to be /home/pi.

Adding the tightvncserver command to /etc/profile suggested on one website works as I want as I can login remotely and get the same desktop and start in /home/pi. Hooray! Except this doesn't run until I login to pi on the console, which is not much use for headless systems!

I am completely out of ideas and need some more help on this one... Peter :cry:
I never noticed that, and mine does the same thing (user "pi" but the terminal is set to root "/").

I am not sure that this will work, but give it a try in the script (Notice the "-" after su). The "-" makes sure that the user pi's environment is set correctly. Things like the PATH, startup HOME directory, etc.:

su - $VNCUSER -c '/usr/bin/tightvncserver :1'

Re: Starting VNC as user pi - SOLVED

Posted: Fri Aug 10, 2012 7:59 pm
by vinntec
pjc123 wrote:I never noticed that, and mine does the same thing (user "pi" but the terminal is set to root "/").
I am not sure that this will work, but give it a try in the script (Notice the "-" after su). The "-" makes sure that the user pi's environment is set correctly. Things like the PATH, startup HOME directory, etc.:

su - $VNCUSER -c '/usr/bin/tightvncserver :1'
Well blow me down! That works! I will try it a few times to be absolutely sure, but came up as pi and looking at /home/pi as it should be first time!

Looking with a magnifying glass at MAN for SUDO looks like the - is needed, but obviously without the dash it partially works and does something slightly odd as it still "understands" the username without it!

Many thanks - Peter

Re: Starting VNC as user pi

Posted: Fri Aug 10, 2012 8:18 pm
by pjc123
Don't confuse sudo with su, they are different commands.

"su" and "su -" both have their place...it depends on what you want to do. There are situations where I need to use one over the other.

If I am root and run "su pi", the callers environment (root) is not changed. The user becomes pi but the PATH, home directory, etc. stay as root's settings.

If I am root and run "su - pi the environment changes to user "pi". The user becomes pi and the PATH, home directory, etc. are now set to pi's settings.

Re: Starting VNC as user pi

Posted: Fri Aug 10, 2012 8:26 pm
by vinntec
pjc123 wrote:Don't confuse sudo with su, they are different commands.
"su" and "su -" both have their place...it depends on what you want to do. There are situations where I need to use one over the other. ...
Many thanks - that's moved my Linux knowledge on another notch, but at the expense of nearly an entire day trying to figure it out! Thanks for clarifying SU, SU - and SUDO. Peter

PS. That means the "Penguin" instructions for setting up VNC for a specific user are probably wrong (as they use SU). I will try and get a message to them.

Re: Starting VNC as user pi

Posted: Tue Sep 15, 2015 9:34 pm
by rlagreid
Hi,

I had the exact same problem after following the instructions from http://elinux.org/RPi_VNC_Server.
It was first fixed when I changed the vncboot file from:
USER=root
HOME=/root
export USER HOME
to
USER=pi
HOME=/home/pi
export USER HOME