My os is Fedora 24 and I tried to test the suid bit functionality.

I wrote below bash as Setuid.bash:

if [ $USER = 'root' ]
    echo "Like Root Run"
    echo "Root User Add in " $(date) >> /etc/SetUid
    echo "Other User Run"
    echo $USER "User Add in " $(date) >> /etc/SetUid

with -rwsrw-r-x. 1 root root 249 May 21 14:45 /bin/Setuid.bash permission and -rwx------. 1 root root 432 May 21 14:45 /etc/SetUid

Now, when I tried /bin/Setuid.bash as root I got:

Like Root Run

but when run that with Test user I encountered:

Other User Run

bin/Setuid.bash: line 8: /etc/SetUid: Permission denied

I'll appreciate if any one let me know, where is my way wrong?


Long time ago, bash (and other shell interpreters) had added built-in security measures again suid (ab)use because it is/was dangerous. Dangers of SUID Shell Scripts

From Why Bash is like that: suid

Bash scripts can’t run with the suid bit set. First of all, Linux doesn’t allow any scripts to be setuid, though some other OS do. Second, bash will detect being run as setuid, and immediately drop the privileges.

This is because shell script security is extremely dependent on the environment, much more so than regular C apps.

Ultimately, nowadays the suid bit is mostly useful for executable binaries. A way of running a script/bash like that is invoking it from a suid compiled binary.

|improve this answer|||||
  • thanks man for the answer, but when I can use this option? – Hossein Vatani May 21 '17 at 12:03
  • added to the answer. – Rui F Ribeiro May 21 '17 at 12:04
  • that is invoking it from a suid compiled binary you meant I should use a binary file to get the correct answer? – Hossein Vatani May 21 '17 at 12:09
  • 1
    Yes, for instance writing in C a wrapper that calls bash stackoverflow.com/questions/556194/… – Rui F Ribeiro May 21 '17 at 12:12
  • You only refer to the Bash shell. No suid applies to any shell script - not just the Bash shell. – fpmurphy May 21 '17 at 13:34

Not the answer you're looking for? Browse other questions tagged or ask your own question.