Page 1 of 1

How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 4:43 pm
by markfilipak
My script needs 'root' privileges, but if I

Code: Select all

sudo myscript.sh
then $USER is 'root'. I need $USER to be 'mark'. How can I do it? Thanks - Mark.

Re: How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 4:51 pm
by mockturtl
You can modify the environment inline with the call:

Code: Select all

sudo USER=mark ./myscript.sh
Or include the re-definition in the script itself:

Code: Select all

#!/bin/sh
USER=mark
echo $USER
That said, it smells a little. Are you sure you can't `sudo` any commands within the script, and run it as a normal user?

Re: How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 5:09 pm
by markfilipak
mockturtl wrote:... That said, it smells a little.
Hahahahahaha... I agree! Wouldn't 'sudo'ing the individual script lines run into the same problem?

Re: How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 8:13 pm
by mockturtl
markfilipak wrote:
mockturtl wrote:... That said, it smells a little.
Hahahahahaha... I agree! Wouldn't 'sudo'ing the individual script lines run into the same problem?
foo.sh:

Code: Select all

#!/bin/sh
echo $USER
sudo ./bar.sh
echo $USER
bar.sh:

Code: Select all

#!/bin/sh
echo $USER
When you run ./foo.sh, it inherits $USER from its parent (say, a terminal window, which inherited it from the login session). When foo.sh "shells out" to sudo -- a child process -- $USER changes to root. The parent's env is not modified.

