1

I have a simple shutdown script which i want to run as root

shutdown -h +30;
echo "succesfull";

I have followed these answer1,2 to make my script root.

first I run this command

sudo chown root.root $HOME/test/test.sh
sudo chmod 4755 $HOME/test/test.sh

and then made changes in sudoers

sudo visudo

added this command after this line %sudo ALL=(ALL:ALL) ALL

eka ALL=(ALL) NOPASSWD: $HOME/test/test.sh

But when i executed my script its showing this error

shutdown: Need to be root
succesfull
Eka
  • 2,967
  • 12
  • 38
  • 60
  • chmod 4755 - I assume you meant to make a setuid script? Scripts can't be made setuid. – muru May 22 '15 at 09:07
  • @muru I was following the answer 1 – Eka May 22 '15 at 09:12
  • That user is mistaken. setuid doesn't work on scripts. Period. – muru May 22 '15 at 09:17
  • @muru thanks for the info.. but how to revert back to normal executable script whether this code be enough sudo chmod +x $HOME/test/test.sh – Eka May 22 '15 at 09:19
  • 2
    You need to do chmod u-s, but as I said, it doesn't matter - setuid doesn't affect scripts. – muru May 22 '15 at 09:20

1 Answers1

4

Don't use setuid shell scripts, the SUID bit is not honored on shell scripts anyway on current systems. Use sudo instead, as you attempted anyway:

eka   ALL=NOPASSWD: /home/eka/test/test.sh

And then:

eka$ sudo ~/test/test.sh

With that, test.sh will be executed as "root". No need to use setuid here.

ckujau
  • 341