简体   繁体   English

linux 中的进程如何决定它拥有的权限

[英]how does a process in linux decides privileges it has

I want to know how does a process in Linux decides what privileges it has?我想知道 Linux 中的进程如何决定它拥有什么权限?

Suppose there is a binary program Read_File that reads from file /home/myname/data.txt and displays the contents of it to the STD output;假设有一个二进制程序Read_File从文件/home/myname/data.txt中读取,并将其内容显示到 STD output; now, how does Read_File decides whether or not it has permission to read data.txt , what type of ids it checks to decide the privileges?现在, Read_File如何决定它是否有权读取data.txt ,它检查什么类型的 id 来决定权限?

First, a bit of background:首先,一点背景:

The process is usually run by a specific user.该过程通常由特定用户运行。 So for example, if you log in yourself and run the program, it will run with the same privileges as yourself.因此,例如,如果您自己登录并运行该程序,它将以与您相同的权限运行。 You can check the permissions on the file with either stat or ls -l .您可以使用statls -l检查文件的权限。

Using stat使用stat

malbert@dredg:/tmp$ stat foo
  File: `foo'
  Size: 0               Blocks: 0          IO Block: 4096   regular empty file
Device: fb00h/64256d    Inode: 618         Links: 1
Access: (0644/-rw-r--r--)  Uid: (11204/ malbert)   Gid: (10513/domain users)
Access: 2011-06-10 13:03:27.181227226 +0200
Modify: 2011-06-10 13:03:27.181227226 +0200
Change: 2011-06-10 13:03:27.181227226 +0200

The important infos here are:这里的重要信息是:

Access: (0644/-rw-r--r--)  Uid: (11204/ malbert)   Gid: (10513/domain users)

This tells you the permissions for the owner ( rw- ), group ( r-- ) and everyone else ( r-- ).这告诉您所有者 ( rw- )、组 ( r-- ) 和其他所有人 ( r-- ) 的权限。 It also shows you the current owner id ( Uid ) and the current group id ( Gid ).它还向您显示当前所有者 id ( Uid ) 和当前组 id ( Gid )。

The abbreviations stand for:缩写代表:

  • r = read access r = 读访问
  • w = write access w = 写访问
  • x = execute/traverse directory access x = 执行/遍历目录访问

Using ls -l使用ls -l

ls -l gives you a quick summary: ls -l给你一个快速的总结:

malbert@dredg:/tmp$ ls -l /tmp
total 48
drwx------ 2 malbert domain users 4096 2011-06-10 08:51 akonadi-malbert.zOSngu
-rw-r--r-- 1 malbert domain users    0 2011-06-10 13:03 foo
drwx------ 2 kdm     nogroup      4096 2011-06-10 08:51 kde-kdm
drwx------ 3 malbert domain users 4096 2011-06-10 08:51 kde-malbert
[snip]

Here you can see the same info as with stat , but as a summary.在这里,您可以看到与stat相同的信息,但作为摘要。 Also, the uid's and gid's are resolved into names (in this case malbert and domain users ).此外,uid 和 gid 被解析为名称(在本例中malbertdomain users )。 You can use ls -u to see these as numeric values.您可以使用ls -u将这些视为数值。

In case you want to run the application as a different user as yourself, you can either use su , sudo or your application itself can drop priviledges and change the user it is running as.如果您想以与您自己不同的用户身份运行应用程序,您可以使用susudo或者您的应用程序本身可以放弃特权并更改它正在运行的用户。 This is usually the way system daemons do things.这通常是系统守护进程做事的方式。

ACLs / extended attributes ACL/扩展属性

Be careful about extended attributes.小心扩展属性。 When listing the files using ls -l these are visible with an appended + sign.使用ls -l列出文件时,这些文件通过附加的+号可见。 For example:例如:

malbert@dredg:/tmp$ ls -l
total 48
drwx------  2 malbert domain users 4096 2011-06-10 08:51 akonadi-malbert.zOSngu
-rw-rwxr--+ 1 malbert domain users    0 2011-06-10 13:03 foo
drwx------  2 kdm     nogroup      4096 2011-06-10 08:51 kde-kdm
drwx------  3 malbert domain users 4096 2011-06-10 08:51 kde-malbert
[snip]

Notice the following line:请注意以下行:

-rwxr--+ 1 malbert domain users    0 2011-06-10 13:03 foo

The + sign in -rwxr--+ points to extended attributes. -rwxr--+中的+符号指向扩展属性。 It is possible that these are ACLs.这些可能是 ACL。 There is an excellent document on ACLs in the SuSE documentation. SuSE 文档中有一篇关于 ACL 的优秀文档。 Go have a look at it if you need to.如果需要,请查看 Go。 Explaining ACLs would certainly explode this article, so I won't discuss those.解释 ACL 肯定会引爆这篇文章,所以我不会讨论这些。

Extended attributes could also be related to the file system.扩展属性也可能与文件系统有关。 See the man page of chattr for more information on that.有关更多信息,请参见chattr 的手册页

Now, as a sidenote: this is StackOverflow.现在,作为旁注:这是 StackOverflow。 Questions should be development related.问题应该与发展相关。 For questions like this one, there's http://www.serverfault.com .对于这样的问题,有http://www.serverfault.com But As you were not aware, that this is not a development problem, but more related to the OS, I felt I should answer anyway;)但是正如您所不知道的,这不是开发问题,而是与操作系统更相关,我觉得无论如何我都应该回答;)

Good luck, and have fun!祝好运并玩得开心点!

The process doesn't decide anything;该过程不决定任何事情。 whenever it tries an operation the operating system checks it's EUID and decides if has the required privileges.每当它尝试操作时,操作系统都会检查它的 EUID 并决定是否具有所需的权限。

For example when it tries to access a file, the owner and permissions of the file are checked.例如,当它尝试访问文件时,会检查文件的所有者和权限。 If the EUID of the process doesn't have enough privileges for an operation, that operation will fail (usually with EPERM ).如果进程的 EUID 没有足够的操作权限,则该操作将失败(通常使用EPERM )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM