How to tell whether a bash script has root privileges [duplicate]












2
















This question already has an answer here:




  • How can a script check if it's being run as root?

    10 answers




I have a bash script that will only work with root privileges, so I want to test whether the user has them. Other posts (see below) ask and answer how to know whether the user is actually running as root, but not whether the script has root privileges. These posts say to test whether $EUID is 0.



To try this idea in the context of sudo, I wrote a bash script /tmp/a.sh:



#!/bin/bash
echo $EUID


The following two commands were run as a non-root user with sudo privileges on Ubuntu 16.04. If the $EUID suggestion worked in the context of sudo, the second command would have printed 0 instead of a blank line.



$ /tmp/a.sh
1000
$ sudo /tmp/a.sh

$


FYI, an example of the related posts I am referencing is:



How can a script check if it's being run as root?










share|improve this question













marked as duplicate by Sergiy Kolodyazhnyy bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 31 at 0:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    echo $UID should work. Or according to David F in the comments of your linked answer you should use echo $(id -u)

    – Terrance
    Jan 30 at 18:32













  • Thanks Terrance, that works too. I posted an answer after noticing a fat-finger that causes $EUID and $UID to resolve properly only for non-root users (still a bit of a mystery).

    – Colin McRae
    Jan 30 at 18:43













  • There are multiple other solutions to detect script running as root on the question to which you referred, if that's really what you're asking. If you are asking specifically why $EUID doesn't work, please edit the question to clarify the title and the body of the question, and we can re-open it

    – Sergiy Kolodyazhnyy
    Jan 31 at 0:58
















2
















This question already has an answer here:




  • How can a script check if it's being run as root?

    10 answers




I have a bash script that will only work with root privileges, so I want to test whether the user has them. Other posts (see below) ask and answer how to know whether the user is actually running as root, but not whether the script has root privileges. These posts say to test whether $EUID is 0.



To try this idea in the context of sudo, I wrote a bash script /tmp/a.sh:



#!/bin/bash
echo $EUID


The following two commands were run as a non-root user with sudo privileges on Ubuntu 16.04. If the $EUID suggestion worked in the context of sudo, the second command would have printed 0 instead of a blank line.



$ /tmp/a.sh
1000
$ sudo /tmp/a.sh

$


FYI, an example of the related posts I am referencing is:



How can a script check if it's being run as root?










share|improve this question













marked as duplicate by Sergiy Kolodyazhnyy bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 31 at 0:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.














  • 1





    echo $UID should work. Or according to David F in the comments of your linked answer you should use echo $(id -u)

    – Terrance
    Jan 30 at 18:32













  • Thanks Terrance, that works too. I posted an answer after noticing a fat-finger that causes $EUID and $UID to resolve properly only for non-root users (still a bit of a mystery).

    – Colin McRae
    Jan 30 at 18:43













  • There are multiple other solutions to detect script running as root on the question to which you referred, if that's really what you're asking. If you are asking specifically why $EUID doesn't work, please edit the question to clarify the title and the body of the question, and we can re-open it

    – Sergiy Kolodyazhnyy
    Jan 31 at 0:58














2












2








2









This question already has an answer here:




  • How can a script check if it's being run as root?

    10 answers




I have a bash script that will only work with root privileges, so I want to test whether the user has them. Other posts (see below) ask and answer how to know whether the user is actually running as root, but not whether the script has root privileges. These posts say to test whether $EUID is 0.



To try this idea in the context of sudo, I wrote a bash script /tmp/a.sh:



#!/bin/bash
echo $EUID


The following two commands were run as a non-root user with sudo privileges on Ubuntu 16.04. If the $EUID suggestion worked in the context of sudo, the second command would have printed 0 instead of a blank line.



$ /tmp/a.sh
1000
$ sudo /tmp/a.sh

$


FYI, an example of the related posts I am referencing is:



How can a script check if it's being run as root?










share|improve this question















This question already has an answer here:




  • How can a script check if it's being run as root?

    10 answers




I have a bash script that will only work with root privileges, so I want to test whether the user has them. Other posts (see below) ask and answer how to know whether the user is actually running as root, but not whether the script has root privileges. These posts say to test whether $EUID is 0.



To try this idea in the context of sudo, I wrote a bash script /tmp/a.sh:



#!/bin/bash
echo $EUID


The following two commands were run as a non-root user with sudo privileges on Ubuntu 16.04. If the $EUID suggestion worked in the context of sudo, the second command would have printed 0 instead of a blank line.



$ /tmp/a.sh
1000
$ sudo /tmp/a.sh

$


FYI, an example of the related posts I am referencing is:



How can a script check if it's being run as root?





This question already has an answer here:




  • How can a script check if it's being run as root?

    10 answers








bash






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 30 at 18:23









