Linux is definitely a multi-user system, so it is necessary to provide a permission system to control the set of authorized operations on files and directories, which includes all the system resources and devices (on a Unix system, any device is represented by a file or directory). This principle is common to all Unix systems, but a reminder is always useful, especially as there are some interesting and relatively unknown advanced uses.
Each file or directory has specific permissions for three categories of users:
its owner (symbolized by u
as in “user”);
its owner group (symbolized by g
as in “group”), representing all the members of the group;
the others (symbolized by o
as in “other”).
Three types of rights can be combined:
reading (symbolized by r
as in “read”);
writing (or modifying, symbolized by w
as in “write”);
executing (symbolized by x
as in “eXecute”).
In the case of a file, these rights are easily understood: read access allows reading the content (including copying), write access allows changing it, and execute access allows you to run it (which will only work if it's a program).
A directory is handled differently. Read access gives the right to consult the list of its entries (files and directories), write access allows creating or deleting files, and execute access allows crossing through it (especially to go there with the cd
command). Being able to cross through a directory without being able to read it gives permission to access the entries therein that are known by name, but not to find them if you do not know their existence or their exact name.
Three commands control the permissions associated with a file:
chown user file
changes the owner of the file;
chgrp group file
alters the owner group;
chmod rights file
changes the permissions for the file.
There are two ways of presenting rights. Among them, the symbolic representation is probably the easiest to understand and remember. It involves the letter symbols mentioned above. You can define rights for each category of users (u
/g
/o
), by setting them explicitly (with =
), by adding (+
), or subtracting (-
). Thus the u=rwx,g+rw,o-r
formula gives the owner read, write, and execute rights, adds read and write rights for the owner group, and removes read rights for other users. Rights not altered by the addition or subtraction in such a command remain unmodified. The letter a
, for “all”, covers all three categories of users, so that a=rx
grants all three categories the same rights (read and execute, but not write).
The (octal) numeric representation associates each right with a value: 4 for read, 2 for write, and 1 for execute. We associate each combination of rights with the sum of the figures. Each value is then assigned to different categories of users by putting them end to end in the usual order (owner, group, others).
For instance, the chmod 754 file
command will set the following rights: read, write and execute for the owner (since 7 = 4 + 2 + 1); read and execute for the group (since 5 = 4 + 1); read-only for others. The 0
means no rights; thus chmod 600 file
allows for read/write rights for the owner, and no rights for anyone else. The most frequent right combinations are 755
for executable files and directories, and 644
for data files.
To represent special rights, you can prefix a fourth digit to this number according to the same principle, where the setuid
, setgid
and sticky
bits are 4, 2 and 1, respectively. chmod 4754
will associate the setuid
bit with the previously described rights.
Note that the use of octal notation only allows to set all the rights at once on a file; you can not use it to simply add a new right, such as read access for the group owner, since you must take into account the existing rights and compute the new corresponding numerical value.