Setuid and setgid
Some programs, such as passwd, need to run as a specific user to work properly:
$ which -l passwd -rwsrwxr-x 1 root root 21544 Mar 30 23:34 /usr/bin/passwd
Notice that the third character in the owner's permissions is s. This indicates a setuid ("set user ID") command; when you run passwd, the program runs as the owner of the file (that is, root). An S means that the setuid bit is set for the file, but the execute bit isn't set.
You might also find some setgid ("set group ID") commands, which run with the same group ID as the owner of the file, but not with the owner's user ID. If setgid is set on a directory, files created in the directory have the directory's group ID, not that of the file's creator. This scheme is commonly used for spool areas, such as /usr/spool/mail, which is setgid and owned by the mail group, so that programs running as the mail group can update things there, but the files still belong to their normal owners.
If you change the ownership of a setuid command, the setuid bit is cleared, unless you're logged in as root. Similarly, if you change the group of a setgid command, the setgid bit is cleared, unless you're root.
Setuid and setgid commands can cause a security problem. If you create any, make sure that only the owner can write them, and that a malicious user can't hijack them—especially if root owns them.
Last modified: 2014-05-14