I have a VMWare VM that directly accesses some disks. In order to run (without running vmware as su which has its own problems) I need to change the ownership of the devices. I've done this manually using Nemo, so I know what needs to be performed.

Writing a shell script to do this, I wrote

chown john:john /dev/disk/by-id/blahblah  /dev/anotherblah  /dev/lastoneblah

I saved it as a file drive-owner, set the execute bit, then in a root shell changed its owner to root and set the +s special mode flag.

The result, according to ls, is -rwsr-sr-x.

If I run that from a normal (non-root) shell, I get errors from chown, Operation not permitted. It runs without complaint from a root shell.

I seem to be missing the part that runs the script as root automatically. I thought that was the purpose and function of the "setuid" permission, and +s is how to specify it.

What am I missing?


Setuid and setgid flags are ignored for shell scripts, for security reasons.

  • Undoubtedly correct in this case, but not universally true. – hildred Jul 22 '15 at 5:48
  • Well that would explain it. So how do I accomplish this? This is a personal computer, so security isn't as much of an issue. – JDługosz Jul 22 '15 at 6:41
  • @hildred Yes, that has only been true for most systems released in the past ~20 years. – Satō Katsura Jul 22 '15 at 7:12
  • @JDługosz Use sudo instead. – Satō Katsura Jul 22 '15 at 7:12
  • 2
    @JDługosz You can configure sudo not to ask for a password for chown when run by you. – Satō Katsura Jul 22 '15 at 10:39

Following these steps:

  • Try sudo
  • Try chmod +x script_path
  • If security does not matter, run chmod 777 script_path
  • You mean the directory that contains the script file? I had left the file in a convenient place related to its purpose, but I suppose it should go in a place where scripts are kept, like a utilities directory. What's the right place for it, esp. If I want to run it automatically when the machine is rebooted? – JDługosz Jul 22 '15 at 10:43
  • Hi Sorry for late reply, for the script to execute at the startup, you would have to put your script in /etc/init.d and create a symlink to /etc/rc.d . And of couse you would have to change the file pemission to executable. #chmod +x /etc/init.d/Script_name #ln -s /etc/init.d/script_name /etc/rc.d/ – Raghav Chadha Jul 30 '15 at 6:59

@hildred noted this in a comment above. I'll expand it into an Answer as I think it's the correct and highly recommended answer.

The super command

… allows specified users to execute scripts (or other commands) as if they were root; or it can set the uid, gid, and/or supplementary groups on a per-command basis before executing the command. It is intended to be a secure alternative to making scripts setuid root. Super also allows ordinary users to supply commands for execution by others; these execute with the uid, gid, and groups of the user offering the command.

(Emphasis mine).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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