Blame
|
1 | # CHMOD |
||||||
| 2 | ||||||||
| 3 | The chmod command is used to change file and directory permissions in Linux and Unix-based systems. Permissions can be specified using either numeric (octal) or symbolic (letter) notation. |
|||||||
| 4 | ||||||||
| 5 | ## Numeric (Octal) Notation |
|||||||
| 6 | ||||||||
| 7 | Each permission is represented by a number: |
|||||||
| 8 | ``` |
|||||||
| 9 | 4 = Read (r) |
|||||||
| 10 | 2 = Write (w) |
|||||||
| 11 | 1 = Execute (x) |
|||||||
| 12 | ``` |
|||||||
| 13 | ||||||||
| 14 | The values are added together for each user category: |
|||||||
| 15 | ``` |
|||||||
| 16 | Owner |
|||||||
| 17 | Group |
|||||||
| 18 | Others |
|||||||
| 19 | ``` |
|||||||
| 20 | ||||||||
| 21 | Example: `chmod 755 script.sh` |
|||||||
| 22 | ||||||||
| 23 | This results in: |
|||||||
| 24 | ``` |
|||||||
| 25 | Owner: rwx (7 = 4+2+1) |
|||||||
| 26 | Group: r-x (5 = 4+1) |
|||||||
| 27 | Others: r-x (5 = 4+1) |
|||||||
| 28 | ``` |
|||||||
| 29 | ||||||||
| 30 | ## Symbolic (Letter) Notation |
|||||||
| 31 | ||||||||
| 32 | Permissions can also be modified using letters: |
|||||||
| 33 | ``` |
|||||||
| 34 | u = User (owner) |
|||||||
| 35 | g = Group |
|||||||
| 36 | o = Others |
|||||||
| 37 | a = All users |
|||||||
| 38 | ``` |
|||||||
| 39 | ||||||||
| 40 | Operators: |
|||||||
| 41 | ``` |
|||||||
| 42 | + = Add permission |
|||||||
| 43 | - = Remove permission |
|||||||
| 44 | = = Set exact permission |
|||||||
| 45 | ``` |
|||||||
| 46 | ||||||||
| 47 | Examples: |
|||||||
| 48 | ``` |
|||||||
| 49 | chmod u+x script.sh # Add execute permission for the owner |
|||||||
| 50 | chmod g-w file.txt # Remove write permission from the group |
|||||||
| 51 | chmod a=r file.txt # Set read-only permission for everyone |
|||||||
| 52 | ``` |
|||||||
| 53 | ||||||||
| 54 | Both notations achieve the same result. Numeric notation is commonly used for setting complete permission sets, while symbolic notation is useful for modifying specific permissions. |
|||||||
|
55 | |||||||
| 56 | ## Special Bits |
|||||||
| 57 | | Bit | Octal | Letter | on file | on directory | |
|||||||
| 58 | | - | - | - | - | - | |
|||||||
| 59 | | Setuid | 4 | u+s | program runs with uid of file | --- | |
|||||||
| 60 | | Setgid | 2 | g+s | program runs with gid of file | directory passes group of directory to new files/directories beneath | |
|||||||
| 61 | | Sticky | 1 | +t | --- | the files within the directory are only deletable and renamable from owner of directory or file | |
|||||||
| 62 | ||||||||
| 63 | example: |
|||||||
| 64 | ``` |
|||||||
| 65 | 3770 Setgid + Sticky-Bit + rwx for user, rwx for group and none for other |
|||||||
| 66 | ``` |
|||||||
