Results 1 to 5 of 5

Thread: Sudo and #!/bin/sh scripts -- question re permissions

  1. #1
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,727
    Blog Entries
    2

    Default Sudo and #!/bin/sh scripts -- question re permissions

    If I enter this command in a console: sudo ifconfig
    of course I get the reply "sudo: ifconfig: command not found"
    because sudo has a different path from root user.

    But if I make a script called script.sh, containing ifconfig like so:
    Code:
    #!/bin/sh
    ifconfig
    and execute it with this command: sudo ./script.sh
    well I get the correct answer. It finds the application ifconfig and runs it this time.

    What I don't understand is why "ifconfig" is found and executed properly when I run the script as sudo. Is that different from runnning the command directly as: "sudo ifconfig"?

    Can someone please explain the nuance here that I don't understand.

    Thanks
    Leap 42.3 & 15.1(Beta) &KDE
    FYIs from the days of yore

  2. #2
    Join Date
    Jun 2008
    Location
    UTC+10
    Posts
    9,686
    Blog Entries
    4

    Default Re: Sudo and #!/bin/sh scripts -- question re permissions

    If you add

    Code:
    echo $PATH
    just before ifconfig in your script, it will be clear how it finds ifconfig. As to why $PATH is set when running a shell script, when you run

    Code:
    sudo ifconfig
    no init files are read, so $PATH doesn't change. When you run

    Code:
    sudo ./script.sh
    It gets settings from init files. As to which init file it gets it from, a search

    Code:
    # grep -r "$PATH" /etc
    matched /etc/default/su, and the line is:

    Code:
    SUPATH=/usr/sbin:/bin:/usr/bin:/sbin
    A search showed that $SUPATH is the initial $PATH for root. Presumably sudo is following precedent. This is poorly documented, I didn't see any mention of SUPATH in any man page.

  3. #3
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,727
    Blog Entries
    2

    Default Re: Sudo and #!/bin/sh scripts -- question re permissions

    Thank you. Clear now.
    Leap 42.3 & 15.1(Beta) &KDE
    FYIs from the days of yore

  4. #4
    Join Date
    Jun 2008
    Location
    Netherlands
    Posts
    24,869

    Default Re: Sudo and #!/bin/sh scripts -- question re permissions

    Ther is a similar effect when using su.

    Using just
    Code:
    su
    will make you root without changing your environment (including the PATH variable).

    Using
    Code:
    su -
    Does start an new shell including all initialisation as done during at login (in a console/terminal). This means that things like .bashrc and .profile from the home directory of root are used and, among other things, the PATH according to these files is set. For security reasons this is important. A normal user may have added directories (including the infamous ./) to his PATH that contain programs with names (like ls) that can create everyting from amazement to disaster when executed as root.

    So I always advise to use the - sign with su.

    I wonder if one calling
    Code:
    sudo ifconfig
    as 'a' user is sure that the right program is run?
    Henk van Velden

  5. #5
    Join Date
    Mar 2008
    Location
    Oz
    Posts
    11,727
    Blog Entries
    2

    Default Re: Sudo and #!/bin/sh scripts -- question re permissions

    Thanks hcvv. Clearer and clearer.

    Actually FYI I asked this because I wrote some code that interrogates the system for some information. It wasn't a bash script. It is a compiled executable. But I was wondering why it will run fine with "sudo program_name" when I expected to be required to run it as "su -c program_name".

    No big deal but I was curious how it happens.

    Ken_yap and you have shone a light on all of that for me now. Thanks guys.
    Leap 42.3 & 15.1(Beta) &KDE
    FYIs from the days of yore

Posting Permissions

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