1. Ownership and permissions
In the Unix security model, a user may own files and directories. When a user owns a file or a directory, he has control over its access (he decides who can access it). To facilitate granting permissions, users may belong to one or more groups. If the owner of a file grants permissions to a group, then all the members of the group have access to this file. Besides granting access to a group, an owner may grant some access rights to everybody, which in Unix terms is referred to as others.
-
When you use the command
ls -l, the first column of the output (the one which has some dashes) shows the attributes of the file.> foo.txtls -l foo.txtThe first char of attributes shows the file type. If this char is a
-it is a regular file,dis for a directory,lfor a symbolic link,cfor a character special file (for example a keyboard or network card), anddfor block special file (like a hard drive or RAM).The remaining 9 characters show the access rights for the file's owner, the file's group, and the rest of the users. They are
rwxfor the user,rwxfor the group, andrwxfor the others, whererstands for reading (viewing the content of the file),wis for writing (modifying the content of the file), andxis for executing (running the file like a program or a script). If there is a minus (or a dash) instead ofr,worx, it means that the corresponding right is missing.For a directory, the
xattribute allows a directory to be entered (e.g.cd directory). Therattribute allows a directory's content to be listed (withls), but only if thexattribute is also set. And thewattribute allows files within a directory to be created, deleted, and renamed, if thexattribute is also set. -
We can change the permissions of a file or directory with
chmod. Only the owner and the superuser can change the permissions of a file or directory.ls -l foo.txtchmod 600 foo.txtls -l foo.txtIn this case we are using octal notation for telling
chmodwhat permissions to set. For example 7 (111) is forrwx, 6 (110) is forrw-, 5 (101) is forr-x, 4 (100) is forr--, and 0 is for---(no permissions).We can also use symbolic notation with
chmod, where u (user) represents the owner, g represents the group, and o (others) represents the world. There is also the symbol a (all) which is a combination of u, g and o.-
Add the execute permission to the user:
chmod u+x foo.txtls -l foo.txt -
Remove the execute permission from the user:
chmod u-x foo.txtls -l foo.txt -
Add execute to user. Group and others should have only read and execute:
chmod u+x,go=rx foo.txtls -l foo.txt -
Remove the execute permission from all:
chmod ugo-x foo.txtchmod a-x foo.txtchmod -x foo.txtls -l foo.txt
-
-
The
umaskcommand controls the default permissions given to a file when it is created:umaskThis octal notation tells which bits will be masked (removed) from the attributes of a file:
rm -f foo.txt
> foo.txt
ls -l foo.txtThe reason that the others don't have a
wpermission is because of the mask. Remember that the number 2 in octal is written as010, so the permissions expressed by it are-w-. This means that thewpermission for the others will be removed from the attributes.Let's change the mask and try again:
rm foo.txtumask 0000
umask> foo.txt
ls -l foo.txtRestore the normal umask:
umask 0022
umask