|
| 1 | +--- |
| 2 | +title: 'File permissions' |
| 3 | +description: 'A short overview of file permissions' |
| 4 | +hasSlides: false |
| 5 | +links: { |
| 6 | + 'Permissions in-depth': 'https://www.linuxfoundation.org/blog/blog/classic-sysadmin-understanding-linux-file-permissions', |
| 7 | +} |
| 8 | +--- |
| 9 | + |
| 10 | +## Access control |
| 11 | + |
| 12 | +Multi-user system require a way to control what level of access |
| 13 | +each user has to a specific file or directory. |
| 14 | + |
| 15 | +In Unix-like systems the access is governed through file permissions. |
| 16 | + |
| 17 | +## Viewing file permissions |
| 18 | + |
| 19 | +Use the `ls -l /path/to/file` to view the permissions |
| 20 | + |
| 21 | +```bash |
| 22 | +ls -alh /bin/bash |
| 23 | +# prints: -rwxr-xr-x 1 root root 1.2M Aug 1 22:56 /bin/bash |
| 24 | +``` |
| 25 | + |
| 26 | +--- |
| 27 | + |
| 28 | +## How are permissions structured |
| 29 | + |
| 30 | +There are 3 types of access: |
| 31 | + |
| 32 | +- Read - view the contents of a file / directory |
| 33 | +- Write - modify the contents of a file / directory |
| 34 | +- Execute - execute a file (run as command) / open a directory |
| 35 | + |
| 36 | +--- |
| 37 | + |
| 38 | +In Unix-like systems permissions are applied on 3 levels: |
| 39 | + |
| 40 | +- the user owning the file |
| 41 | +- the group owning the file |
| 42 | +- anyone else |
| 43 | + |
| 44 | +--- |
| 45 | + |
| 46 | +When listing permissions we see that are 3 triplets: |
| 47 | + |
| 48 | +_spaces added for clarity_ |
| 49 | + |
| 50 | +``` |
| 51 | +rwx r-x r-- |
| 52 | +``` |
| 53 | + |
| 54 | +In this example: |
| 55 | + |
| 56 | +- The user has full access |
| 57 | +- Members of the group can read and execute |
| 58 | +- Everyone else can only read |
| 59 | + |
| 60 | +--- |
| 61 | + |
| 62 | +### Permissions as octal numbers |
| 63 | + |
| 64 | +Another way to represent the same information is using octal numbers: |
| 65 | + |
| 66 | +``` |
| 67 | +rwx r-x r-- |
| 68 | +
|
| 69 | +# same as |
| 70 | +
|
| 71 | +754 |
| 72 | +``` |
| 73 | + |
| 74 | +--- |
| 75 | + |
| 76 | +This is achieved by assigning a unique, complimentary number to each permission and summing them: |
| 77 | + |
| 78 | +- Read: 4 |
| 79 | +- Write: 2 |
| 80 | +- Execute: 1 |
| 81 | + |
| 82 | +So full access is 4+2+1 = 7 |
| 83 | + |
| 84 | +--- |
| 85 | + |
| 86 | +## Modifying permissions |
| 87 | + |
| 88 | +Use the `chmod` command to set the permissions for a file / directory: |
| 89 | + |
| 90 | +```bash |
| 91 | +# add the execute permissions for all levels to a file |
| 92 | +chmod +x filename |
| 93 | + |
| 94 | +# remove the execute permissions for all levels to a file |
| 95 | +chmod -x filename |
| 96 | +``` |
| 97 | + |
| 98 | +--- |
| 99 | + |
| 100 | +Use the octal representation to set the full permissions of a file / directory: |
| 101 | + |
| 102 | +```bash |
| 103 | +# only the owning user has full access to the file |
| 104 | +chmod 700 filename |
| 105 | +``` |
0 commit comments