User avatar
TideMan
Posts: 210
Joined: Fri Jun 22, 2012 8:08 am
Location: ChCh, NZ

Starting VNC as user pi

Sun Aug 05, 2012 2:19 am

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?
If you wish in this world to advance, your merits you're bound to enhance.
You must stir it and stump it and blow your own trumpet, or trust me you haven't a chance.
Ruddigore, G&S

spitecho
Posts: 24
Joined: Tue Jul 10, 2012 12:03 am
Location: internet

Re: Starting VNC as user pi

Sun Aug 05, 2012 3:00 am

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:
[email protected]:~$ sudo su pi -c "whoami"
pi

User avatar
TideMan
Posts: 210
Joined: Fri Jun 22, 2012 8:08 am
Location: ChCh, NZ

Re: Starting VNC as user pi

Sun Aug 05, 2012 3:49 am

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
If you wish in this world to advance, your merits you're bound to enhance.
You must stir it and stump it and blow your own trumpet, or trust me you haven't a chance.
Ruddigore, G&S

spitecho
Posts: 24
Joined: Tue Jul 10, 2012 12:03 am
Location: internet

Re: Starting VNC as user pi

Tue Aug 07, 2012 12:23 am

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

User avatar
TideMan
Posts: 210
Joined: Fri Jun 22, 2012 8:08 am
Location: ChCh, NZ

Re: Starting VNC as user pi

Tue Aug 07, 2012 1:47 am

Yes, adding:

Code: Select all

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

Thank you Spitecho!!
If you wish in this world to advance, your merits you're bound to enhance.
You must stir it and stump it and blow your own trumpet, or trust me you haven't a chance.
Ruddigore, G&S

vinntec
Posts: 144
Joined: Thu Aug 02, 2012 9:37 am
Location: Basingstoke, UK

Re: Starting VNC as user pi

Thu Aug 09, 2012 4:02 pm

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

User avatar
TideMan
Posts: 210
Joined: Fri Jun 22, 2012 8:08 am
Location: ChCh, NZ

Re: Starting VNC as user pi

Thu Aug 09, 2012 9:40 pm

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.
If you wish in this world to advance, your merits you're bound to enhance.
You must stir it and stump it and blow your own trumpet, or trust me you haven't a chance.
Ruddigore, G&S

User avatar
TideMan
Posts: 210
Joined: Fri Jun 22, 2012 8:08 am
Location: ChCh, NZ

Re: Starting VNC as user pi

Thu Aug 09, 2012 9:49 pm

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
If you wish in this world to advance, your merits you're bound to enhance.
You must stir it and stump it and blow your own trumpet, or trust me you haven't a chance.
Ruddigore, G&S

vinntec
Posts: 144
Joined: Thu Aug 02, 2012 9:37 am
Location: Basingstoke, UK

Re: Starting VNC as user pi

Fri Aug 10, 2012 9:09 am

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

vinntec
Posts: 144
Joined: Thu Aug 02, 2012 9:37 am
Location: Basingstoke, UK

Re: Starting VNC as user pi

Fri Aug 10, 2012 3:57 pm

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:

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Starting VNC as user pi

Fri Aug 10, 2012 7:44 pm

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'
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

vinntec
Posts: 144
Joined: Thu Aug 02, 2012 9:37 am
Location: Basingstoke, UK

Re: Starting VNC as user pi - SOLVED

Fri Aug 10, 2012 7:59 pm

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

pjc123
Posts: 913
Joined: Thu Mar 29, 2012 3:37 pm
Contact: Website

Re: Starting VNC as user pi

Fri Aug 10, 2012 8:18 pm

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.
My Raspberry Pi Project Page:
https://www.flaminghellmet.com/launch/

vinntec
Posts: 144
Joined: Thu Aug 02, 2012 9:37 am
Location: Basingstoke, UK

Re: Starting VNC as user pi

Fri Aug 10, 2012 8:26 pm

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.

rlagreid
Posts: 3
Joined: Wed May 27, 2015 12:39 pm
Location: Norway
Contact: Website

Re: Starting VNC as user pi

Tue Sep 15, 2015 9:34 pm

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

Return to “Beginners”

Who is online

Users browsing this forum: Alessandro1234, Google [Bot], markkuk and 40 guests