FedoraForum.org - Fedora Support Forums and Community
Results 1 to 7 of 7
  1. #1
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    4,989

    Wink Need to run sudo for a normal user script sub-function

    I have a script with multiple functions
    vis
    function definition1() ...
    function definition2() ...
    function definition3()...

    #When I invoke the script I would like to have

    definition1
    sudo definition2
    definition 3
    exit 0

    From what I have read, because I want definition2 to be invoked via sudo, I should make it a stand alone script. But I really would like it to remain in this script because of defined variables and functions.

    Is it possible for a standard (non root owned script) to do some work with root's libraries if the script is run by an administrator ?
    Last edited by lsatenstein; 16th April 2018 at 07:00 PM.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  2. #2
    Join Date
    Dec 2017
    Location
    USA
    Posts
    22

    Re: Need to run sudo for a normal user script sub-function

    Sudo executes other executables with root privileges. Unfortunately you cannot call a function defined by the shell (bash) because it's just a part of bash. So what you can do is go modify definition2(), find all the executables that are run, and insert the sudo statement in front of them.

  3. #3
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    4,989

    Re: Need to run sudo for a normal user script sub-function

    In a related topic,

    If I do a sudo su or sudo command, during the time the script or user is in root, does the $LOGNAME remain that of the user or it changes to that of root.

    I have a sudo alias which is

    alias sudo='sudo ' #one space character after the o in the substitution.

    With Gnome/Fedora, $LOGNAME is invariant.
    With SUSE Tumbleweed $LOGNAME takes on the value root during sudo execution

    I did solve my problem with using $SUDO_USER in place of LOGNAME, while executing as root.
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  4. #4
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,870

    Re: Need to run sudo for a normal user script sub-function

    The problem with using an environment variable is that they can be set to almost anything. Hence it is not a reliable way of determining the user.
    A safer solution would be the logname command.

    User error. Please replace user and try again

  5. #5
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    4,989

    Re: Need to run sudo for a normal user script sub-function

    Quote Originally Posted by ocratato
    The problem with using an environment variable is that they can be set to almost anything. Hence it is not a reliable way of determining the user.
    A safer solution would be the logname command.
    LOGNAME is not consistent vs SUSE and FEDORA
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

  6. #6
    Join Date
    Oct 2010
    Location
    Canberra
    Posts
    2,870

    Re: Need to run sudo for a normal user script sub-function

    Quote Originally Posted by lsatenstein
    LOGNAME is not consistent vs SUSE and FEDORA
    Yes, you said that before.
    What I am suggesting is that $LOGNAME is not a safe way to determine the user's identity since they might have done
    export LOGNAME=fred
    before running your script.

    A safer way is to use the logname command. It should also be independent of the distribution and sudo.
    Last edited by ocratato; 19th April 2018 at 03:09 AM.

    User error. Please replace user and try again

  7. #7
    Join Date
    Jun 2005
    Location
    Montreal, Que, Canada
    Posts
    4,989

    Re: Need to run sudo for a normal user script sub-function

    Hi Ocratato

    Thanks for the idea about using th export. Rather than logname, I could export a variable containing my user id "$euid" or $LOGNAME to a new variable.
    If you use modify sudo rules via sudo visudo
    and uncomment the line

    %wheel ALL=(ALL) NOPASSWD: ALL

    This allowed me to run
    Code:
    #/bin/bash
    function1( ){..)
    function2( ){..)
    function3( ){..)
    function4( ){...}
    
    if [ $euid ==0 ];
    then
     function3  #needs root
     else
     function1
     function2
     sudo $0    #call myself as root.
     function4
    fi
     exit 0
    #......................
    Fortunately RedHat and SUSE distributions allow a user to omit entering the pasword via that NOPASSWD keyword
    Leslie in Montreal

    Interesting web sites list
    http://forums.fedoraforum.org/showth...40#post1697840

Similar Threads

  1. Run script(s) at boot as sudo
    By macallan in forum Using Fedora
    Replies: 6
    Last Post: 25th October 2013, 04:46 PM
  2. How to run sudo command in the script?
    By borispr in forum Using Fedora
    Replies: 2
    Last Post: 4th July 2012, 03:24 PM
  3. Let normal user run script that needs root pemissions
    By Saint Mike in forum Using Fedora
    Replies: 3
    Last Post: 28th July 2007, 09:18 AM
  4. PHP script with sudo user
    By PaLoBo in forum Servers & Networking
    Replies: 0
    Last Post: 18th January 2007, 07:37 PM
  5. Replies: 1
    Last Post: 16th June 2005, 04:03 PM

Posting Permissions

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