Colin McRaeColin McRae

212




212




marked as duplicate by Sergiy Kolodyazhnyy bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 31 at 0:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.









marked as duplicate by Sergiy Kolodyazhnyy bash
Users with the  bash badge can single-handedly close bash questions as duplicates and reopen them as needed.

StackExchange.ready(function() {
if (StackExchange.options.isMobile) return;

$('.dupe-hammer-message-hover:not(.hover-bound)').each(function() {
var $hover = $(this).addClass('hover-bound'),
$msg = $hover.siblings('.dupe-hammer-message');

$hover.hover(
function() {
$hover.showInfoMessage('', {
messageElement: $msg.clone().show(),
transient: false,
position: { my: 'bottom left', at: 'top center', offsetTop: -7 },
dismissable: false,
relativeToBody: true
});
},
function() {
StackExchange.helpers.removeMessages();
}
);
});
});
Jan 31 at 0:56


This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.










  • 1





    echo $UID should work. Or according to David F in the comments of your linked answer you should use echo $(id -u)

    – Terrance
    Jan 30 at 18:32













  • Thanks Terrance, that works too. I posted an answer after noticing a fat-finger that causes $EUID and $UID to resolve properly only for non-root users (still a bit of a mystery).

    – Colin McRae
    Jan 30 at 18:43













  • There are multiple other solutions to detect script running as root on the question to which you referred, if that's really what you're asking. If you are asking specifically why $EUID doesn't work, please edit the question to clarify the title and the body of the question, and we can re-open it

    – Sergiy Kolodyazhnyy
    Jan 31 at 0:58














  • 1





    echo $UID should work. Or according to David F in the comments of your linked answer you should use echo $(id -u)

    – Terrance
    Jan 30 at 18:32













  • Thanks Terrance, that works too. I posted an answer after noticing a fat-finger that causes $EUID and $UID to resolve properly only for non-root users (still a bit of a mystery).

    – Colin McRae
    Jan 30 at 18:43













  • There are multiple other solutions to detect script running as root on the question to which you referred, if that's really what you're asking. If you are asking specifically why $EUID doesn't work, please edit the question to clarify the title and the body of the question, and we can re-open it

    – Sergiy Kolodyazhnyy
    Jan 31 at 0:58








1




1





echo $UID should work. Or according to David F in the comments of your linked answer you should use echo $(id -u)

– Terrance
Jan 30 at 18:32







echo $UID should work. Or according to David F in the comments of your linked answer you should use echo $(id -u)

– Terrance
Jan 30 at 18:32















Thanks Terrance, that works too. I posted an answer after noticing a fat-finger that causes $EUID and $UID to resolve properly only for non-root users (still a bit of a mystery).

– Colin McRae
Jan 30 at 18:43







Thanks Terrance, that works too. I posted an answer after noticing a fat-finger that causes $EUID and $UID to resolve properly only for non-root users (still a bit of a mystery).

– Colin McRae
Jan 30 at 18:43















There are multiple other solutions to detect script running as root on the question to which you referred, if that's really what you're asking. If you are asking specifically why $EUID doesn't work, please edit the question to clarify the title and the body of the question, and we can re-open it

– Sergiy Kolodyazhnyy
Jan 31 at 0:58





There are multiple other solutions to detect script running as root on the question to which you referred, if that's really what you're asking. If you are asking specifically why $EUID doesn't work, please edit the question to clarify the title and the body of the question, and we can re-open it

– Sergiy Kolodyazhnyy
Jan 31 at 0:58










2 Answers
2






active

oldest

votes


















1














The script /tmp/a.sh only works with #!/bin/bash on the first line. When actually running the example I gave, the ! was accidentally omitted and the only user reported was the non-root user as shown in the question.






