Based on the Linux Master 2 exam syllabusReport an error
s and t. SetUID is in the owner's x slot, SetGID is in the group's x slot, and Sticky is in the other's x slot./usr/bin/passwd, Sticky bit is /tmp.The 755 we've seen so far is actually the last three digits. There is one more digit in front.
4 7 5 5
│ │ │ │
│ │ │ └── other
│ │ └──────── group
│ └────────────── user
└──────────────────── special permission| Special permission | Value | Where it attaches | Display |
|---|---|---|---|
| SetUID | 4 | x of user | s |
| SetGID | 2 | x of group | s |
| Sticky bit | 1 | x of other | t |
chmod 755 and chmod 0755 are the same.chmod 4755 program # octal method
chmod u+s program # symbolic method -rwsr-xr-x 1 root root ... /usr/bin/passwd
└ the s in the owner's x slot = SetUID/usr/bin/passwd. Passwords are in /etc/shadow, which only root can read, yet a regular user must be able to change their own password. Because SetUID is set on passwd, the moment it runs it can modify shadow with root privileges.$ ls -l /etc/shadow /usr/bin/passwd
----------. 1 root root 1284 Sep 3 09:00 /etc/shadow
-rwsr-xr-x. 1 root root 27856 Sep 3 09:00 /usr/bin/passwd| Step | What happens |
|---|---|
| ① | A regular user alice runs passwd (it is executable because the other slot has x) |
| ② | Because the owner slot is s, this process runs with the privileges of the file's owner, root, not alice |
| ③ | With root privileges, it can modify /etc/shadow, which alice can't even read |
| ④ | When passwd ends, those privileges end too. alice's shell is still alice |
bob, it just runs with bob's privileges. It is not "SetUID = root privileges" but "SetUID = owner's privileges," and it becomes dangerous when the owner is root.passwd, SetUID programs include su and sudo — commands that a regular user runs but that can only do their job with administrator privileges.chmod 2755 program # on a file
chmod g+s /project # on a directorySetGID does different things when placed on a file and on a directory.
| Target | Effect |
|---|---|
| File | While running, it operates with the privileges of the file's owning group |
| Directory | Files created inside it inherit the directory's group |
-rwxr-sr-x 1 root tty ... /usr/bin/wall
└ the s in the group's x slot = SetGIDsudo chgrp dev /project
sudo chmod 2770 /project # SetGID + rwxrwx---
# now whoever creates a file inside /project, the group is always devPutting before and after side by side makes the difference clear.
$ ls -ld /project
drwxrws---. 2 root dev 6 Sep 3 09:00 /project ← s in the group slot
$ id -gn
alice
$ touch /project/plan.txt
$ mkdir /project/docs
$ ls -l /project
drwxr-sr-x. 2 alice dev 6 Sep 3 09:06 docs
-rw-r--r--. 1 alice dev 0 Sep 3 09:05 plan.txtalice, the new file's group is dev. Yesterday this was alice.docs created inside also inherits SetGID (drwxr-sr-x). So the group is maintained even deeper down.chgrp -R dev /project.plan.txt above has no w for the group. To let members edit each other's files, set umask to 002 or add chmod g+w.chmod 1777 /shared
chmod o+t /shared drwxrwxrwt 10 root root ... /tmp
└ the t in the other's x slot = Sticky bitw permission, by default you can even delete other people's files. That's because, regardless of the file's own permissions, write permission on the directory lets you delete the entries inside./tmp. Everyone must be able to create temporary files, but must not delete other people's temporary files, so it is set to 1777.$ ls -l /tmp/a.txt
-rw-r--r--. 1 alice alice 6 Sep 3 09:00 /tmp/a.txt
$ whoami
bob
$ cat /tmp/a.txt ← the file's other slot has r, so reading works
hello
$ rm -f /tmp/a.txt
rm: cannot remove '/tmp/a.txt': Operation not permitted
$ mv /tmp/a.txt /tmp/b.txt
mv: cannot move '/tmp/a.txt' to '/tmp/b.txt': Operation not permitted/tmp, /var/tmp is also 1777.| Directory permission | Anyone can create files | Delete others' files |
|---|---|---|
777 (rwxrwxrwx) | O | O — dangerous |
1777 (rwxrwxrwt) | O | X |
755 (rwxr-xr-x) | X (owner only) | X |
| Kind | Value | Display | Position | Signature example | What it does |
|---|---|---|---|---|---|
| SetUID | 4 | s | user x | /usr/bin/passwd | Runs with owner's privileges |
| SetGID | 2 | s | group x | /usr/bin/wall | Runs with group's privileges / group inheritance |
| Sticky | 1 | t | other x | /tmp | Can delete only your own files |
If there is originally no x in that slot, it is shown as uppercase S or T.
| Display | Meaning |
|---|---|
s / t | Special permission set + execute permission present |
S / T | Special permission set + no execute permission |
-rwSr--r-- is a state where SetUID is set but the owner has no execute permission. It's usually a configuration mistake.s and t are marks that borrow the x slot. Since two pieces of information (execute permission + special permission) have to be written overlapped in one slot, they are distinguished by case.
| Character in that slot | Execute permission | Special permission |
|---|---|---|
x | Yes | No |
- | No | No |
s / t | Yes | Yes |
S / T | No | Yes |
$ chmod 4644 a.sh ; ls -l a.sh
-rwSr--r--. 1 user user 0 Sep 3 09:00 a.sh ← 644 has no x, so uppercase S
$ chmod u+x a.sh ; ls -l a.sh
-rwsr--r--. 1 user user 0 Sep 3 09:00 a.sh ← once x is added, lowercase sRead a four-digit value by splitting it into the first digit (special) + the last three digits (regular). When converting notation to a number, first treat s and t as x, work out the three digits, and then attach the front digit.
| Notation | Special | Regular | Value |
|---|---|---|---|
rwsr-xr-x | SetUID 4 | rwxr-xr-x 755 | 4755 |
rwxr-sr-x | SetGID 2 | rwxr-xr-x 755 | 2755 |
rwxrwxrwt | Sticky 1 | rwxrwxrwx 777 | 1777 |
rwxrws--- | SetGID 2 | rwxrwx--- 770 | 2770 |
rwsr-sr-x | SetUID 4 + SetGID 2 | rwxr-xr-x 755 | 6755 |
rwxrwsrwt | SetGID 2 + Sticky 1 | rwxrwxrwx 777 | 3777 |
rwSr--r-- | SetUID 4 | rw-r--r-- 644 | 4644 |
s is in — if in the first three slots, SetUID; if in the second three slots, SetGID. t is always in the last slot.| What you want to do | Symbolic method | Octal method (based on 755) |
|---|---|---|
| Set SetUID | chmod u+s file | chmod 4755 file |
| Set SetGID | chmod g+s target | chmod 2755 target |
| Set Sticky bit | chmod o+t directory (+t works too) | chmod 1755 directory |
| Clear SetUID | chmod u-s file | chmod 0755 file |
| Clear SetGID | chmod g-s target | — |
| Clear Sticky bit | chmod o-t directory | — |
s to u or g, and t to o. chmod u+t and chmod o+s are combinations that make no sense.find / -perm -4000 -type f # every file with SetUID set
find / -perm -2000 -type f # SetGID
find / -perm -1000 -type d # Sticky bit
ls -ld /tmp # drwxrwxrwt
ls -l /usr/bin/passwd # -rwsr-xr-xThe range that gets found changes depending on the symbol in front of the number after -perm.
| Usage | What it finds |
|---|---|
-perm 4755 | Permissions are exactly 4755 |
-perm -4000 | The SetUID bit is on, whatever the other permissions are |
-perm -6000 | Both SetUID and SetGID are on |
-perm /6000 | Either one of SetUID and SetGID is on |
-perm 4000, it looks for "files whose permissions are exactly 4000 (---S------)," so nothing comes out. Adding the - makes it "those that include that bit."$ stat -c '%a %A %n' /tmp /usr/bin/passwd
1777 drwxrwxrwt /tmp
4755 -rwsr-xr-x /usr/bin/passwdTomorrow we cover the space where files are actually stored — the commands for attaching, detaching, and checking file systems.
/tmp : The classic shared temporary directory with the Sticky bit set. Its permission is 1777./usr/bin/passwd : The classic command with SetUID set. It lets a regular user modify /etc/shadow.Click a choice to reveal the answer and explanation.
Question 1
Which of the following correctly lists the octal values for SetUID, SetGID, and Sticky bit, in order?
Question 2
Which of the following permissions is set on the /tmp directory so that you can delete only the files you created?
Question 3
Which of the following is a correct description of a file with the permission -rwsr-xr-x?
Question 4
Which of the following is the effect when SetGID is set on a directory?
Question 5
Which of the following commands grants the SetUID permission on a file?