Changing Ownership with chown and Permissions with chmod
Command chown will reassign the ownership of a directory:
- • sudo chown suzy:users /path_to/folder will reassign the directory "folder" to user "suzy" and group "users" but will leave the contents of "folder" unchanged
- • sudo chown -R suzy:users /path_to/folder will reassign the directory "folder" to user "suzy" and group "users" recursively (i.e. all the contained files and subdirectories are also reassigned).
Command chmod will reassign the permissions on a directory. You can express the options either numerically or in terms of patterns formed from r, w and x.
Chmod via patterns: Make patterns from u for user/owner, g for group and o for others (i.e. neither the owner of members of the group). Couple these with any or all of r for read, w for write and x for executable. For example you can set u=r, u=rw or u=rwx and so on. You can set an additional bit on a directory, the restricted deletion bit, denoted t, whereby only the owner (or root) can delete an included file or directory. There are many combinations of options. I will list some common ones to exemplify the usage:
- • sudo chmod u=r,g=,o= /path_to/folder will change a directory into readable only for the owner and otherwise forbidden (dr--------).
- • sudo chmod u=rwx,g=rx,o=rx /path_to/folder will create the normal Linux permissions on a directory: writeable by owner, readable by all and where files are executable by all (drwxr-xr-x).
- • sudo chmod u=rwx,g=rwx,o=rwx /path_to/folder gives a directory unlimited permissions: writeable by all, readable by all and executable by all (drwxrwxrwx).
- • sudo chmod +t /path_to/folder will add the "sticky" or "restricted deletion" bit to a directory whereby items inside the directory can be renamed or deleted only by the item's owner, the directory's owner, or the superuser.
- • sudo chmod -t /path_to/folder will remove the "sticky" or "restricted deletion" bit from a directory.
Chmod via numbers: This method is more elegant but perhaps harder to remember. Here's the template:
400 read by owner
040 read by group
004 read by anybody (other)
200 write by owner
020 write by group
002 write by anybody
100 execute by owner
010 execute by group
001 execute by anybody
Calculate the number to use in chmod by adding the numbers from all the filled locations, using the table to the left or the graphic below.
Here are the examples I gave above, recast into the numerical format:
- • dr--------: sudo chmod 400 changes a directory into readable only for the owner and otherwise forbidden.
- • drwxr-xr-x: sudo chmod 755 sets the normal Linux permissions on a directory: writeable by owner, readable by all and where files are executable by all.
- • drwxrwxrwx: sudo chmod 777 gives a directory unlimited permissions: writeable by all, readable by all and executable by all.
- • Add 1000 to the numerical value you use in chmod if you wish to set the "sticky" or "restricted deletion" bit on a directory (items inside the directory can be renamed or deleted only by the item's owner, the directory's owner, or the superuser).
Footnote for the advanced:
1000 sticky / restricted delete
2000 set gid on execution
4000 set uid on execution