share|improve this answer



















  • 2





    Probably because without the shebang, the script was interpreted by your current shell (likely bash) when called without sudo, but was interpreted by dash (which doesn't set EUID) when called with sudo

    – steeldriver
    Jan 30 at 18:49











  • @steeldriver Confirmed. On my Ubuntu 18.04 virtual machine the script with ls -l /proc/$$/exe reports the script is run via /bin/dash, however on my other debian-based system root has bash as login shell and that's what is reported there. So sudo checks either $SHELL environment variable or /etc/passwd or other login system such as LDAP

    – Sergiy Kolodyazhnyy
    Jan 31 at 1:14











  • @SergiyKolodyazhnyy it's kind of interesting that sudo appears to use the login shell specified in the target user's passwd entry even when not explicitly invoked with the -i (or --login) option - I'd never really thought about that before

    – steeldriver
    Jan 31 at 14:22











  • @steeldriver Correction: it defaults to /bin/sh . I've repeated the experiment couple times again: /proc/19051/exe -> /bin/dash is reported on both systems, so previous conclusion is mistaken.

    – Sergiy Kolodyazhnyy
    Feb 1 at 0:34



















0














I use the following in some of my scripts:



#!/bin/bash

if [ "$(id -un)" != "root" ]; then
echo "Need root - sudoing..."
exec sudo "$0" "$@"
fi
# Now we are root






share|improve this answer






























    2 Answers
    2






    active

    oldest

    votes








    2 Answers
    2






    active

    oldest

    votes









    active

    oldest

    votes






    active

    oldest

    votes









    1














    The script /tmp/a.sh only works with #!/bin/bash on the first line. When actually running the example I gave, the ! was accidentally omitted and the only user reported was the non-root user as shown in the question.






    share|improve this answer



















    • 2





      Probably because without the shebang, the script was interpreted by your current shell (likely bash) when called without sudo, but was interpreted by dash (which doesn't set EUID) when called with sudo

      – steeldriver
      Jan 30 at 18:49











    • @steeldriver Confirmed. On my Ubuntu 18.04 virtual machine the script with ls -l /proc/$$/exe reports the script is run via /bin/dash, however on my other debian-based system root has bash as login shell and that's what is reported there. So sudo checks either $SHELL environment variable or /etc/passwd or other login system such as LDAP

      – Sergiy Kolodyazhnyy
      Jan 31 at 1:14











    • @SergiyKolodyazhnyy it's kind of interesting that sudo appears to use the login shell specified in the target user's passwd entry even when not explicitly invoked with the -i (or --login) option - I'd never really thought about that before

      – steeldriver
      Jan 31 at 14:22











    • @steeldriver Correction: it defaults to /bin/sh . I've repeated the experiment couple times again: /proc/19051/exe -> /bin/dash is reported on both systems, so previous conclusion is mistaken.

      – Sergiy Kolodyazhnyy
      Feb 1 at 0:34
















    1














    The script /tmp/a.sh only works with #!/bin/bash on the first line. When actually running the example I gave, the ! was accidentally omitted and the only user reported was the non-root user as shown in the question.






    share|improve this answer



















    • 2





      Probably because without the shebang, the script was interpreted by your current shell (likely bash) when called without sudo, but was interpreted by dash (which doesn't set EUID) when called with sudo

      – steeldriver
      Jan 30 at 18:49











    • @steeldriver Confirmed. On my Ubuntu 18.04 virtual machine the script with ls -l /proc/$$/exe reports the script is run via /bin/dash, however on my other debian-based system root has bash as login shell and that's what is reported there. So sudo checks either $SHELL environment variable or /etc/passwd or other login system such as LDAP

      – Sergiy Kolodyazhnyy
      Jan 31 at 1:14











    • @SergiyKolodyazhnyy it's kind of interesting that sudo appears to use the login shell specified in the target user's passwd entry even when not explicitly invoked with the -i (or --login) option - I'd never really thought about that before

      – steeldriver
      Jan 31 at 14:22











    • @steeldriver Correction: it defaults to /bin/sh . I've repeated the experiment couple times again: /proc/19051/exe -> /bin/dash is reported on both systems, so previous conclusion is mistaken.

      – Sergiy Kolodyazhnyy
      Feb 1 at 0:34














    1












    1








    1







    The script /tmp/a.sh only works with #!/bin/bash on the first line. When actually running the example I gave, the ! was accidentally omitted and the only user reported was the non-root user as shown in the question.






    share|improve this answer













    The script /tmp/a.sh only works with #!/bin/bash on the first line. When actually running the example I gave, the ! was accidentally omitted and the only user reported was the non-root user as shown in the question.







    share|improve this answer












    share|improve this answer



    share|improve this answer










    answered Jan 30 at 18:41









    Colin McRaeColin McRae

    212




    212








    • 2





      Probably because without the shebang, the script was interpreted by your current shell (likely bash) when called without sudo, but was interpreted by dash (which doesn't set EUID) when called with sudo

      – steeldriver
      Jan 30 at 18:49











    • @steeldriver Confirmed. On my Ubuntu 18.04 virtual machine the script with ls -l /proc/$$/exe reports the script is run via /bin/dash, however on my other debian-based system root has bash as login shell and that's what is reported there. So sudo checks either $SHELL environment variable or /etc/passwd or other login system such as LDAP

      – Sergiy Kolodyazhnyy
      Jan 31 at 1:14











    • @SergiyKolodyazhnyy it's kind of interesting that sudo appears to use the login shell specified in the target user's passwd entry even when not explicitly invoked with the -i (or --login) option - I'd never really thought about that before

      – steeldriver
      Jan 31 at 14:22











    • @steeldriver Correction: it defaults to /bin/sh . I've repeated the experiment couple times again: /proc/19051/exe -> /bin/dash is reported on both systems, so previous conclusion is mistaken.

      – Sergiy Kolodyazhnyy
      Feb 1 at 0:34














    • 2





      Probably because without the shebang, the script was interpreted by your current shell (likely bash) when called without sudo, but was interpreted by dash (which doesn't set EUID) when called with sudo

      – steeldriver
      Jan 30 at 18:49











    • @steeldriver Confirmed. On my Ubuntu 18.04 virtual machine the script with ls -l /proc/$$/exe reports the script is run via /bin/dash, however on my other debian-based system root has bash as login shell and that's what is reported there. So sudo checks either $SHELL environment variable or /etc/passwd or other login system such as LDAP

      – Sergiy Kolodyazhnyy
      Jan 31 at 1:14











    • @SergiyKolodyazhnyy it's kind of interesting that sudo appears to use the login shell specified in the target user's passwd entry even when not explicitly invoked with the -i (or --login) option - I'd never really thought about that before

      – steeldriver
      Jan 31 at 14:22











    • @steeldriver Correction: it defaults to /bin/sh . I've repeated the experiment couple times again: /proc/19051/exe -> /bin/dash is reported on both systems, so previous conclusion is mistaken.

      – Sergiy Kolodyazhnyy
      Feb 1 at 0:34








    2




    2





    Probably because without the shebang, the script was interpreted by your current shell (likely bash) when called without sudo, but was interpreted by dash (which doesn't set EUID) when called with sudo

    – steeldriver
    Jan 30 at 18:49





    Probably because without the shebang, the script was interpreted by your current shell (likely bash) when called without sudo, but was interpreted by dash (which doesn't set EUID) when called with sudo

    – steeldriver
    Jan 30 at 18:49













    @steeldriver Confirmed. On my Ubuntu 18.04 virtual machine the script with ls -l /proc/$$/exe reports the script is run via /bin/dash, however on my other debian-based system root has bash as login shell and that's what is reported there. So sudo checks either $SHELL environment variable or /etc/passwd or other login system such as LDAP

    – Sergiy Kolodyazhnyy
    Jan 31 at 1:14





    @steeldriver Confirmed. On my Ubuntu 18.04 virtual machine the script with ls -l /proc/$$/exe reports the script is run via /bin/dash, however on my other debian-based system root has bash as login shell and that's what is reported there. So sudo checks either $SHELL environment variable or /etc/passwd or other login system such as LDAP

    – Sergiy Kolodyazhnyy
    Jan 31 at 1:14













    @SergiyKolodyazhnyy it's kind of interesting that sudo appears to use the login shell specified in the target user's passwd entry even when not explicitly invoked with the -i (or --login) option - I'd never really thought about that before

    – steeldriver
    Jan 31 at 14:22





    @SergiyKolodyazhnyy it's kind of interesting that sudo appears to use the login shell specified in the target user's passwd entry even when not explicitly invoked with the -i (or --login) option - I'd never really thought about that before

    – steeldriver
    Jan 31 at 14:22













    @steeldriver Correction: it defaults to /bin/sh . I've repeated the experiment couple times again: /proc/19051/exe -> /bin/dash is reported on both systems, so previous conclusion is mistaken.

    – Sergiy Kolodyazhnyy
    Feb 1 at 0:34





    @steeldriver Correction: it defaults to /bin/sh . I've repeated the experiment couple times again: /proc/19051/exe -> /bin/dash is reported on both systems, so previous conclusion is mistaken.

    – Sergiy Kolodyazhnyy
    Feb 1 at 0:34













    0














    I use the following in some of my scripts:



    #!/bin/bash

    if [ "$(id -un)" != "root" ]; then
    echo "Need root - sudoing..."
    exec sudo "$0" "$@"
    fi
    # Now we are root






    share|improve this answer




























      0














      I use the following in some of my scripts:



      #!/bin/bash

      if [ "$(id -un)" != "root" ]; then
      echo "Need root - sudoing..."
      exec sudo "$0" "$@"
      fi
      # Now we are root






      share|improve this answer


























        0












        0








        0







        I use the following in some of my scripts:



        #!/bin/bash

        if [ "$(id -un)" != "root" ]; then
        echo "Need root - sudoing..."
        exec sudo "$0" "$@"
        fi
        # Now we are root






        share|improve this answer













        I use the following in some of my scripts:



        #!/bin/bash

        if [ "$(id -un)" != "root" ]; then
        echo "Need root - sudoing..."
        exec sudo "$0" "$@"
        fi
        # Now we are root







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jan 30 at 22:26









        RalfRalf

        20116




        20116















            Popular posts from this blog

            flock() on closed filehandle LOCK_FILE at /usr/bin/apt-mirror

            Mangá

            Eduardo VII do Reino Unido