I am interested in knowing the difference between these to know when to use which one? In the case that I need to modify the permissions of a directory or file on my localhost .
Note: I have read several topics in Spanish but I perceive that they explain in a banal way, I find better information in English but I do not fully understand them.
In the end I end up solving it with something that I consider "nacade" but it works:
$ sudo chmod 777 -R /ruta/
Although after time I have already tired of this solution and I prefer to do it correctly.
chmod
is to change the permissions (from English change mod e )chown
It is to change the owner (from the English change own er )To change permissions use the first one.
Permissions are represented by flags that indicate whether the permission is set or not:
r
read _ _w
write _ _x
execution _ _If the permission is not set, a dash appears
-
And they are grouped into three levels:
Additionally the first letter denotes if it is a directory or not
So a file that had all permissions would look like this:
And one that did not have any permissions would look like this:
The chmod command can receive as an argument a number that represents each group of permissions, for example
Gives all permissions to the file
Each permission group can be thought of as a binary number.
For example no permission would be 0
run only would be 1
write only, 2
write and execute, 3
read only, 4
read and execute, 5
reading and writing, 6
All permissions, 7
Another example: all permissions for the owner (
7=rwx
) read and execute for the group (5=r-x
) and read-only for the rest (4=r--
) will be specified like this:It is also possible to set and remove specific permissions using the format:
Where who is
u
for the owner ( user ) org
for the group ando
for the rest (others)So if you wanted to remove the write permission for others you can write:
I read it as: chmod other less write file
Or to add execute permission to the owner
chmod user plus execute file
If the "who" is not specified, the add or remove applies to the three groups, so that everyone can read it would be
Change mode (
chmod
), changes the access permissions (read, write and execute.r
,w
andx
respectively):In the example above I added execute permissions (
x
) to everyone (user, group and others) usinga
Change owner (
chown
) change to the owners (users, groups):In the example above I changed the
user:group
to file fromcesar:cesar
tojuan:juan
The chown command is used to change a file's owner and user group information, the chmod command is used to change file access permissions such as write, read, and execute.
For example:
Assign all permissions to the file
archivo.txt
Assign read and write permissions, not execution permissions, to all the files and directories of the directory where we execute the command.
This gives permissions to all files and directories in the directory where the command is invoked and in all directories that hang from it. The assigned permissions are read to all users, write only to the owner of the file, and execute to no one.
chown
- change the user and group owners of fileschmod
- change file access permissionsIf you want to modify the permissions on a certain group or a certain user, you can use
chmod
giving the corresponding permissions.Now in the case that the permissions are correctly assigned for example, but you decide that the directory or file no longer belongs to a certain user or group, you do it by changing it through
chown
Easy.
chmod allows you to change write, read, and execute permissions for a file or directory. While chown allows you to change the owner of the file or directory.
Examples for chmod:
Give all permissions to the file.
Give execution permissions
Remove read and write permissions to the group
Examples for chown:
change owner
Change the user owner in the developers group