Page 1 of 2 12 LastLast
Results 1 to 10 of 13

Thread: run sub-script as normal user within a script run by root

  1. #1
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,031

    Default run sub-script as normal user within a script run by root

    Hello.

    I run the following script as user root from a terminal in a kde environment. I start it using :
    Code:
    sudo /path/to/script/main.sh
    Code:
    #!/bin/bash
    #
    ########################
    #
    #    {config_script}
    #
    #    /xxx/yyyyyyy/main.sh
    #
    #    §2019_08_11§
    #
    #    ¨version:34¨
    #
    #    ɸminor_version:0-0-0ɸ
    #
    ########################
    
    # # ensure running as root
    if [ "$(id -u)" != "0" ]; then
        echo
        echo
        echo "This script must be run as root user"
        echo
        echo
        exit 255
    fi
    
    #
    # TASK 1
    #
    /xxx/yyyyyyy/task_1.sh
    
    #
    # TASK 2
    #
    /xxx/yyyyyyy/task_2.sh
    
    #
    # TASK 3
    #
    /xxx/yyyyyyy/task_3.sh
    
    .....................
    .....................
    .....................
    .....................
    
    #
    # TASK n
    #
    /xxx/yyyyyyy/task_n.sh
    Some sub-script 'zzzzz' within script 'xxxxxx' (run as root user) must be run as normal user 'userA' .
    Or some command must be run as normal user 'userA' .

    As an real example, I need to create a firefox profile for 'userA' .
    To do so, within the caller script 'xxxxxxxx' I call a script named : create_firefox_profile_user.sh
    Code:
    runuser -l userA  -c '/path/to/script/create_firefox_profile_user.sh  "userA" '
    Code:
    #!/bin/bash
    #
    #
    ########################
    #
    #    {config_jcd}
    #
    #    /xxx/yyyyyyy/create_firefox_profile_user.sh 
    #
    #    §2019_08_11§
    #
    #    ¨version:34¨
    #
    #    ɸminor_version:0-0-0ɸ
    #
    ########################
    
    CUR_USER="$1"
    echo "Creating firefox profile for user $CUR_USER"
    firefox -P
    I got an error :
    Code:
    Error: no DISPLAY environment variable specified
    Changing the command to :
    Code:
    DISPLAY=":0"  firefox -P
    suppress the error.

    Is there a more academic way to run script as another user without the error concerning the dispaly variable.
    Is there other environment variables that are not set from the point of view of the user currently changed.

    Any help and/or comments are welcome.
    Thanks for helping. JCD
    __________

    server leap 15-- ASUS g75vw KDE leap 42.3 -- ASUS g750JZ KDE leap 42.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  2. #2
    Join Date
    Aug 2010
    Location
    Chicago suburbs
    Posts
    12,478
    Blog Entries
    3

    Default Re: run sub-script as normal user within a script run by root

    You are having this problem, mainly because "sudo" does not pass on the DISPLAY and XAUTHORITY environment variables. If you use "su -" to get a root shell, then those variables are passed. Or you can modify "/etc/sudoers" and tell it to pass DISPLAY and XAUTHORITY.
    openSUSE Leap 15.1; KDE Plasma 5;
    testing Leap 15.2Alpha

  3. #3
    Join Date
    Jun 2008
    Location
    Groningen, Netherlands
    Posts
    19,763
    Blog Entries
    14

    Default Re: run sub-script as normal user within a script run by root

    Maybe you could work around this by changing it to this
    Code:
    firefox -P --headless
    and use ' su - ' like nrickert suggests, then
    Code:
    sudo -u YOUR_USER /path/to/script.sh
    ° Appreciate my reply? Click the star and let me know why.

    ° Perfection is not gonna happen. No way.

    https://en.opensuse.org/openSUSE:Board#Members
    http://en.opensuse.org/User:Knurpht
    http://nl.opensuse.org/Gebruiker:Knurpht

  4. #4
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    26,640
    Blog Entries
    15

    Default Re: run sub-script as normal user within a script run by root

    Hi
    Not sure what tasks your performing, but a lot can be preconfigured in /usr/lib64/firefox/defaults/pref/ for example all my about:config options are done via a customized all-openSUSE.js file.
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

  5. #5
    Join Date
    Jun 2008
    Location
    San Diego, Ca, USA
    Posts
    11,101
    Blog Entries
    2

    Default Re: run sub-script as normal user within a script run by root

    I'm not clear on what you're trying to do but my personal SOP is to...

    Start your script as your normal User, particularly if it's in the security context of your logged in User (if another user you can sudo to the other User for executing the script in that User's security context).
    Store the script's results in variables.
    Then sudo or su to root and execute your "master script" and as necessary retrieve values from the variables.

    It generally makes more sense to increase your security context as your flow proceeds, rather than escalating and de-escalating security contexts..

    HTH,
    TSU
    Beginner Wiki Quickstart - https://en.opensuse.org/User:Tsu2/Quickstart_Wiki
    Solved a problem recently? Create a wiki page for future personal reference!
    Learn something new?
    Attended a computing event?
    Post and Share!

  6. #6
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,031

    Default Re: run sub-script as normal user within a script run by root

    Quote Originally Posted by tsu2 View Post
    I'm not clear on what you're trying to do but my personal SOP is to...

    Start your script as your normal User, particularly if it's in the security context of your logged in User (if another user you can sudo to the other User for executing the script in that User's security context).
    Store the script's results in variables.
    Then sudo or su to root and execute your "master script" and as necessary retrieve values from the variables.

    It generally makes more sense to increase your security context as your flow proceeds, rather than escalating and de-escalating security contexts..

    HTH,
    TSU
    Thank you for tour answer which is interesting.
    But does not answer my question.
    Thanks for helping. JCD
    __________

    server leap 15-- ASUS g75vw KDE leap 42.3 -- ASUS g750JZ KDE leap 42.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  7. #7
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,031

    Default Re: run sub-script as normal user within a script run by root

    Quote Originally Posted by malcolmlewis View Post
    Hi
    Not sure what tasks your performing, but a lot can be preconfigured in /usr/lib64/firefox/defaults/pref/ for example all my about:config options are done via a customized all-openSUSE.js file.
    My question is more general.
    My question is not limited to firefox.
    Any way thank you for helping.
    Thanks for helping. JCD
    __________

    server leap 15-- ASUS g75vw KDE leap 42.3 -- ASUS g750JZ KDE leap 42.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  8. #8
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,031

    Default Re: run sub-script as normal user within a script run by root

    Quote Originally Posted by Knurpht View Post
    Maybe you could work around this by changing it to this
    Code:
    firefox -P --headless
    That just open a terminal in the current 'firefox profile' ( should be a new profile created before ).

    I don't think that can help me to create a new profile for a new user within my running script.

    Any way thank you for helping
    Thanks for helping. JCD
    __________

    server leap 15-- ASUS g75vw KDE leap 42.3 -- ASUS g750JZ KDE leap 42.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  9. #9
    Join Date
    Jun 2008
    Location
    South-West France
    Posts
    1,031

    Default Re: run sub-script as normal user within a script run by root

    Quote Originally Posted by nrickert View Post
    You are having this problem, mainly because "sudo" does not pass on the DISPLAY and XAUTHORITY environment variables. If you use "su -" to get a root shell, then those variables are passed. Or you can modify "/etc/sudoers" and tell it to pass DISPLAY and XAUTHORITY.
    > If you use "su -" to get a root shell, then those variables are passed
    Code:
    exec su - -c "${0} ${CMDLN_ARGS}"  --> Create a new profile for user ROOT
    Code:
    exec su root -c "${0} ${CMDLN_ARGS}"  --> Create a new profile for user 'user_install'
    USER : user_install

    ID : 0

    CUR_USER = root

    running : 25-2_create_profile_firefox
    Running Firefox as root in a regular user's session is not supported. ($XDG_RUNTIME_DIR is /run/user/1000 which is owned by user_install.)
    Same message with :
    Code:
    exec su -c "${0} ${CMDLN_ARGS}"
    It seems that there is no other solution than mine posted in my question ?

    Any help is comment/welcome
    Thanks for helping. JCD
    __________

    server leap 15-- ASUS g75vw KDE leap 42.3 -- ASUS g750JZ KDE leap 42.3 -- acer aspire s13 win 10 home -- HP Omen win 10 home - scan EPSON V500 - Brother HL2250DN - Samsung CLP-325W

  10. #10
    Join Date
    Jun 2008
    Location
    Podunk
    Posts
    26,640
    Blog Entries
    15

    Default Re: run sub-script as normal user within a script run by root

    Quote Originally Posted by jcdole View Post
    > If you use "su -" to get a root shell, then those variables are passed
    Code:
    exec su - -c "${0} ${CMDLN_ARGS}"  --> Create a new profile for user ROOT
    Code:
    exec su root -c "${0} ${CMDLN_ARGS}"  --> Create a new profile for user 'user_install'


    Same message with :
    Code:
    exec su -c "${0} ${CMDLN_ARGS}"
    It seems that there is no other solution than mine posted in my question ?

    Any help is comment/welcome
    Hi
    Why not switch to the user to create what ever your doing with your scripts, or why not use profile.d to run them when the user logs in?
    Cheers Malcolm °¿° SUSE Knowledge Partner (Linux Counter #276890)
    SUSE SLE, openSUSE Leap/Tumbleweed (x86_64) | GNOME DE
    If you find this post helpful and are logged into the web interface,
    please show your appreciation and click on the star below... Thanks!

Page 1 of 2 12 LastLast

Posting Permissions

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