Setting the setuid and setgid bits with chmod

You have perhaps, by now, thought okay, but how exactly do I set these special permission bits?

This is simple: you use the chmod(1) command (or system call); this table shows how chmod can be used to set the setuid/setgid permission bits:

chmod via: Notation for setuid Notation for setgid
symbolic notation u+s g+s
octal notation 4<octal #> (eg. 4755) 2<octal #> (eg. 2755)

As a trivial example, take a simple Hello, world C program and compile it:

gcc hello.c -o hello

Now we set the setuid bit, then remove it, and set the setgid bit instead (in one operation: via the u-s,g+s parameter to chmod), then remove the setgid bit, all the while long-listing the binary executable so that the ...

Get Hands-On System Programming with Linux now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.