I know that r means read permission, w means write permission, and x means execute permission in the output of ls -l, like -rwxr-xr-x.

But My question is what is behavior difference on operation system to this modes?
I mean what system does with read only file and what system does with writable file or how system does when it see an executable file?

Or better to say: why are not all of files in one mode?

I'm asking not about the differences of permissions for user, group, and others, please ignore that.
My question is about what the technical difference between executable mode and readable and writable mode. What is the difference between these modes on files access?

  • I edited the question and it is much more clear now. As the real question is pretty good, I think it's worth reopening. – Volker Siegel Aug 17 '14 at 0:24

When a program wants to read or write to a file, it needs to call the system call open() for the file first.
One of the arguments to the call specifies which operations the program wants to be able to do.
It the program indicates it wants to read or write the file, and the process does not have the perspective for the operations, the open() call ends up in the error EACCESS, and the file can not be used.

In a similar way, when a program - for example, your shell - needs to execute a program file, it uses the system call execve(). This returns the error EACCESS if the execute permission is not given by the file mode.

Below are some relevant parts of the man pages in section 2, "system calls"

From the man page for open(2) man 2 open:

OPEN(2)                   Linux Programmer's Manual                   OPEN(2)

        open, creat - open and possibly create a file or device

        #include <sys/types.h>
        #include <sys/stat.h>
        #include <fcntl.h>

        int open(const char *pathname, int flags);

[ ... ]

        The argument flags must include one of  the  following  access  modes:
        O_RDONLY,  O_WRONLY,  or O_RDWR.  These request opening the file read-
        only, write-only, or read/write, respectively.

[ ... ]

        EACCES The requested access to the file is not allowed, or search per‐
               mission is denied for one of the directories in the path prefix
               of pathname, or the file did not exist yet and write access  to
               the  parent  directory  is not allowed.  (See also path_resolu‐

From the man page for execve(2) man 2 execve:

EXECVE(2)                  Linux Programmer's Manual                  EXECVE(2)

        execve - execute program

        #include <unistd.h>

        int execve(const char *filename, char *const argv[],
                   char *const envp[]);

[ ... ]

        EACCES Execute permission is denied for the file or a script or ELF interpreter.
| improve this answer | |

When user tries to access a file or directory, the kernel allows to opens the file or directory with the mode parameter set to what permissions is assigned for that particular user.

So if the user has only read permission the file opened by an application will be given only to read the contents of the file by the kernel.

| improve this answer | |

RWX permissions control how users on the system can interact with files. For example, the Root user can do anything to a file. whereas a standard user's access to a particular file is governed by the permissions of that file. As far as I know the OS doesn't directly interact with files itself. But instead uses system user accounts which in turn obey the file permissions. System user accounts normally have a uid of below 500 (some distros use 1000) and cannot be logged in to directly. The OS will use these accounts to modify files.

| improve this answer | |
  • This is incorrect. The OS is responsible for obeying the permissions whenever the appropriate system call is executed. – Jordan Samuels Aug 16 '14 at 14:00
  • @Jordan Samuels What part of the answer are you saying is incorrect? – mintyfreshpenguin Aug 16 '14 at 14:07
  • The statement that they don't affect the OS. They do - because the permissions affect the behavior of the system calls such as open(), which are provided by the OS/kernel. – Jordan Samuels Aug 16 '14 at 14:14
  • Thank you. I see what you mean now. I'll edit my answer to reflect your comments. – mintyfreshpenguin Aug 16 '14 at 18:11

Not the answer you're looking for? Browse other questions tagged or ask your own question.