Results 1 to 10 of 10

Thread: How to reference current User root when a script runs as root (su or sudo)?

  1. #1
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    13,295
    Blog Entries
    2

    Default How to reference current User root when a script runs as root (su or sudo)?

    Am modifying a script I found for another distro(Ubuntu). It mostly works but some behaviors seem to be different on openSUSE.

    Scenario:
    The script has to be run with root permissions (su or sudo).
    References in the script cd to the logged in User typically with something like the following
    Code:
    cd ~/directoryname
    Problem:
    Whereas on Ubuntu the above code sets the directory to the home directory of the logged in User, on openSUSE it instead sets to root's home directory (because is running in the context of su or sudo).

    Is there a simple solution?
    Am considering stuff like declaring a variable with value of the current User's directory, then cd to that.

    Code you think should work appreciated.

    An alternative I've considered is to simply place all directories which normally would be in the User's home directory in root's home directory instead.

    TIA,
    TSU

  2. #2
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    32,317
    Blog Entries
    15

    Default Re: How to reference current User root when a script runs as root(su or sudo)?

    On Wed 05 Mar 2014 05:16:01 PM CST, tsu2 wrote:


    Am modifying a script I found for another distro(Ubuntu). It mostly
    works but some behaviors seem to be different on openSUSE.

    *Scenario:*
    The script has to be run with root permissions (su or sudo).
    References in the script cd to the logged in User typically with
    something like the following

    Code:
    --------------------
    cd ~/directoryname
    --------------------


    *Problem:*
    Whereas on Ubuntu the above code sets the directory to the home
    directory of the logged in User, on openSUSE it instead sets to root's
    home directory (because is running in the context of su or sudo).

    Is there a simple solution?
    Am considering stuff like declaring a variable with value of the current
    User's directory, then cd to that.

    Code you think should work appreciated.

    An alternative I've considered is to simply place all directories which
    normally would be in the User's home directory in root's home directory
    instead.

    TIA,
    TSU


    Hi
    Allow the user to run the application by adding the user name and
    application via visudo?

    If running su and starts in the users home directory it should retain
    the users env (add this your script to check/debug), else set the HOME
    env variable.

    --
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    openSUSE 13.1 (Bottle) (x86_64) GNOME 3.10.2 Kernel 3.11.10-7-desktop
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!


  3. #3
    Join Date
    Sep 2012
    Posts
    7,091

    Default Re: How to reference current User root when a script runs as root (su or sudo)?

    For su this is documented behavior (see man su); I do not know if it is configurable. For sudo see always_set_home and env_reset in man sudoers.

  4. #4
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    13,295
    Blog Entries
    2

    Default Re: How to reference current User root when a script runs as root (su or sudo)?

    Cool.
    Thx guys.

    TSU

  5. #5
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: How to reference current User root when a script runs as root(su or sudo)?

    On 2014-03-05 18:16, tsu2 wrote:

    > Whereas on Ubuntu the above code sets the directory to the home
    > directory of the logged in User, on openSUSE it instead sets to root's
    > home directory (because is running in the context of su or sudo).


    Code:
    Telcontar:/home/cer # who am i
    cer      pts/12       2014-03-06 17:14 (:0)
    Telcontar:/home/cer # exit
    cer@Telcontar:~> su -
    Password:
    Telcontar:~ # who am i
    cer      pts/12       Mar  6 17:14 (:0)
    Telcontar:~ #

    You mean that?


    --
    Cheers / Saludos,

    Carlos E. R.
    (from 13.1 x86_64 "Bottle" at Telcontar)

  6. #6

    Default Re: How to reference current User root when a script runs as root (su or sudo)?

    This will lead you to the roots path
    Code:
    foo@localhost:/somedirectory> su -
    password:
    echo "$PWD"
    /root
    There are other options to that see

    Code:
    su --help | grep -F -- -,
     -, -l, --login                  make the shell a login shell

    so if you do

    Code:
    foo@localhost:/somedirectory> su
    password:
    echo "$PWD"
    /somedirectory
    "Unfortunately time is always against us" -- [Morpheus]

    .:https://github.com/Jetchisel:.

  7. #7
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,726

    Default Re: How to reference current User root when a script runs as root (su or sudo)?

    When you want root's environment use
    Code:
    su -
    instead of
    Code:
    su
    BTW my advice: alkways use su -, for security reasons.
    Henk van Velden

  8. #8
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    13,295
    Blog Entries
    2

    Default Re: How to reference current User root when a script runs as root (su or sudo)?

    Thx for the suggestions,
    but they don't actually remedy the issue I described in my original post.

    In the end, instead of trying to find a scripting solution that might not exist, I decided to "go with the flow of least resistance" (opt for the easiest available solution) and simply modify the script requirements to create objects and run in root's home directory instead of the User root.

    TSU

  9. #9
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    29,726

    Default Re: How to reference current User root when a script runs as root (su or sudo)?

    Quote Originally Posted by tsu2 View Post
    Thx for the suggestions,
    but they don't actually remedy the issue I described in my original post.

    In the end, instead of trying to find a scripting solution that might not exist, I decided to "go with the flow of least resistance" (opt for the easiest available solution) and simply modify the script requirements to create objects and run in root's home directory instead of the User root.

    TSU
    Maybe we did not quite understand your question.

    I for myself am very confused with your statement: "... run in root's home directory instead of the User root"

    Because doing things inside root's home directory is only allowed to root owned processes (colloquialy called "being root"). Except of course when you have changed the permisions there, but I hope that such a break of security is at least reported here in the thread to make us understand what you are doing.
    Henk van Velden

  10. #10
    Join Date
    Feb 2009
    Location
    Spain
    Posts
    25,547

    Default Re: How to reference current User root when a script runs as root(su or sudo)?

    On 2014-06-09 11:16, hcvv wrote:

    > Maybe we did not quite understand your question.


    Same here.

    > I for myself am very confused with your statement: "... run in root's
    > home directory instead of the User root"
    >
    > Because doing things inside root's home directory is only allowed to
    > root owned processes (colloquialy called "being root"). Except of course
    > when you have changed the permisions there, but I hope that such a break
    > of security is at least reported here in the thread to make us
    > understand what you are doing.


    If you do "su" or "sudo", it is possible that things get written to the
    home directory of the user that called su or sudo, but with root's
    attributes. However, if you do "su -", they would go to "/root".

    Maybe he refers to that.

    --
    Cheers / Saludos,

    Carlos E. R.
    (from 13.1 x86_64 "Bottle" at Telcontar)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •