Python os.getuid() 和 os.geteuid() 有什么区别?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14950378/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is difference between os.getuid() and os.geteuid()?
提问by Santosh Kumar
The documentation for os.getuid()says:
的文档os.getuid()说:
Return the current process's user id.
返回当前进程的用户 ID。
And of os.geteuid()says:
和os.geteuid()说:
Return the current process's effective user id.
返回当前进程的有效用户 ID。
So what is the difference between user idand effective user id?
那么用户 ID和有效用户 ID之间有什么区别呢?
For me both works same (on both 2.x and 3.x). I am using it to check if script is being run as root.
对我来说,两者的工作方式相同(在 2.x 和 3.x 上)。我正在使用它来检查脚本是否以 root 身份运行。
采纳答案by Blckknght
To understand how os.getuidand os.geteuiddiffer, you need to understand that they're are not Python specific functions (other than the osmodule prefix). Those functions are wrapping the getuidand geteuidsystem calls that are provided by essentially all Unix-like operating systems.
要了解如何os.getuid和os.geteuid不同,您需要了解它们不是 Python 特定的函数(os模块前缀除外)。这些函数封装了几乎所有类 Unix 操作系统提供的getuid和geteuid系统调用。
So, rather than looking at Python docs (which are not likely to give a lot of details), you should look at the docs for your operating system. Hereis the relevant documentation for Linux, for example. Wikipedia also has a good article on Unix User IDs.
因此,与其查看 Python 文档(不太可能提供很多细节),不如查看适用于您的操作系统的文档。例如,这里是 Linux 的相关文档。维基百科也有一篇关于 Unix 用户 ID的好文章。
The difference between the regular UID and the Effective UID is that only the EUID is checked when you do something that requires special access (such as reading or writing a file, or making certain system calls). The UID indicates the actual user who is performing the action, but it is (usually) not considered when examining permissions. In normal programs they will be the same. Some programs change their EUID to add or subtract from the actions they are allowed to take. A smaller number also change their UID, to effectively "become" another user.
常规 UID 和有效 UID 之间的区别在于,当您执行某些需要特殊访问的操作(例如读取或写入文件,或进行某些系统调用)时,只会检查 EUID。UID 指示执行操作的实际用户,但在检查权限时(通常)不考虑它。在正常程序中,它们是相同的。一些程序会更改其 EUID,以增加或减少允许执行的操作。较小的数字也会更改他们的 UID,以有效地“成为”另一个用户。
Here's an example a program that changes its EUID: The passwdprogram (which is used to change your password) must write to the system's password file, which is owned by the root user. Regular users can't write to that file, since if they could, they could change everyone else's password too. To resolve this, the passwdprogram has a bit set in its file permissions (known as the setuid bit) that indicates to the OS that it should be run with the EUID of the program's owner (e.g. root) even when it is launched by another user. The passwdprogram would then see its UIDas the launching user, and its EUID as root. Writing to the system password file requires the EUID to be privileged. The UID is useful too, since passwdneeds to know which user it's changing the password for.
下面是一个更改其 EUID 的passwd程序示例: 该程序(用于更改您的密码)必须写入系统的密码文件,该文件由 root 用户拥有。普通用户无法写入该文件,因为如果可以,他们也可以更改其他人的密码。为了解决这个问题,passwd程序在其文件权限(称为setuid bit)中设置了一个位,向操作系统表明它应该使用程序所有者的 EUID(例如root)运行,即使它是由另一个用户启动的。然后passwd程序会将其UID视为启动用户,并将其 EUID 视为 root。写入系统密码文件需要 EUID 具有特权。UID 也很有用,因为passwd需要知道它正在为哪个用户更改密码。
There are a few other cases where the UID and EUID won't match, but they're not too common. For instance, a file server running as the super user might change its EUID to match a specific user who is requesting some file manipulations. Using the user's EUID allows the server to avoid accessing things that the user is not allowed to touch.
还有一些 UID 和 EUID 不匹配的其他情况,但它们并不常见。例如,作为超级用户运行的文件服务器可能会更改其 EUID 以匹配请求某些文件操作的特定用户。使用用户的 EUID 可以让服务器避免访问用户不允许触摸的东西。
回答by Mikhail Vladimirov
Function os.getuid()returns ID of a user who runs your program. Function os.geteuid()of a user your program use permissions of. In most cases this will be the same. Well known case when these values will be different is when setuidbit is set for your program executable file, and user that runs your program is different from user that own program executable. In this case os.getuid()will return ID of user who runs program, while os.geteuid()will return ID of user who own program executable.
函数os.getuid()返回运行您的程序的用户的 ID。功能os.geteuid()的用户程序中使用权限。在大多数情况下,这将是相同的。这些值不同的众所周知的情况setuid是为您的程序可执行文件设置了位,并且运行您的程序的用户与拥有程序可执行文件的用户不同。在这种情况下,os.getuid()将返回运行程序os.geteuid()的用户 ID ,同时返回拥有程序可执行文件的用户 ID。