(More generally, sudo doesn't preserve the environment by default. Try adding `export myvar=someval` in foo.sh, and `echo $myvar` in bar.sh. Without sudo, the value is inherited; with it, not.)

Re: How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 8:43 pm
by markfilipak
mockturtl wrote:(More generally, sudo doesn't preserve the environment by default.
Indeed. I'm having problems with my Xfce desktop. So, quite unrelated to my OP, I conducted the following experiment:

Code: Select all

                                                     mark@Iris ~ $ su
                                                     Password: 
mark@Iris ~ $ sudo env                               Iris mark # env
[sudo] password for mark:

COLORTERM=Terminal                                   COLORTERM=Terminal
                                                     DBUS_SESSION_BUS_ADDRESS=unix:abstract=/tmp/dbu...
                                                     DEFAULTS_PATH=/usr/share/gconf/default.default....
                                                     DESKTOP_SESSION=default
                                                     DESKTOP_STARTUP_ID=
DISPLAY=:0.0                                         DISPLAY=:0.0
HOME=/home/mark                                      HOME=/root
LANG=en_US.UTF-8                                     LANG=en_US.UTF-8
LOGNAME=root                                         LOGNAME=root
MAIL=/var/mail/root                                  MAIL=/var/mail/root
                                                     MANDATORY_PATH=/usr/share/gconf/default.mandato...
                                                     MDMSESSION=default
                                                     MDM_LANG=en_US.UTF-8
                                                     MDM_XSERVER_LOCATION=local
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/...   PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/...
                                                     PWD=/home/mark
                                                     SESSION_MANAGER=local/Iris:@/tmp/.ICE-unix/1251...
SHELL=/bin/bash                                      SHELL=/bin/bash
                                                     SHLVL=2
                                                     SSH_AGENT_PID=1467
                                                     SSH_AUTH_SOCK=/tmp/ssh-AjPJ14f3GtEC/agent.1251
SUDO_COMMAND=/usr/bin/env
SUDO_GID=1000
SUDO_UID=1000
SUDO_USER=mark
TERM=xterm                                           TERM=xterm
USER=root                                            USER=root
USERNAME=root                                        USERNAME=mark
                                                     WINDOWID=62914564
                                                     WINDOWPATH=8
XAUTHORITY=/home/mark/.Xauthority                    XAUTHORITY=/home/mark/.Xauthority
                                                     XDG_CONFIG_DIRS=/etc/xdg/xdg-default:/etc/xdg
                                                     XDG_DATA_DIRS=/usr/share/default:/usr/local/sha...
                                                     XDG_RUNTIME_DIR=/run/user/root
                                                     XDG_SESSION_COOKIE=461c99289f676f26ea71a0485157...
                                                     _=/usr/bin/env

mark@Iris ~ $                                        Iris mark # 
As can easily be seen, 'sudo' (left) and 'su' (right) are not at all equivalent. When I attempted to load the desktop with

Code: Select all

sudo xfdesktop
I got an error that 'SESSION_MANAGER' was undefined. When I attempted to load the desktop with

Code: Select all

su
xfdesktop
things did not go well, but it did find SESSION_MANAGER in the environment. I would conclude that one should not install system thingies using 'sudo'.
mockturtl wrote:Try adding `export myvar=someval` in foo.sh, and `echo $myvar` in bar.sh. Without sudo, the value is inherited; with it, not.)
Well, I don't follow that - remember: I'm a newbie.

Re: How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 9:33 pm
by mockturtl
I would conclude that one should not install system thingies using 'sudo'.
Don't do graphical things with sudo -- that's for CLI. The GUI analogue [for a gnome-based desktop] is gksudo. (resp., su; gksu)

But system install things -- apt-get, dpkg -- are correctly run with sudo, or a GUI front-end (see how /usr/bin/mintinstall calls out to obtain root permissions).

Normally, the "display manager" (mdm | gdm | kdm | xdm | ...) handles the desktop session.
I got an error that 'SESSION_MANAGER' was undefined.
That's pretty deep in dragon country, I think.

What are you doing?

Code: Select all

As can easily be seen, 'sudo' (left) and 'su' (right) are not at all equivalent.
Note with su, $USER is "root," but "mark" is preserved as $USERNAME.
Well, I don't follow that - remember: I'm a newbie.
Declaring a variable syntax: name=value (or with double-quotes, "value", or single-quotes, 'value', with or without interpolation).

Using a variable syntax: $name or ${name}.

If you want a sub-process environment to have the variable, declare it with export.

Re: How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 9:42 pm
by markfilipak
I'm sorry, mockturtl, but I don't understand what you're writing.

Re: How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 9:45 pm
by mockturtl
foo.sh:

Code: Select all

#!/bin/sh
export hello="World!"

./bar.sh             # without sudo, outputs 'mark World!'
sudo ./bar.sh        # with sudo, outputs 'root'; $hello has no value
sudo -E ./bar.sh     # with sudo, preserving environment, outputs 'root World!'
bar.sh:

Code: Select all

#!/bin/sh
echo $USER $hello

Re: How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 9:48 pm
by mockturtl
markfilipak wrote:I'm sorry, mockturtl, but I don't understand what you're writing.
Let's see the script.

Re: How can I get 'root' privileges but retain $USER?

Posted: Mon Apr 01, 2013 10:06 pm
by markfilipak

Code: Select all

#!/bin/bash                   # Usage: sudo ./setup.sh <vboxsf-name>
cd ~                          #
mkdir "./$1"                  # Create shared folder (VBox-share, not net-share)
mount -t vboxsf "$1" "./$1"   # For this session, mount shared folder.
apt-get purge virtualbox*     # Remove guest additions already installed.
cd /media/$USER               #
for d in `ls ./`              # Find & run Guest Additions (I.E., VBoxLinuxAdditions.run).
do 
  if test -x "./$d/VBoxLinuxAdditions.run"
    then
    if -z `grep vboxsf /etc/fstab`; then echo -e "$1 /home/$USER/$1 vboxsf defaults 0 0" | tee -a /etc/fstab; fi
    sh "./$d/VBoxLinuxAdditions.run"
    exit
  fi
done
echo "Could not find Guest Additions."
The mount fails, but that's not important. The big thing is that 'VBoxLinuxAdditions.run' fails because 'sudo' doesn't have the right environment. Oh, doG! I've been struggling with this for so many days, I'm getting punchy. I think the most recent version I ran was this:

Code: Select all

#!/bin/bash                        # Usage: sudo ./setup.sh <vboxsf-name>
cd ~                               #
mkdir "./$1"                       # Create shared folder (VBox-share, not net-share)
sudo mount -t vboxsf "$1" "./$1"   # For this session, mount shared folder.
sudo apt-get purge virtualbox*     # Remove guest additions already installed.
cd /media/$USER                    #
for d in `ls ./`                   # Find & run Guest Additions (I.E., VBoxLinuxAdditions.run).
do 
  if test -x "./$d/VBoxLinuxAdditions.run"
    then
    if -z `grep vboxsf /etc/fstab`; then echo -e "$1 /home/$USER/$1 vboxsf defaults 0 0" | sudo tee -a /etc/fstab; fi
    sudo sh "./$d/VBoxLinuxAdditions.run"
    exit
  fi
done
echo "Could not find Guest Additions."
doG! I don't know. I think I'm going to have to start from scratch again. I'm really getting tired of this.

Re: How can I get 'root' privileges but retain $USER?

Posted: Tue Apr 02, 2013 4:02 am
by mockturtl
Are you just installing vbox "guest additions?" If so, do it within the guest OS (vbox window) via Menu -> Devices -> Install guest additions. It will mount an .iso, and launch its autorun.sh.

Should be trivial.

Your script is mysterious and terrifying, to me.

Re: How can I get 'root' privileges but retain $USER?

Posted: Tue Apr 02, 2013 10:54 am
by restamp
Sudo sets SUDO_USER to the invoking user, so you could probably code your script to use something like "${SUDO_USER-$USER}" to get the non-root userid. If invoked from "sudo" it will use $SUDO_USER, if not it will use $USER.

YMMV