Good people! 🙂 First of all, it is worth mentioning that it is my first contribution to the community, I hope someone will find it useful
=> Basic structure of permissions in files
=> Basic structure of permissions in directories
=> User, Groups and Others
=> Chmod octal
Table of Contents
1.- Basic structure of permissions in files
There are 3 basic attributes for simple files: read, write, and execute.
>> Read permission (read)
If you have permission to read a file, you can see its content.
>> Write permission (write)
If you have permission to write a file, you can modify the file. You can add, overwrite or delete its content.
>> Execute permission (execute)
If the file has execute permission, then you can tell the operating system to run it as if it were a program. If it is a program called "foo" we can execute it as any command.
Or a script (interpreter) that needs read and execute permission, a compiled program only needs to be read.
The characters attributed to the permissions are:
r means writing and comes from Read
w means reading and comes from Write
x means execution and comes from eXcut
Using chmod to change permissions
chmod (change mode) is the command used to change permissions, you can add or remove permissions to one or more files with + (plus) or - (minus)
If you want to prevent yourself from modifying an important file, simply remove the write permission on your "file" with the chmod command
$ chmod -w yourFile
if you want to make an executable script, write
$ chmod + x tuScript
if you want to remove or add all attributes at once
$ chmod -rwx file $ chmod + rwx file
you can also use the = sign (equal) to set the permissions in an exact combination, this command removes the write and execute permissions leaving only the read one
$ chmod = r file
2.- Basic structure of permissions in directories
In the case of directories we have the same permissions, but with a different meaning.
>> Read permission on a directory
If a directory has read permission, you can see the files it contains. You can use an "ls (list directory)" to see its content, just because you have read permission on a directory does not mean that you can read the content of its files if you don't have read permission on those.
>> Write permission on a directory.
With write permission you can add, remove or move files to the directory
>> Execute permission on a directory.
Execution allows you to use the name of the directory when you are accessing files in that directory, that is, this permission makes it take into account in searches carried out by a program, for example, a directory without execution permission would not be checked by the command find
3.- Users, Groups and Others
Now we know the 3 permissions and how to add or remove them, but these 3 permissions are stored in 3 different places called.
User (u) comes from user
Group (g) comes from group
Others (or) comes from other
When you run
$ chmod = r file
Change the permissions in 3 places, when you list directories with "ls -l" you will see something similar to.
-r - r - r-- 1 wada users 4096 Apr 13 19:30 file
note those 3 r's for the 3 different types of permits
x ------------- x ------------- x | permissions | belongs | x ------------- x ------------- x | rwx ------ | user | | --- rx --- | group | | ------ rx | other | x ------------- x ------------- x
we can remove permits for each owner; suppose we have a file:
-rwxr-xr-x 1 wada users 4096 Apr 13 19:30 file
To remove the execution permissions to groups and others, just use:
$ chmod gx, ox file
our file will have these permissions
-rwxr - r-- 1 wada users 4096 Apr 13 19:30 file
if you want to remove user write permission:
$ chmod ux file
-r-xr - r-- 1 wada users 4096 Apr 13 19:30 file
Adding and removing two permissions at the same time:
$ chmod u-x + w file
-rw-r - r-- 1 wada users 4096 Apr 13 19:30 file
Very simple right?
4.- chmod in octal
The octal representation of chmod is very simple
Reading has the value of 4
Writing has the value of 2
Execution has the value of 1
x ----- x ----- x ----------------------------------- x | rwx | 7 | Read, write and execute | | rw- | 6 | Reading, writing | | rx | 5 | Reading and execution | | r-- | 4 | Reading | | -wx | 3 | Writing and execution | | -w- | 2 | Writing | | --x | 1 | Execution | | --- | 0 | No permissions | x ----- x ----- x ----------------------------------- x
x ------------------------ x ----------- x | chmod u = rwx, g = rwx, o = rx | chmod 775 | | chmod u = rwx, g = rx, o = | chmod 760 | | chmod u = rw, g = r, o = r | chmod 644 | | chmod u = rw, g = r, o = | chmod 640 | | chmod u = rw, go = | chmod 600 | | chmod u = rwx, go = | chmod 700 | x ------------------------ x ----------- x