文件权限对系统级的影响是什么?

文件权限对系统级的影响是什么?

我知道这意味着在 的输出中r表示读取权限,w表示写入权限,表示执行权限,例如。xls -l-rwxr-xr-x

但我的问题是操作系统与这种模式的行为差异是什么?
我的意思是系统对只读文件做什么,系统对可写文件做什么,或者系统在看到可执行文件时如何做?

或者更好地说:为什么不是所有文件都处于一种模式?

我问的不是用户、组和其他人的权限差异,请忽略。
我的问题是可执行模式和可读可写模式之间的技术区别是什么。这些文件访问模式有什么区别?

答案1

当用户尝试访问文件或目录时,允许使用设置为该特定用户分配的权限的参数kernel打开文件或目录。mode

因此,如果用户仅具有read应用程序打开的文件的权限,则只能通过kernel.

答案2

当程序想要读取或写入文件时,需要open()先调用该文件的系统调用。
调用的参数之一指定程序希望能够执行哪些操作。
如果程序指示要读取或写入文件,并且进程没有操作的视角,则调用open()最终会出现错误EACCESS,并且文件无法使用。

类似地,当程序(例如您的 shell)需要执行程序文件时,它会使用系统调用execve()EACCESS如果文件模式未给出执行权限,则会返回错误。


以下是第 2 节中手册页的一些相关部分,“系统调用”

来自 open(2) 的手册页man 2 open:

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



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

 SYNOPSIS
        #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.

[...]

ERRORS
        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‐
               tion(7).)


来自 execve(2) 的手册页man 2 execve:

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



 NAME
        execve - execute program

 SYNOPSIS
        #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.

答案3

RWX 权限控制系统上的用户如何与文件交互。例如,Root 用户可以对文件执行任何操作。而标准用户对特定文件的访问由该文件的权限控制。据我所知,操作系统本身并不直接与文件交互。而是使用系统用户帐户,而系统用户帐户又遵守文件权限。系统用户帐户的 uid 通常低于 500(某些发行版使用 1000),并且无法直接登录。操作系统将使用这些帐户来修改文件。

相关内容