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

All Gurus once were Newbies
Forum rules
There are no such things as "stupid" questions. However if you think your question is a bit stupid, then this is the right place for you to post it. Please stick to easy to-the-point questions that you feel people can answer fast. For long and complicated questions prefer the other forums within the support section.
Before you post please read this

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

Postby markfilipak on Mon Apr 01, 2013 4:43 pm

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.
VMware Player 5.0.2
Host: WinXP3, 32-bit
Guest: Linux Mint 14, 64-bit + Xfce 4.10
markfilipak
Level 1
Level 1
 
Posts: 38
Joined: Sun Mar 10, 2013 8:08 pm

Linux Mint is funded by ads and donations.
 

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

Postby mockturtl on Mon Apr 01, 2013 4:51 pm

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?
Image
User avatar
mockturtl
Level 4
Level 4
 
Posts: 437
Joined: Sat Oct 09, 2010 8:51 pm

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

Postby markfilipak on Mon Apr 01, 2013 5:09 pm

mockturtl wrote:... That said, it smells a little.
Hahahahahaha... I agree! Wouldn't 'sudo'ing the individual script lines run into the same problem?
VMware Player 5.0.2
Host: WinXP3, 32-bit
Guest: Linux Mint 14, 64-bit + Xfce 4.10
markfilipak
Level 1
Level 1
 
Posts: 38
Joined: Sun Mar 10, 2013 8:08 pm

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

Postby mockturtl on Mon Apr 01, 2013 8:13 pm

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.)
Image
User avatar
mockturtl
Level 4
Level 4
 
Posts: 437
Joined: Sat Oct 09, 2010 8:51 pm

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

Postby markfilipak on Mon Apr 01, 2013 8:43 pm

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.
VMware Player 5.0.2
Host: WinXP3, 32-bit
Guest: Linux Mint 14, 64-bit + Xfce 4.10
markfilipak
Level 1
Level 1
 
Posts: 38
Joined: Sun Mar 10, 2013 8:08 pm

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

Postby mockturtl on Mon Apr 01, 2013 9:33 pm

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.
Image
User avatar
mockturtl
Level 4
Level 4
 
Posts: 437
Joined: Sat Oct 09, 2010 8:51 pm

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

Postby markfilipak on Mon Apr 01, 2013 9:42 pm

I'm sorry, mockturtl, but I don't understand what you're writing.
VMware Player 5.0.2
Host: WinXP3, 32-bit
Guest: Linux Mint 14, 64-bit + Xfce 4.10
markfilipak
Level 1
Level 1
 
Posts: 38
Joined: Sun Mar 10, 2013 8:08 pm

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

Postby mockturtl on Mon Apr 01, 2013 9:45 pm

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
Image
User avatar
mockturtl
Level 4
Level 4
 
Posts: 437
Joined: Sat Oct 09, 2010 8:51 pm

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

Postby mockturtl on Mon Apr 01, 2013 9:48 pm

markfilipak wrote:I'm sorry, mockturtl, but I don't understand what you're writing.
Let's see the script.
Image
User avatar
mockturtl
Level 4
Level 4
 
Posts: 437
Joined: Sat Oct 09, 2010 8:51 pm

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

Postby markfilipak on Mon Apr 01, 2013 10:06 pm

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.
VMware Player 5.0.2
Host: WinXP3, 32-bit
Guest: Linux Mint 14, 64-bit + Xfce 4.10
markfilipak
Level 1
Level 1
 
Posts: 38
Joined: Sun Mar 10, 2013 8:08 pm

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

Postby mockturtl on Tue Apr 02, 2013 4:02 am

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.
Image
User avatar
mockturtl
Level 4
Level 4
 
Posts: 437
Joined: Sat Oct 09, 2010 8:51 pm

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

Postby restamp on Tue Apr 02, 2013 10:54 am

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
restamp
Level 1
Level 1
 
Posts: 16
Joined: Sun Mar 31, 2013 9:39 pm

Linux Mint is funded by ads and donations.
 

Return to Newbie Questions

Who is online

Users browsing this forum: colyn, Google Adsense [Bot], Kugelsicher, Mute Ant, Reorx and 27 guests